gpt4 book ai didi

python - OpenCV 错误 : bad argument, 重载解析失败,索引为 0 的序列项类型错误

转载 作者:行者123 更新时间:2023-12-02 15:56:43 27 4
gpt4 key购买 nike

<分区>

我正在做一个简单的项目来检测 QR 码并从网络摄像头捕获中绘制边界框。

import cv2
import numpy as np
import sys
import time
#
# Sanity Check
print("QR Scanner initialized.")


# Utility function to get a video frame from webcam.
# @param: cap is a cv2.videoCapture object.
def captureFrame(cap):
ret, frame = cap.read()
if ret == False:
print("Capture failed.")
return frame

# Utility function to draw bounding box on frame.
def display(img, bbox):
n = len(bbox)

for j in range(n):
cv2.line(img,
tuple(bbox[j][0]),
tuple(bbox[ (j+1) % n][0]),
(255,0,0),
3)
cv2.imshow("Video", img)

# Function to detect QR code in an input image
def qrDetect(inputImage):
# Create a qrCodeDetector Object.
qrDecoder = cv2.QRCodeDetector()

# Look for a qr code.
t = time.time()

data, bbox, rectifiedImage = qrDecoder.detectAndDecode(inputImage)
print("Time Taken for Detect and Decode : {:.3f} seconds.".format(time.time() - t))

# Print output if applicable.
if len(data) > 0:
print("Decoded Data : {}".format(data))
return 1, inputImage, bbox
else:
print("QR Code not detected")
return 0, inputImage, 0


# Main function.
def main():
print("I'm alive!")

# Stream webcam frames until 'q' is pressed.
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
frame = captureFrame(cap)
ret, img, bbox = qrDetect(frame)

if ret:
display(img, bbox)
else:
cv2.imshow("Video", img)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

#
if __name__ == "__main__":
main()

我得到的错误如下:

cv2.error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'line'
> Overload resolution failed:
> - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
> - Can't parse 'pt1'. Sequence item with index 0 has a wrong type

从我读到的 another thread ,传递的元组 cv2.line 必须由整数组成。

我试过:

  1. 遍历 bbox 并将每个值转换为整数
for j in range(n):
cv2.line(img,
tuple(int(bbox[j][0])),
tuple(int(bbox[ (j+1) % n][0])),
(255,0,0),
3)
  1. 使用.astype(int)
bbox = bbox.astype(int)
for j in range(n):
cv2.line(img,
tuple(bbox[j][0]),
tuple(bbox[ (j+1) % n][0]),
(255,0,0),
3)

编辑:bbox(单个边界框的四个角)内容如下:

[[[134.13043 150.     ]
[362.3125 150. ]
[360.64886 362.94632]
[143.58028 367.34634]]]

如有任何建议,我们将不胜感激!

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com