栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python OpenCV人脸检测代码有时会引发'tuple'对象没有属性'shape'`

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python OpenCV人脸检测代码有时会引发'tuple'对象没有属性'shape'`

问题的原因是当没有匹配项时

detectMultiScale
返回一个空的元组
()
,而
numpy.ndarray
在有匹配项时返回一个空的元组。

>>> faces = classifier.detectMultiScale(cv2.imread('face.jpg'))>>> print(type(faces), faces)<class 'numpy.ndarray'> [[ 30 150  40  40]]>>> faces = classifier.detectMultiScale(cv2.imread('wall.jpg'))>>> print(type(faces), faces)<class 'tuple'> ()

您可能期望负结果将是形状为(0,4)的ndarray,但事实并非如此。

该行为及其背后的原因未在文档中进行说明,而是指示返回值应为“对象”。

OpenCV有很多这样的疣,而隐秘的错误消息也无济于事。处理它的一种方法是在代码中添加日志记录语句或断言,以检查所有内容是否都是您期望的类型。

探索库如何在诸如 ipython之类 的repl中工作是非常有用的。这在Rahul
KP的答案中使用

在这种情况下,您可以不使用来解决您的问题

shape
。Python有那些序列或集合,例如许多的数据类型
tuple
list
dict
。所有这些都实现了
len()
内置功能,您也可以使用循环它们
forx in y
。相反,
shape
它只是的一个属性
numpy.ndarray
,在任何内置的python数据类型中都找不到。

如果您将代码重写为use

len(faces)
而不是,则您的代码应该可以工作
faces.shape[0]
,因为前者可同时用于tuple和ndarray。

for img_fname in os.listdir('/home/work/images/caltech_face_dataset/'):    img_path = '/home/work/images/caltech_face_dataset/' + img_fname    im = imread(img_path)    gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)    faces = faceCascade.detectMultiScale(gray)  # use the grayscale image    print "Number of faces found in-> {} are {}".format(        img_fname, len(faces))  # len() works with both tuple and ndarray    num_faces_dict[img_fname] = len(faces)    # when faces is (), the following loop will never run, so it's safe.    for (x,y,w,h) in faces:         cv2.rectangle(im, (x,y), (x+w,y+h), (255,255,255), 3)    rect_img_path = '/home/work/face_detected/rect_' + img_fname    cv2.imwrite(rect_img_path,im)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/634425.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号