pytorch-yolo-v3 icon indicating copy to clipboard operation
pytorch-yolo-v3 copied to clipboard

Help!!!cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle'

Open Zxl1556180489 opened this issue 3 years ago • 5 comments

Traceback (most recent call last): File "/home/zxl/PycharmProjects/wjchaoGit/pytorch-yolo-v3-master(usbcam)/pytorch-yolo-v3-master/cam_demo.py", line 151, in list(map(lambda x: write(x, orig_im), output)) File "/home/zxl/PycharmProjects/wjchaoGit/pytorch-yolo-v3-master(usbcam)/pytorch-yolo-v3-master/cam_demo.py", line 151, in list(map(lambda x: write(x, orig_im), output)) File "/home/zxl/PycharmProjects/wjchaoGit/pytorch-yolo-v3-master(usbcam)/pytorch-yolo-v3-master/cam_demo.py", line 49, in write cv2.rectangle(img, c1, c2, color, 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

Zxl1556180489 avatar Jun 22 '21 07:06 Zxl1556180489

Hi Zxl1556180489, Did you solve this issue? I am getting the same issue.

syed-javed avatar Aug 23 '21 07:08 syed-javed

At that point c1 and c2 are still tensors, so OpenCV raises type error. To fix it change lines 286 and 287 to:
c1 = tuple(x[1:3].cpu().int().numpy()) c2 = tuple(x[3:5].cpu().int().numpy())

VasLem avatar Sep 05 '21 21:09 VasLem

c1 and c2 are tensors,you could use like c1[0].item() to fix it. cv2.rectangle(img, (c1[0].item(),c1[1].item()), (c2[0].item(),c2[1].item()), color, 2)

ray7jq avatar Oct 25 '21 06:10 ray7jq

x = x.detach().cpu().int().numpy()
img = results[x[0]]
c1 = (x[1], x[2])
c2 = (x[3], x[4])
cls = int(x[-1])

expnn avatar Dec 02 '22 04:12 expnn

Replace lines 178 and 179 with this

c1 = tuple(map(torch.Tensor.item, x[1:3].int()))
c2 = tuple(map(torch.Tensor.item, x[3:5].int()))

Roboskeletron avatar May 21 '23 14:05 Roboskeletron