2

I have a simple code as mentioned below:

import cv from opencv.cv import * from opencv.highgui import * img = cv.LoadImage("test.jpg") cap = cv.CreateCameraCapture(0) while cv.WaitKey(1) != 10: img = cv.QueryFrame(cap) cv.ShowImage("cam view", img) cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1)) 

But I faced to this error:

# AttributeError: 'module' object has no attribute 'LoadImage' 

when I change the code to below:

import cv #from opencv.cv import * #from opencv.highgui import * img = cv.LoadImage("test.jpg") cap = cv.CreateCameraCapture(0) while cv.WaitKey(1) != 10: img = cv.QueryFrame(cap) cv.ShowImage("cam view", img) cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1)) 

now the first error got solve and another error raise.

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

I need both of the modules but it seems that they have conflict to gether.

Now what I have to do?

4
  • Can you please specify which classes are contained in which module? Commented Jan 31, 2012 at 10:39
  • 1
    You aren't using any of the stuff you've imported in lines 2 and 3. And as far as I understand how python works, it is impossible for commenting out those lines to solve your AttributeError in the first case (It's either in cv module or it's not, and importing more stuff into the global namespace is not going to change that) Commented Jan 31, 2012 at 11:05
  • from the error message you can identify that the last line of code need to this module importing!!! Commented Jan 31, 2012 at 12:38
  • @Amin please provide a link to the opencv framework you are using, so that we can have a look at the package structure etc. Commented Jan 31, 2012 at 12:50

2 Answers 2

5

In OpenCV to load a haar classifier (in the python interface anyway) you just use cv.Load.

import cv cascade = cv.Load('haarcascade_frontalface_alt.xml') 

See the examples here.

Also, the samples that come with the OpenCV source are really good (in OpenCV-2.xx/samples/python).

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

Comments

1

How do you access the stuff you've imported?

# imports the cv module, all stuff contained in it and # the module itself is now accessible via: cv.classname, cv.functionname # where classname, functionname is the name of the class/function which # the cv module provides.. import cv # imports everything contained in the opencv.cv module # after this import it is available via it's classname, functionname, etc. # Attention: without prefix!! from opencv.cv import * # @see opencv.cv import from opencv.highgui import * 

@see python modules for more details about modules and imports in python.

If you can provide which classes are contained in which module I could add a specific solution for your problem.

2 Comments

I think this question is more related to how to import OpenCV libraries properly, than import in Python in general
python opencv has some library that are with different syntax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.