使用OpenCV Python检测二维码
参考
实现方法:
import cv2
capture = cv2.VideoCapture(0) # 视频相关
detect = cv2.QRCodeDetector() # 二维码相关
while True:
_, frame = capture.read()
text, points, _ = detect.detectAndDecode(frame)
if points is not None:
# 画红线,标记二维码
points = tuple(points)[0]
len_points = len(points)
for i in range(len_points):
cv2.line(frame, tuple(points[i]), tuple(points[(i+1)%len_points]), (0, 0, 255), 5)
# 命令行输出二维码值
print("decode: ", text)
cv2.imshow('qrcode', frame)
# 使用按键'q'退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源
capture.release()
# 销毁窗口
cv2.destroyAllWindows()
实现效果
存在问题
- 帧率不高(使用的是树莓派 3B+)。
- 可能会报错:
cv2.error: OpenCV(4.4.0) /tmp/pip-wheel-2l8ccy47/opencv-python/modules/core/src/alloc.cpp:73: error: (-4: Insufficient memory) Failed to allocate 1521000000 bytes in function 'OutOfMemoryError'
,重新启动程序可以解决。
comment:
- Valine
- LiveRe
- ChangYan