Return _cv.cvHaarDetectObjects (* args)

I am trying to detect a face from a webcam using opencv python on ubuntu. I got this online code and tried to run this program, and I get a pointer to a NULL array, I think that it cannot capture video from a webcam, but with the same code (only for capturing the camera). I turned on the camera and he captured the video. Here is my code:

import cv from opencv import highgui HAAR_CASCADE_PATH = "/home/OpenCV-2.3.1/data/haarcascades/haarcascade_frontalface_default.xml" CAMERA_INDEX = 0 def detect_faces(image): faces = [] detected = cv.HaarDetectObjects(image, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (100,100)) if detected: for (x,y,w,h),n in detected: faces.append((x,y,w,h)) return faces if __name__ == "__main__": cv.NamedWindow("Video", cv.CV_WINDOW_AUTOSIZE) capture = cv.CaptureFromCAM(0) storage = cv.CreateMemStorage() cascade = cv.Load(HAAR_CASCADE_PATH) print cascade faces = [] i = 0 c = -1 while (c == -1): image = cv.QueryFrame(capture) # Only run the Detection algorithm every 5 frames to improve performance #if i%5==0: faces = detect_faces(image) #print image for (x,y,w,h) in faces: cv.Rectangle(image, (x,y), (x+w,y+h), 255) cv.ShowImage("w1", image) i += 1 

And the error I get is:

 Traceback (most recent call last): File "/home/OpenCV-2.3.1/webcam_try.py", line 38, in <module> faces = detect_faces(frame) File "/home/OpenCV-2.3.1/webcam_try.py", line 13, in detect_faces detected = cv.cvHaarDetectObjects(frame, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING,(100,100)) File "/usr/lib/pymodules/python2.7/opencv/cv.py", line 1626, in cvHaarDetectObjects return _cv.cvHaarDetectObjects(*args) NotImplementedError: Wrong number of arguments for overloaded function 'cvHaarDetectObjects'. Possible C/C++ prototypes are: cvHaarDetectObjects_Shadow(CvArr const *,CvHaarClassifierCascade *,CvMemStorage *,double,int,int,CvSize) cvHaarDetectObjects_Shadow(CvArr const *,CvHaarClassifierCascade *,CvMemStorage *,double,int,int) cvHaarDetectObjects_Shadow(CvArr const *,CvHaarClassifierCascade *,CvMemStorage *,double,int) cvHaarDetectObjects_Shadow(CvArr const *,CvHaarClassifierCascade *,CvMemStorage *,double) cvHaarDetectObjects_Shadow(CvArr const *,CvHaarClassifierCascade *,CvMemStorage *) 
+4
source share
2 answers

Your code worked fine for me (although I use OpenCV v2.4.3 instead of your version, 2.3.1). I started working from the same online code (posted here ) last week, and I eventually gave up using cv and switched to the new cv2 library.

So. I updated your code to use the new cv2 interface.

The Python cv2 for working with cascading Haar classifiers is much easier to use. Check out the documentation for cv2.CascadeClassifier.detectMultiScale() here . The new cv2 interface greatly simplifies your code. Here are the highlights:

  • You no longer need to worry about creating memory buffers.
  • The results returned from detectMultiScale are returned in a super useful form, eliminating the need for your old detect_faces() function
  • You need to specify only one parameter: the image itself. All other parameters are optional. I have included the options that you used in the updated code below, but feel free to remove them.

One tip: if your code is slow, one of the best things you can do is increase minSize. For my webcam, using (100,100) results in an ultra-low frame rate of around 0.2fps. Changing it to (300,300) raises it to 20 frames per second.

The code should work with your existing installation, since you are using 2.3.1, but if it is not, try upgrading to the latest version.

 import cv2 import cv2.cv as cv HAAR_CASCADE_PATH = "/home/OpenCV-2.3.1/data/haarcascades/haarcascade_frontalface_default.xml"; CAMERA_INDEX = 0; if __name__ == "__main__": # Open window, load webcam and load Haar cascade classifier cv2.namedWindow("Video", cv.CV_WINDOW_AUTOSIZE) capture = cv2.VideoCapture(CAMERA_INDEX); cascade = cv2.CascadeClassifier(HAAR_CASCADE_PATH); i = 0; while True: # Grab frame from webcam retVal, image = capture.read(); # note: ignore retVal # Only run the Detection algorithm every 5 frames to improve performance #if i%5==0: faces = cascade.detectMultiScale(image, scaleFactor=1.2, minNeighbors=2, minSize=(100,100), flags=cv.CV_HAAR_DO_CANNY_PRUNING); # Draw rectangles on image, and then show it for (x,y,w,h) in faces: cv2.rectangle(image, (x,y), (x+w,y+h), 255) cv2.imshow("Video", image) i += 1; 
+5
source

I changed Brandon's code a bit after accessing http://goo.gl/UziMVU . This is working code for me.

 import cv2 import cv2.cv as cv HAAR_CASCADE_PATH = "/usr/local/Cellar/opencv/2.4.6.1/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml" cv2.namedWindow("preview") vc = cv2.VideoCapture(0); cascade = cv2.CascadeClassifier(HAAR_CASCADE_PATH); retVal, frame = vc.read(); # note: ignore retVal while True: if frame is not None: faces = cascade.detectMultiScale(frame, scaleFactor=1.2, minNeighbors=2, flags=cv.CV_HAAR_DO_CANNY_PRUNING, minSize=(100,100)); for (x,y,w,h) in faces: cv2.rectangle(frame, (x,y), (x+w,y+h), 255) cv2.imshow("preview", frame) rval, frame = vc.read() if cv2.waitKey(1) & 0xFF == ord('q'): break 
+2
source

All Articles