【问题标题】:TypeError cv2.rect类型错误 cv2.rect
【发布时间】:2020-04-19 07:05:34
【问题描述】:

我正在尝试使用 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) 

【问题讨论】:

  • 哪个opencv版本?你试过cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 0, 255), 1)吗?
  • 版本是 4.2.0 。我尝试使用您指定的相同功能。
  • 我在通话中删除了 xminyminxmaxymax 周围的额外括号。

标签: python opencv object-detection-api openvino


【解决方案1】:

重现了您的错误。 此版本的 OpenCV (4.2.0) 中的错误消息非常令人困惑。

如果你在 OpenCV 4.3.0 中做同样的事情,你会得到一个更容易理解的错误,比如

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

这基本上表示您传递给cv2.rectangleimg 参数类型错误。 问题是你打电话后

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

img 不能被 OpenCV 解释为图像。 例如,可以通过以下方式解决该问题:
1.将变换前的img复制到另一个变量imgForDisplaying
2.使用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)

【讨论】:

  • 嘿,Artemy,我非常感谢你,它就像一个魅力。但是还有一个问题出现了,希望你能解决这个问题,
  • Traceback(最近一次调用最后):文件“check_infer.py”,第 70 行,在 cv2.imshow(rect,'face_detection') SystemError: returned NULL 不设置错误
  • 好吧,没关系,修好了!!!! Artemy 再次感谢你。你真的让我很开心!!!!
  • 太棒了! :) 我很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2016-10-28
  • 2018-04-24
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多