TypeError cv2.rect

问题描述 投票:1回答:1

正在尝试使用ssd_inception_v2_coco_2018_01_28在图像上实现对象检测模型。我试图获取顶部带有边界框的输出。

1)ssd_inception_v2_coco_2018_01_28对象检测模型。已成功转换为IR。

2)拍摄输入图像并根据模型的输入层信息进行预处理。

3)成功提取模型输出。

4)将边界框添加到输出图像。

但是出现以下错误,请帮忙!

源代码

#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)

输出

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) 
python opencv object-detection-api openvino
1个回答
1
投票

复制了您的错误。在此版本的OpenCV(4.2.0)中,错误消息非常令人困惑。

如果您在OpenCV 4.3.0中执行相同的操作,则会得到更清晰的错误,例如

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

基本上说您传递给imgcv2.rectangle参数的类型错误。问题是您致电

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

img不能被OpenCV解释为图像。例如,可以通过以下方式解决此问题:1.转换前将img复制到另一个变量imgForDisplaying2.使用imgForDisplaying绘制矩形。

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)
© www.soinside.com 2019 - 2024. All rights reserved.