I'm doing a simple project to detect QR codes and draw bounding boxes from a webcam capture.
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()
The error I'm getting is as follows:
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
From what I've read from another thread, the tuples cv2.line is being passed have to be comprised of ints.
I've tried:
- iterating through bbox and casting each value as ints
for j in range(n):
cv2.line(img,
tuple(int(bbox[j][0])),
tuple(int(bbox[ (j+1) % n][0])),
(255,0,0),
3)
- using .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)
Edit: the contents of bbox (four corners of a single bounding box) are as follows:
[[[134.13043 150. ]
[362.3125 150. ]
[360.64886 362.94632]
[143.58028 367.34634]]]
Any suggestions would be greatly appreciated!