2

I was trying to make a face detection program where it would use xml files to train classifiers and identify face, mouth, and eyes from a screenshot.

However when I tried to load the xml files, it gives me the error that cv2 has no 'Load' attribute. Since I had an attribute problem before with cv2 due to different versions and documentation (using 3.0.0-bet), I suspect that it is something as simple as a syntax error. However I am not really sure, can anyone tell me what is causing the problem and how do I fix it?

Error:

Traceback (most recent call last):
  File "/home/anthony/Documents/Programming/Python/Computer-Vision/Tests/nowayout.py", line 18, in <module>
    haarFace = cv2.Load('haarcascade_frontalface_default.xml')
AttributeError: 'module' object has no attribute 'Load'

Code:

# -*- coding: utf-8 -*-

from PIL import Image
from numpy import *
from pylab import *
import pyscreenshot as ImageGrab
import urllib
import cv2
#import cv

image=ImageGrab.grab()
ImageGrab.grab_to_file('image.png')

# input image
imcolor = cv2.imread('image.png') 

# loading the classifiers
haarFace = cv2.Load('haarcascade_frontalface_default.xml')
haarEyes = cv2.Load('haarcascade_eye.xml')
haarMouth= cv2.Load('haarcascade_mcs_mouth.xml')

# running the classifiers
storage = cv2.CreateMemStorage()
detectedFace = cv2.HaarDetectObjects(imcolor, haarFace, storage)
detectedEyes = cv2.HaarDetectObjects(imcolor, haarEyes, storage)
detectedMouth = cv2.HaarDetectObjects(imcolor, haarMouth, storage)

# draw a green rectangle where the face is detected
if detectedFace:
 for face in detectedFace:
  cv2.Rectangle(imcolor,(face[0][0],face[0][1]),
               (face[0][0]+face[0][2],face[0][1]+face[0][3]),
               cv2.RGB(155, 105, 25),2)

# draw a purple rectangle where the eye is detected
if detectedEyes:
 for face in detectedEyes:
  cv2.Rectangle(imcolor,(face[0][0],face[0][1]),
               (face[0][0]+face[0][2],face[0][1]+face[0][3]),
               cv2.RGB(155, 55, 200),2)
# draw a purple rectangle where the eye is detected
if detectedMouth:
 for face in detectedMouth:
  cv2.Rectangle(imcolor,(face[0][0],face[0][1]),
               (face[0][0]+face[0][2],face[0][1]+face[0][3]),
               cv2.RGB(255, 0, 0),2)


cv2.NamedWindow('Face Detection', cv.CV_WINDOW_AUTOSIZE)
cv2.ShowImage('Face Detection', imcolor) 
cv2.WaitKey()
5
  • all the functions you're trying to use, are from the deprecated cv api (which does no more exist in opencv3.0), not from cv2. Commented Jan 4, 2015 at 9:07
  • maybe look at the sample code Commented Jan 4, 2015 at 9:49
  • I copied them from a tutorial on Youtube, so that's why I suspect that this is a version problem (using 3.0 while Youtube tutorial uses 2.9) Commented Jan 4, 2015 at 15:47
  • "I copied them from a tutorial on Youtube," - lol. learn, not to do that. instead, rather look at opencv's excellent samples Commented Jan 4, 2015 at 15:48
  • It looks like the python bindings for OpenCV3 are automatically generated from the C++ - which is why a lot of (Python) function signatures broke between OpenCV 2 and OpenCV 3. They also deprecated OpenCV 1, so all the XML handling functions simply don't exist. FileStorage exists, but there's no way to actually write an object. You can do cv2.FileStorage('test', cv2.FileStorage_WRITE) and it will touch test, but I have no idea how to get any data into it. For loading you could try cv2.FileStorage('haarcascade_frontalface_default.xml', cv2.FileStorage_READ) Commented Feb 22, 2016 at 15:20

1 Answer 1

1

Try changing Load() to CascadeClassifier(), and changing all your cv2.YourMethods to cv2.cv.YourMethods and see if it helps.

Sign up to request clarification or add additional context in comments.

5 Comments

I added cv2.CadcadeClassifier() and it passed. Then I tried changing cv2 to cv2.cv but it ended up giving something like this: Traceback (most recent call last): line 22, in <module> storage = cv2.cv.CreateMemStorage() AttributeError: 'module' object has no attribute 'cv'
It appears that it rejects cv2.cv and only accepts cv2. Also, when I just use cv2, an error like this appears: line 22, in <module> storage = cv2.CreateMemStorage() AttributeError: 'module' object has no attribute 'CreateMemStorage'
@user3377126, interesting. You may try: print dir(cv2) and see if you can find the methods within? Also, are you sure you have your opencv installed properly?
OpenCV 3.0.0 beta is the one I am using (according to an opencv command I typed into my Python IDE) And I did not find the exact same function, but I found 'FileStorage_MEMORY' instead of 'CreateMemStorage'
@user3377126, have you got ipython? It could be much easier for you to use some built-in tools (double TAB) to list available methods. In such case, I believe the API changes of 3.0 is exceeding my knowledge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.