We Are Going To Discuss About (-5:Bad argument) in function ‘rectangle’ – Can’t parse ‘pt1’. Sequence item with index 0 has a wrong type. So lets Start this Python Article.
(-5:Bad argument) in function ‘rectangle’ – Can’t parse ‘pt1’. Sequence item with index 0 has a wrong type
- How to solve (-5:Bad argument) in function 'rectangle' – Can't parse 'pt1'. Sequence item with index 0 has a wrong type
The problem is that you are passing tuples with floats into the function's parameters as the points. Here is the error reproduced:
import cv2 import numpy as np img = np.zeros((600, 600), 'uint8') c1 = 50.2, 12.4 c2 = 88.8, 40.8 cv2.rectangle(img, c1, c2, (255, 0, 0), -1)
Output:Traceback (most recent call last): File "C:/Users/User/Desktop/temp.py", line 9, in <module> cv2.rectangle(img, c1, c2, (255, 0, 0), -1) cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle' > 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 > - Can't parse 'rec'. Expected sequence length 4, got 2 > - Can't parse 'rec'. Expected sequence length 4, got 2
And to fix it, simply use theint()
wrapper around the coordinates:import cv2 import numpy as np img = np.zeros((600, 600), 'uint8') c1 = 50.2, 12.4 c2 = 88.8, 40.8 cv2.rectangle(img, (int(c1[0]), int(c1[1])), (int(c2[0]), int(c2[1])), (255, 0, 0), -1)
- (-5:Bad argument) in function 'rectangle' – Can't parse 'pt1'. Sequence item with index 0 has a wrong type
The problem is that you are passing tuples with floats into the function's parameters as the points. Here is the error reproduced:
import cv2 import numpy as np img = np.zeros((600, 600), 'uint8') c1 = 50.2, 12.4 c2 = 88.8, 40.8 cv2.rectangle(img, c1, c2, (255, 0, 0), -1)
Output:Traceback (most recent call last): File "C:/Users/User/Desktop/temp.py", line 9, in <module> cv2.rectangle(img, c1, c2, (255, 0, 0), -1) cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle' > 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 > - Can't parse 'rec'. Expected sequence length 4, got 2 > - Can't parse 'rec'. Expected sequence length 4, got 2
And to fix it, simply use theint()
wrapper around the coordinates:import cv2 import numpy as np img = np.zeros((600, 600), 'uint8') c1 = 50.2, 12.4 c2 = 88.8, 40.8 cv2.rectangle(img, (int(c1[0]), int(c1[1])), (int(c2[0]), int(c2[1])), (255, 0, 0), -1)
Solution 1
The problem is that you are passing tuples with floats into the function’s parameters as the points. Here is the error reproduced:
import cv2
import numpy as np
img = np.zeros((600, 600), 'uint8')
c1 = 50.2, 12.4
c2 = 88.8, 40.8
cv2.rectangle(img, c1, c2, (255, 0, 0), -1)
Output:
Traceback (most recent call last):
File "C:/Users/User/Desktop/temp.py", line 9, in <module>
cv2.rectangle(img, c1, c2, (255, 0, 0), -1)
cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle'
> 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
> - Can't parse 'rec'. Expected sequence length 4, got 2
> - Can't parse 'rec'. Expected sequence length 4, got 2
And to fix it, simply use the int()
wrapper around the coordinates:
import cv2
import numpy as np
img = np.zeros((600, 600), 'uint8')
c1 = 50.2, 12.4
c2 = 88.8, 40.8
cv2.rectangle(img, (int(c1[0]), int(c1[1])), (int(c2[0]), int(c2[1])), (255, 0, 0), -1)
Original Author Ann Zen Of This Content
Solution 2
Just try the below changes it’s works for me.
fontScale = 0.5
score = out_scores[i]
class_ind = int(out_classes[i])
bbox_color = colors[class_ind]
bbox_thick = int(0.6 * (image_h + image_w) / 600)
c1, c2 = (coor[0], coor[1]), (coor[2], coor[3])
print(c1, c2, bbox_color, bbox_thick)
cv2.rectangle(image, (int(c1[0]), int(c1[1])), (int(c2[0]), int(c2[1])), bbox_color, bbox_thick)
if show_label:
bbox_mess = '%s: %.2f' % (classes[class_ind], score)
t_size = cv2.getTextSize(bbox_mess, 0, fontScale, thickness=bbox_thick // 2)[0]
c3 = (c1[0] + t_size[0], c1[1] - t_size[1] - 3)
cv2.rectangle(image, (int(c1[0]), int(c1[1])), (int(c3[0]), int(c3[1])), (255, 0, 0), -1) #filled
cv2.putText(image, bbox_mess, (int(c1[0]), int(c1[1] - 2)), cv2.FONT_HERSHEY_SIMPLEX,
fontScale, (0, 0, 0), bbox_thick // 2, lineType=cv2.LINE_AA)
return image
Original Author Darshana shah Of This Content
Solution 3
n_lines = len(bbox)
for i in range(n_lines):
# draw all lines
point1 = tuple(bbox[i][0])
point2 = tuple(bbox[(i+1) % n_lines][0])
cv2.line(img, point1, point2, (255, 0, 0), thickness=2)
Original Author Varshini Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.