问题的原因是当没有匹配项时
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)


