close

[Solved] Tflite detext error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Tflite detext error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Tflite detext error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’ Error Occurs?

Today I get the following error Tflite detext error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’ in python.

How To Solve Tflite detext error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’ Error ?

  1. How To Solve Tflite detext error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle' Error ?

    To Solve Tflite detext error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle' Error The problem is that you are passing tuples with floats into the function's parameters as the points. Here is the error reproduced:

  2. Tflite detext error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle'

    To Solve Tflite detext error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle' Error The problem is that you are passing tuples with floats into the function's parameters as the points. Here is the error reproduced:

Solution 1

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

Solution 2


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)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read