我正在按照教程讓 qr 閱讀器在 python 中作業,但在運行它時遇到以下錯誤:
發生例外:錯誤 OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'line' 多載決議失敗:
- 無法決議“pt1”。索引為 0 的序列項型別錯誤
- 無法決議“pt1”。索引為 0 的序列項的型別錯誤 File "C:\Users\me\project\qrreader.py", line 18, in 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 的更改有關,但我似乎無法修復它。
如果不是元組,line 函式需要什么?
uj5u.com熱心網友回復:
你bbox是一個形狀為 3 維陣列(1,4,2)。我建議您通過將其重塑為二維陣列來簡化它。要將其轉換為int,numpy 陣列具有該astype方法。最后, atuple仍然需要cv2.line,所以保持原樣。
這是一個可能的解決方案塊:
# 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)
Numpy 檔案:reshape,astype。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/365636.html
