Most basic FaceDetection possible [closed]

asked 2014-01-28 14:36:47 -0500

updated 2014-01-28 14:50:07 -0500

berak gravatar image

I am trying to find the most basic way of detecting if a face is present in a video stream from a webcam. However I am new to OpenCV and am struggling to get it working in an embeded project I am building.

Due to lack of serious processing power, I don't want to display the image at all. No drawn boxes, no x,y of faces, just a count of the number of faces, output to a file.

I'm having trouble trying to figure out the minimum amount of code I can use to detect when a face is present and then simply return the number of faces back to the command line into a file.

It seems like I could take the facedetect.cpp line: 'void detectAndDraw' and change it to 'int detectAndDraw' and have it return that int but since I'm trying to write it in python instead of c++ I'm having trouble figuring out what I do and do not need from the example. This is what I have so far, based on some older code I found. where (path) will be /dev/video1

import cv2

def detect(path):
    img = cv2.imread(path)
    cascade = cv2.CascadeClassifier("/galileo/opencv/haarcascade_frontalface_alt.xml")
    rects = cascade.detectMultiScale(img, 1.3, 4,, (20,20))

    if len(rects) == 0:
        return [], img
    rects[:, 2:] += rects[:, :2]
    return rects

I think I messed this up though since I just want to return a count of the faces, not the contents of the rects object. Any helpful pointers or if there is already a minimalist example of face counting in python someplace I missed would be greatly appreciated.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-09-28 13:42:54.539767


  • don't worry at all about post-processing the rects, - that won't make a dent at all, detectMultiScale is the hog here.
  • if you can stand a bit loss of precision, - try a lbp-cascade ( same code, just different xml-file )
berak gravatar imageberak ( 2014-01-28 15:05:23 -0500 )edit