-
Notifications
You must be signed in to change notification settings - Fork 726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OpenCV(4.5.2) 👎 error: (-5:Bad argument) in function 'rectangle' #69
Comments
this is because cv2.rectangle(). only take int ,u can copy my write(x, results) below
|
Got the same problem and applied @EZ4BRUCE suggestion. The program runs smoothly, but the output image is a bit strange, the detected bounding boxes are no longer boxes, they become some pure colour rectangles. Here is the det image I have now: I'm still digging in this issue, will update if I found the solution. |
Update: Here is the modified (and should be bug-free) code: def write(x, results):
c1 = tuple(x[1:3].int())
c2 = tuple(x[3:5].int())
d1 = (int(c1[0]), int(c1[1]))
d2 = (int(c2[0]), int(c2[1]))
img = results[int(x[0])]
cls = int(x[-1])
color = random.choice(colors)
label = "{0}".format(classes[cls])
cv2.rectangle(img, d1, d2, color, 1)
t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1, 1)[0]
c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4
d2 = (int(c2[0]), int(c2[1]))
cv2.rectangle(img, d1, d2, color, -1)
cv2.putText(img, label, (int(c1[0]), int(c1[1] + t_size[1] + 4)), cv2.FONT_HERSHEY_PLAIN, 1, [225, 255, 255], 1)
return img |
You can change c1,c2 from tensor to int type,such as c1 = (int(c1[0].item()), int(c1[1].item()))
c2 = (int(c2[0].item()), int(c2[1].item())) Therefore, the whole function is def write(x, results):
c1 = tuple(x[1:3].int())
c2 = tuple(x[3:5].int())
# 将c1和c2的值转换为整数类型
c1 = (int(c1[0].item()), int(c1[1].item()))
c2 = (int(c2[0].item()), int(c2[1].item()))
img = results[int(x[0])]
cls = int(x[-1])
color = random.choice(colors)
label = "{0}".format(classes[cls])
cv2.rectangle(img, c1, c2, color, 2)
t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1 , 1)[0]
c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4
cv2.rectangle(img, c1, c2,color, -1)
cv2.putText(img, label, (c1[0], c1[1] + t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 1, [225,255,255], 1);
return img |
Objects Detected: bicycle truck dog
Traceback (most recent call last):
File "detect.py", line 201, in
list(map(lambda x: write(x, loaded_ims), output))
File "detect.py", line 201, in
list(map(lambda x: write(x, loaded_ims), output))
File "detect.py", line 193, in write
cv2.rectangle(img, c1, c2, color, 1)
cv2.error: OpenCV(4.5.2) 👎 error: (-5:Bad argument) in function 'rectangle'
I am getting this error when I ran the detect.py file for testing. There is some error in the cv2.rectangle().
If anyone knows help me.
The text was updated successfully, but these errors were encountered: