1

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??

  • Possible duplicate of [Python OpenCV face detection code sometimes raises \`'tuple' object has no attribute 'shape'\`](https://stackoverflow.com/questions/37761340/python-opencv-face-detection-code-sometimes-raises-tuple-object-has-no-attrib) – Jared Smith Feb 01 '19 at 13:53

1 Answers1

3

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

hyperTrashPanda
  • 838
  • 5
  • 18