[英]How can I read qr codes using CV2 given their CV2's breaking of tuples?
我正在按照教程讓 qr 閱讀器在 python 中工作,但是在運行它時遇到以下錯誤:
發生異常:錯誤 OpenCV(4.5.4):-1: 錯誤:(-5:Bad argument) in function 'line' 重載解析失敗:
- 無法解析“pt1”。 索引為 0 的序列項類型錯誤
- 無法解析“pt1”。 索引為 0 的序列項的類型錯誤 文件“C:\Users\me\project\qrreader.py”,第 18 行,在 cv2.line(img, tuple(bbox[i][0]), tuple(bbox[ (i+1) % len(bbox)][0]), 顏色=(255,
腳本如下
import cv2
# set up camera object
cap = cv2.VideoCapture(0)
# QR code detection object
detector = cv2.QRCodeDetector()
while True:
# get the image
_, img = cap.read()
# get bounding box coords and data
data, bbox, _ = detector.detectAndDecode(img)
# if there is a bounding box, draw one, along with the data
if(bbox is not None):
for i in range(len(bbox)):
cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255,
0, 255), thickness=2)
cv2.putText(img, data, (int(bbox[0][0][0]), int(bbox[0][0][1]) - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
if data:
print("data found: ", data)
# display the image preview
cv2.imshow("code detector", img)
if(cv2.waitKey(1) == ord("q")):
break
# free camera object and exit
這個腳本似乎在所有的教程中,但據我所知,它似乎已經與 opencv 4.5.2 更改中斷,但我似乎無法修復它。
如果不是元組,那么 function 行需要什么?
您的bbox
是一個形狀為(1,4,2)
1,4,2) 的 3 維數組。 我建議您通過將其重塑為二維數組來簡化它。 要將其轉換為int
, numpy arrays 具有astype
方法。 最后, cv2.line
仍然需要一個tuple
,所以保持原樣。
這是一個可能的解決方案塊:
# if there is a bounding box, draw one, along with the data
if bbox is not None:
bb_pts = bbox.astype(int).reshape(-1, 2)
num_bb_pts = len(bb_pts)
for i in range(num_bb_pts):
cv2.line(img,
tuple(bb_pts[i]),
tuple(bb_pts[(i+1) % num_bb_pts]),
color=(255, 0, 255), thickness=2)
cv2.putText(img, data,
(bb_pts[0][0], bb_pts[0][1] - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.