Python OpenCV face detection code sometimes raises `'tuple' object has no attribute 'shape'`

前端 未结 4 1023
花落未央
花落未央 2020-12-21 21:54

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
c         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-21 22:45

    From your error understand that you are trying to read the shape. But shape 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)
     in ()
    ----> 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)

提交回复
热议问题