Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code worked fine for me (although I'm running OpenCV v2.4.3 instead of your version, 2.3.1). I started working from the same online code (posted <a href="http://www.calebmadrigal.com/facial-detection-opencv-python/" rel="nofollow">here</a>) last week, and I eventually gave up on using <code>cv</code> and switched to the new <code>cv2</code> library. </p> <p>So. I've updated your code so that it uses the new <code>cv2</code> interface. </p> <p>The <code>cv2</code> Python interface for running Haar Cascade Classifiers is much easier to use. Check out the documentation for <code>cv2.CascadeClassifier.detectMultiScale()</code> <a href="http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html#cascadeclassifier-detectmultiscale" rel="nofollow">here</a>. The new <code>cv2</code> interface simplifies your code significantly. Here are the highlights:</p> <ol> <li>You no longer have to worry about creating memory buffers.</li> <li>The results that are returned from <code>detectMultiScale</code> come back in a super useful form, eliminating the need for your old code's <code>detect_faces()</code> function!</li> <li>You only need to provide one parameter: the image itself. All other parameters are optional. I've included the parameters you were using in the revised code below, but feel free to remove them.</li> </ol> <p>One piece of advice: if your code is running slowly, one of the best things you can do is increase the minSize. For my webcam, using (100,100) causes a super-slow frame rate of about 0.2fps. Changing it to (300,300) boosts it to the respectable 20fps.</p> <p>The code should work on your existing installation since you're running 2.3.1, but if it doesn't then try upgrading to the latest version.</p> <pre><code>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; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload