Python OpenCV face detection code sometimes raises `'tuple' object has no attribute 'shape'`
Question: I am trying to build a face detection application in python using opencv.  
Please see below for my code snippets:

    
    
     # Loading the Haar Cascade Classifier
    cascadePath = "/home/work/haarcascade_frontalface_default.xml"
    faceCascade = cv2.CascadeClassifier(cascadePath)
    
    # Dictionary to store image name & number of face detected in it
    num_faces_dict = {}
    
    # Iterate over image directory. 
    # Read the image, convert it in grayscale, detect faces using HaarCascade Classifier
    # Draw a rectangle on the image    
    
    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(im)
        print "Number of faces found in-> ", img_fname, " are ", faces.shape[0]
        num_faces_dict[img_fname] = faces.shape[0]
        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)
    

This code works fine for most of the images but for some of them it throws an
error -

> AttributeError: 'tuple' object has no attribute 'shape' [![enter image
> description
> here](http://i.stack.imgur.com/DTjKC.png)](http://i.stack.imgur.com/DTjKC.png)

I get error in the line where I print the number of faces. Any help would be
appreciated.


Answer: From your error understand that you are trying to read the `shape`. But
[shape](http://docs.scipy.org/doc/numpy-1.10.1/reference/arrays.ndarray.html)
is the attribute of `numpy.ndarray`. You are trying to read the shape from the
result of face detection. But that will only return the position only. Look at
the types. Here `img` is an image and `faces` is the result of face detection.
I hope you got the problem.

**Updated with full code. For more clarification**

    
    
    In [1]: import cv2
    In [2]: cap = cv2.VideoCapture(0)
    In [3]: ret,img = cap.read()
    In [4]: cascadePath = "/home/bikz05/Desktop/SNA_work/opencv-2.4.9/data/haarcascades/haarcascade_frontalface_default.xml"
    In [5]: faceCascade = cv2.CascadeClassifier(cascadePath) 
    In [6]: faces = faceCascade.detectMultiScale(img)
    In [7]: type(img)
    Out[1]: numpy.ndarray
    In [8]: type(faces)
    Out[2]: tuple
    

Look at the diffrence.

    
    
    In [9]: img.shape
    Out[3]: (480, 640, 3)
    In [10]: faces.shape
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-40-392225a0e11a> in <module>()
    ----> 1 faces.shape
    AttributeError: 'tuple' object has no attribute 'shape'
    

If you want the number of faces. It's in the form of list of tuple. You can
find the number of faces using `len` like `len(faces)`

