简体   繁体   中英

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

I have been writing a code about autoencoencoder for face recognition the part of code I used is as follows:

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

this is the error i get :

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

the error is in this line:

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

Any idea on how I can fix it??

Check out this link from 2016.

"The cause of the problem is that detectMultiScale returns an empty tuple () when there's no matches, but a numpy.ndarray when there are matches", so the AttributeError you're getting makes some sense.

You should add some validation code to catch this case, and check either if detectMultiScale has returned results, or what's the data type of your variable before using .shape[0] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM