当前位置:
首页
文章
前端
详情

Python实例:通过摄像头实时识别一维条形码

#coding:utf-8
'''
通过摄像头识别一维条形码
'''

import cv2
from pyzbar.pyzbar import decode

# results = decode(cv2.imread('datas/images/barcode-3.jpg'))
# for result in results:
#     print('barcode = %s'% str(result.data))

cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print('cannot open camera 0')
    exit(0)

while True:
    ret,frame = cap.read()
    if not ret:
        print('cannot grab frame from camera')
        continue
    results = decode(frame)
    for result in results:
        print('barcode = %s' % result.data)
        barcode_roi = frame[result.rect.left:result.rect.width,result.rect.top:result.rect.height]
        cv2.imshow('barcode:%s' % result.data,barcode_roi)

    cv2.imshow('camera',frame)
    key = cv2.waitKey(10)
    if key == 27:
        break

cv2.destroyAllWindows()

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。