错误:“元组”对象没有属性“形状”

时间:2019-02-01 13:46:26

标签: python keras deep-learning face-recognition

我一直在编写有关用于面部识别的autoencoencoder的代码,我使用的部分代码如下:

face_cascade = cv2.CascadeClassifier('C:/Users/PC/PycharmProjects/haarcascade_frontalface_default.xml')
print(face_cascade)
img = cv2.imread('C:/Users/PC/PycharmProjects/exmpforbike6/training_images/JenniferGroup.jpg')
print(img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print("voici",gray)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    roi_gray = gray[y:y + h, x:x + w]
    roi_color = img[y:y + h, x:x + w]

a = []
for i in range(0, faces.shape[0]):
    a.append(gray[faces[i][1]:faces[i][1] + faces[i][3], faces[i][0]:faces[i][0] + faces[i][2]])

这是我得到的错误:

AttributeError: 'tuple' object has no attribute 'shape'

此行中的错误:

for i in range(0, faces.shape[0]):
    a.append(gray[faces[i][1]:faces[i][1] + faces[i][3], faces[i][0]:faces[i][0] + faces[i][2]])

关于如何解决它的任何想法?

1 个答案:

答案 0 :(得分:3)

查看2016年的this链接。

“问题的原因是,detectMultiScale在没有匹配项时返回一个空的元组(),但在存在匹配项时返回一个numpy.ndarray”,因此您得到的AttributeError有道理。

您应该添加一些验证码来捕获这种情况,并在使用detectMultiScale之前检查.shape[0]是否返回了结果,或者变量的数据类型是什么。

相关问题