python的一维码识别

    一维码的识别,识别的速度和图像的大小有模切的关系,当需要快速的进行识别,一定要使用小图,否则时间会跟不上。

库的安装命令:pip install pyzbar

import cv2
import time
from pyzbar.pyzbar import decode


# img = cv2.imread("bar.jpg")

img_path = 'picture_2025-04-09_14-56-03-518_ORI.png'

  
    


  
# gen_rectangle1 (ROI_0, 1619.6, 1194.08, 2155.75, 2392.6)
img=cv2.imread(img_path)
# gen_rectangle1 (ROI_0, 781.349, 1112.69, 1204.74, 1499.9)
# img=image_camera1[1619:2155,1194:2392]



# 将图像转换为灰度图
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

for i in range(50):

    # 使用pyzbar进行条形码解码
    start_time = time.perf_counter()
    barcodes = decode(gray_image)
    end_time = time.perf_counter()
    elapsed_time = (end_time - start_time)*1000
    print("从收图像到检测出结果回传结果用的时间!!!!!!!!!!: {} ms".format(elapsed_time))

    # image_save_Path='Desktop/test.png'
    # # 遍历解码结果
    # for barcode in barcodes:
    #     barcode_data = barcode.data.decode("utf-8")
    #     barcode_type = barcode.type
    #     # 绘制边界框和文本
    #     x, y, w, h = barcode.rect
    #     cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    #     cv2.putText(img, barcode_data, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    #     cv2.imwrite(image_save_Path,img)

你可能感兴趣的:(python)