鑑於他們的 CV2 破壞了元組,我如何使用 CV2 讀取二維碼?

金絲網

我正在按照教程讓 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 函數需要什麼?

羅勒

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 文檔:reshapeastype

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章