Aditya Nikhil
Aditya Nikhil

Reputation: 351

TypeError cv2.rect

Am trying to implement an object detection model on an image using ssd_inception_v2_coco_2018_01_28 model.
Am trying to get the output with a bounding box on top.

1) ssd_inception_v2_coco_2018_01_28 object detection model. Successfully converted to IR.

2) Taken an input image and preprocessed it according to the input layer information of the model.

3) Successfully extracted the model outputs.

4) Added bounding boxes to the output image.

But am getting the following error, please help!



Source Code

#Preprocessing image

img = r'D:\SriSailam.jpg'
img = cv2.imread(img)
img = cv2.resize(img,(300,300))
img = img.transpose((2,0,1))
img = img.reshape(1,3,300,300)



exec_net = engine.load_network(net,'cpu')


#Asynchronous request

infer_request_handle = exec_net.start_async(request_id=0, inputs={'image_tensor': img}) 
infer_status = infer_request_handle.wait()
output = next(iter(net.outputs))


n,c,h,w = img.shape

result = infer_request_handle.outputs[output]  #output layer shape


'''
Draw bounding boxes onto the image.
'''
for box in result[0][0]: # Output shape is 1x1x100x7
        conf = box[2]  #box[2] = result[0][0][2] is a probability value
        print("probability value : ",conf)
        print("box[0] : ",box[0])
        print("box[1] : ",box[1])
        print("xmin : ", box[3])
        print("ymin : ", box[4])
        print("xmax : ", box[5])
        print("ymax : ", box[6])

        if conf >= 0:
            xmin = int(box[3] * w)  #box[3] - xmin
            ymin = int(box[4] * h) #box[4] - ymin
            xmax = int(box[5] * w)  #box[5] - xmax
            ymax = int(box[6] * h) #box[6] - ymax
            rect = cv2.rectangle(img, ((xmin), (ymin)), ((xmax), (ymax)), (0, 0, 255), 1)
cv2.imshow(rect,'face_detection')
cv2.waitKey(0)



Output

C:\Users\adity\Desktop\openvino>py check_infer.py                                                                                                                        
probability value :  0.6344592                                                                                                                                           
box[0] :  0.0                                                                                                                                                            
box[1] :  1.0                                                                                                                                                            
xmin :  0.1831626                                                                                                                                                        
ymin :  0.10697469                                                                                                                                                       
xmax :  0.86960804                                                                                                                                                       
ymax :  1.0                                                                                                                                                              
Traceback (most recent call last):                                                                                                                                       
  File "check_infer.py", line 68, in <module>                                                                                                                            
    rect = cv2.rectangle(img, ((xmin), (ymin)), ((xmax), (ymax)), (0, 0, 255), 1)                                                                                        
TypeError: an integer is required (got type tuple) 

Upvotes: 1

Views: 752

Answers (1)

Artemy Skrebkov
Artemy Skrebkov

Reputation: 345

Reproduced your error. The error message is pretty confusing in this version of OpenCV (4.2.0).

If you do the same in OpenCV 4.3.0 you will get a more intelligible error like

  File "<stdin>", line 1, in <module>
TypeError: Expected Ptr<cv::UMat> for argument 'img'

Which basically says that img argument you passed into cv2.rectangle has a wrong type. The problem is that after you call

img = img.transpose((2,0,1))
img = img.reshape(1,3,300,300)

img cannot be interpreted as an image by OpenCV. The issue can be solved, for example, this way:
1. Copy img before the transformation to another variable imgForDisplaying
2. Use imgForDisplaying to draw rectangle.

img = r'D:\SriSailam.jpg'
img = cv2.imread(img)
img = cv2.resize(img,(300,300))
imgToDisplay = img
img = img.transpose((2,0,1))
img = img.reshape(1,3,300,300)
...
rect = cv2.rectangle(imgToDisplay, (xmin, ymin), (xmax, ymax), (0, 0, 255), 1)

Upvotes: 1

Related Questions