Python - Opencv + pyzbar实时摄像头识别二维码

直接上代码:

import cv2
from pyzbar.pyzbar import decode

cap = cv2.VideoCapture(0)  # 打开摄像头

while True:  # 循环读取摄像头帧
    ret, frame = cap.read()

    # 在循环中,将每一帧作为图像输入,使用pyzbar的decode()函数识别二维码
    barcodes = decode(frame)

    # 绘制二维码框和数据
    for barcode in barcodes:
        rect = barcode.rect
        x, y, w, h = rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        data = barcode.data.decode("utf8")
        cv2.putText(frame, data, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        print("Result:" + data)
    # 显示结果  
    cv2.imshow('Image', frame)

    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

简单使用的记录
Python - Opencv + pyzbar实时摄像头识别二维码_第1张图片

你可能感兴趣的:(Python,opencv,python,计算机视觉)