19

As I detect my tflite file, the problem happened.

The command I wrote.

python detect.py --weights ./checkpoints/yolov4-tiny-tf.tflite --size 416 --model yolov4 --image D:\yolov4\training\tensorflow-yolov4-tflite-master\data\rice.jpg --framework tflite --tiny true

And the wrong message:

cv2.rectangle(image, c1, c2, bbox_color, bbox_thick)
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

Here is the related code.(core/utils.py)

fontScale = 0.5
    score = out_scores[0][i]
    class_ind = int(out_classes[0][i])
    bbox_color = colors[class_ind]
    bbox_thick = int(0.6 * (image_h + image_w) / 600)
    c1, c2 = (coor[1], coor[0]), (coor[3], coor[2])
    cv2.rectangle(image, c1, c2, 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, c1, (np.float32(c3[0]), np.float32(c3[1])), bbox_color, -1) #filled

        cv2.putText(image, bbox_mess, (c1[0], np.float32(c1[1] - 2)), cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale, (0, 0, 0), bbox_thick // 2, lineType=cv2.LINE_AA)
return image

I have change it to

fontScale = 0.5
    score = out_scores[0][i]
    class_ind = int(out_classes[0][i])
    bbox_color = colors[class_ind]
    bbox_thick = int(0.6 * (image_h + image_w) / 600)
    c1, c2 = (int(coor[1]), int(coor[0])), (int(coor[3]), int(coor[2]))
    print(c1, c2, bbox_color, bbox_thick)
    cv2.rectangle(image, (int(coor[1]), int(coor[0])), (int(coor[3]), int(coor[2])), bbox_color, -1)

    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(coor[1]), int(coor[0])), (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

As it didn't got wrong, it still didn't show the image.

    [{'name': 'input_1', 'index': 0, 'shape': array([  1, 416, 416,   3]), 'shape_signature': array([ -1, 416, 416,   3]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]
[{'name': 'Identity', 'index': 232, 'shape': array([   1, 2535,    4]), 'shape_signature': array([ 1, -1,  4]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'Identity_1', 'index': 211, 'shape': array([   1, 2535,    2]), 'shape_signature': array([ 1, -1,  2]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

the last message

Did anyone have any thought to solve it? Thanks for your help!

Here is my files : https://github.com/piggychu0w0/food-image-detection

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
CYH
  • 345
  • 1
  • 2
  • 9

3 Answers3

32

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)
Red
  • 26,798
  • 7
  • 36
  • 58
  • I have tried it, but it still wrong. I add the wrong above. – CYH Jun 10 '21 at 13:06
  • cv2.rectangle(image, c1, (np.float32(c3[0]), np.float32(c3[1])), bbox_color, -1) #filled cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle' – CYH Jun 10 '21 at 13:10
  • It has the new mistake. – CYH Jun 10 '21 at 13:10
  • @CYH Try `cv2.rectangle(img, (int(c1[0]), int(c1[1])), (int(c2[0]), int(c2[1])), (255, 0, 0), -1)` – Red Jun 10 '21 at 13:11
  • Still having the same mistake. cv2.rectangle(image, c1, (np.float32(c3[0]), np.float32(c3[1])), bbox_color, -1) #filled – CYH Jun 10 '21 at 13:14
  • @CYH But you have `(np.float32(c3[0]), np.float32(c3[1]))`... – Red Jun 10 '21 at 13:15
  • So I need to change it to cv2.rectangle(image, int(c1), (int(c3[0]), int(c3[1])), bbox_color, -1) #filled. Like this? – CYH Jun 10 '21 at 13:21
  • It finally have not appear wrong. But it still can't show the image. This is the last it process. What does it means?? – CYH Jun 10 '21 at 13:30
  • I add the message at the bottom of the post. Do you have any experience about this?? @Ann Zen – CYH Jun 10 '21 at 13:45
  • If `c1` is a tuple, you can't do `int(c1)`; you'll need to unpack it. – Red Jun 10 '21 at 14:36
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
0
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)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Can you please check your indentation? – Suraj Rao Apr 22 '22 at 05:22
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Apr 22 '22 at 18:34
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. –  Apr 28 '22 at 01:57