Attribute error while using opencv for face recognition

38,486

Solution 1

The latest openCV no longer allows importing the legacy cv module. Furthermore the naming convention of the constants generally does away with the leading "CV_..." and several/many of the names have been altered somewhat. I think you are running into both problems.

Specifically, the error you are reporting is in regards to this expression in your code: cv2.cv.CV_HAAR_SCALE_IMAGE. This expression is trying to find the named constant CV_HAAR_SCALE_IMAGE within the cv submodule of the cv2 package you imported. But alas, there is no cv2.cv anymore.

In openCV 3, I believe this constant is now referenced as follows: cv2.CASCADE_SCALE_IMAGE

Also, you may find this link useful. It is to the facedetect.py sample script found in the OpenCV source code. You can see the usage of the new constant name in this example, and you may also inspect it for other changes from older sources/tutorials.

Solution 2

This code is working fine for me i'm using the opencv3 lib, please try it

import cv2
import sys
 # Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags=cv2.CASCADE_SCALE_IMAGE
)

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("Faces found", image)
cv2.waitKey(0)
Share:
38,486
RatstabOfficial
Author by

RatstabOfficial

I'm a music producer, and coding stuff is a hobby of mine. Check my music out at: www.youtube.com/channel/UCbQ2CHJlOX2oHJkFedIVJag

Updated on July 09, 2022

Comments

  • RatstabOfficial
    RatstabOfficial almost 2 years

    I am teaching myself how to use openCV by writing a simple face recognition program I found on youtube. I have installed opencv version 2 as well as numpy 1.8.0. I am using python 2.7.

    I copyed this code exactly how it was done in the video and article links below, yet I keep getting errors.

    AttributeError: 'module' object has no attribute 'cv'

    What am I doing wrong? Here is the code I'm using.

    import cv2
    import sys
    
    # Get user supplied values
    imagePath = sys.argv[1]
    cascPath = sys.argv[2]
    
    # Create the haar cascade
    faceCascade = cv2.CascadeClassifier(cascPath)
    
    # Read the image
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Detect faces in the image
    faces = (faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
    )
    
    print "Found {0} faces!".format(len(faces))
    
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
    
    cv2.imshow("Faces found", image)
    cv2.waitKey(0)
    

    https://www.youtube.com/watch?v=IiMIKKOfjqE

    https://realpython.com/blog/python/face-recognition-with-python/

  • Stuti Verma
    Stuti Verma almost 6 years
    This is the answer of @svohara.
  • ah bon
    ah bon over 5 years
    Sorry, I got error as follows: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\objdetect\src\casca‌​dedetect.cpp:1698: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'.
  • Timus
    Timus over 3 years
    Welcome to SO! When you are about to answer an old question (this one is almost 5 years old) that already has an accepted answer (that is the case here) please ask yourself: Do I really have a substantial improvement to offer? If not, consider refraining from answering.