1

I study on a instance segmentation model. I complede training part. BUt in the inference part; i faced with an error as I showed below. I converted boxes[i][0] to the int(boxes[i][0]) but it gives also error. Are there any idea to help?

def get_prediction(img_path, confidence):
    
    img = Image.open(img_path)
    transform = transforms.ToTensor()
    img = transform(img)
    img = img.to(device)
    pred = model([img])
    pred_score = list(pred[0]['scores'].detach().cpu().numpy())
    pred_t = [pred_score.index(x) for x in pred_score if x>confidence][-1]
    masks = (pred[0]['masks']>0.5).squeeze().detach().cpu().numpy()
    # print(pred[0]['labels'].numpy().max())
    pred_class = [CLASS_NAMES[i] for i in list(pred[0]['labels'].cpu().numpy())]
    pred_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]['boxes'].detach().cpu().numpy())]
    masks = masks[:pred_t+1]
    pred_boxes = pred_boxes[:pred_t+1]
    pred_class = pred_class[:pred_t+1]
    return masks, pred_boxes, pred_class

def segment_instance(img_path, confidence=0.5, rect_th=2, text_size=2, text_th=2):

    masks, boxes, pred_cls = get_prediction(img_path, confidence)
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    for i in range(len(masks)):
      rgb_mask = get_coloured_mask(masks[i])
      img = cv2.addWeighted(img, 1, rgb_mask, 0.5, 0)
      cv2.rectangle(img, boxes[i][0], boxes[i][1],color=(0, 255, 0), thickness=rect_th)
      cv2.putText(img,pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX, text_size, (0,255,0),thickness=text_th)
    plt.figure(figsize=(20,30))
    plt.imshow(img)
    plt.xticks([])
    plt.yticks([])
    plt.show()



segment_instance('image1.png', confidence=0.4)

error:

Input In [31], in segment_instance(img_path, confidence, rect_th, text_size, text_th)
     20   rgb_mask = get_coloured_mask(masks[i])
     21   img = cv2.addWeighted(img, 1, rgb_mask, 0.5, 0)
---> 22   cv2.rectangle(img, boxes[i][0], boxes[i][1],color=(0, 255, 0), thickness=rect_th)
     23   cv2.putText(img,pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX, text_size, (0,255,0),thickness=text_th)
     24 plt.figure(figsize=(20,30))

error: OpenCV(4.6.0) :-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
  • argument for rectangle() given by name ('color') and position (3)
  • argument for rectangle() given by name ('color') and position (3)
dilara
  • 19
  • 9

1 Answers1

0

cv::rectangle has two overloads, where the first takes two cv::Points and the second takes a cv::Rect. Both are tuples in Python, where the elements are integers.

Creating a tuple for pt argument like the following may work.

pt1 = tuple([int(j) for j in boxes[i][0]])
pt2 = tuple([int(j) for j in boxes[i][1]])
cv2.rectangle(img, pt1, pt2, color=(0, 255, 0), thickness=rect_th)

As for the second overload, note that cv::Rect consists of (x, y, width, height).


pred_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]['boxes'].detach().cpu().numpy())]

Given that you create boxes yourself, you can make the coordinates integer in the first place.

pred_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]['boxes'].detach().cpu().numpy().astype(int))]
Burak
  • 2,251
  • 1
  • 16
  • 33
  • is it possible for you to give any email adress to contact with you? @Burak – dilara Nov 03 '22 at 12:06
  • @dilara You can [join a chat](https://chat.stackoverflow.com/rooms/249294/how-can-i-solve-opencv4-6-0-1-error-5bad-argument-in-function-rectangl) when you have 20 reputation. I tried to add you explicitly but there occured an issue with the server. – Burak Nov 03 '22 at 14:44
  • Dear @Burak I cannot join to that. I would like to ask something about my whole code. Are there anything else to reach you? – dilara Nov 03 '22 at 14:53
  • @dilara If you improve a post by editing it, you get 2 reputation points. You are very close to be able to chat. – Burak Nov 03 '22 at 17:33
  • okey I will do it. I have a problem with this predicted boxes. They do not draw around the circle. boxes are tooo big. It prints wrongly. I just wanted to send you my whole code. @Burak – dilara Nov 07 '22 at 10:12
  • @dilara You can ask a different question about it. The issue may be related to how a rectangle is defined. It seems `tf` uses `ymin xmin ymax xmax` format. See [`draw_bounding_boxes`](https://www.tensorflow.org/api_docs/python/tf/image/draw_bounding_boxes) function, it may come in handy. – Burak Nov 07 '22 at 14:57
  • Dear Burak, I tried to edit and answer the questions. but they decreased my reputation to from 20 to 15. May be it would be better if you could send me a message for contact? : thingma.stone07@hotmail.com @Burak – dilara Nov 10 '22 at 19:27