slow face detection on opencv and intel galileo gen2

554 views Asked by At

i want to program my intel galileo gen 2 so that it displays the number of faces in front of the webcam and simply print it on the shell (using opencv). My code is working but the problem is that the processing speed is really slow. It prints the number like once every 15 seconds. This way I am also unable to check if the number is correct or not. Is there any way or someone has done it ? Here''s the code..

import cv2
import sys
import time

cascPath = '/media/mmcblk0p1/haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascPath)

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    print len(faces)

    time.sleep(0.033)
1

There are 1 answers

0
George Profenza On

Although it's an Intel CPU, there aren't that many resources(400MHz CPU, 256MB RAM) on the Intel Galileo for advanced computer vision algorithms (such as face detection).

The first thing I notice is you're not setting the capture dimension. I don't know what the camera specifications are, but I'm guessing you're opening the camera at full resolution. I recommend opening the camera at a lower resolution, such as 320x240 or even 160x120 as there will be far less pixels to process.

HAAR cascades are a bit intensive as well (especially on a system like Intel Galileo Gen2). I recommend looking into Local Binary Patterns (LBP). These are already implemented in OpenCV and you can check out an LBP c++ sample here. It should be easy to adapt this to the Python API or find a Python API example. LBP cascades should be faster than HAAR cascades.

Although less standard, depending on your camera, you may have lower level access to it. If you do either retrieve the images in grayscale directly, or if the raw colour stream is in YUV format, retrieve only the Y channel. This should give you a minor boost as you're no longer converting colorspaces, but pursue this only if it's easy to control the camera (or you have the time and resources to go deeper just for a partial boost).

Although slower to prototype than with Python, you might also want to try using native c or c++ and check if there any compiler optimization flags that can take advantage of the CPU as much as possible.

Note: you can find a c++ face detection sample for Intel Galileo here