I am working on Odroid and running face detection using openCV python on it. But there is too much lag in the camera. I have tried a lot of things but couldn't remove the lag. Please suggest how can i remove the lag. I want to detect faces from at least 15 feet for that I need high resolution images, but high resolution images cause more lag. Currently I am having 2 second lag between frames. If there are suggestions please share.
import cv2, sys, numpy, os
import json
size = 3
fn_haar = 'haarcascade_frontalface_default.xml'
fn_haareye = 'haarcascade_eye.xml'
(im_width, im_height) = (112, 92)
haar_cascade = cv2.CascadeClassifier(fn_haar)
eye_cascade = cv2.CascadeClassifier(fn_haareye)
webcam = cv2.VideoCapture(0)
while True:
(rval, frame) = webcam.read()
frame=cv2.flip(frame,1,0)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size))
faces = haar_cascade.detectMultiScale(mini,scaleFactor=1.05, minNeighbors=3, minSize=(20, 20), flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
for i in range(len(faces)):
face_i = faces[i]
(x, y, w, h) = [v * size for v in face_i]
face = gray[y:y + h, x:x + w]
face_resize = cv2.resize(face, (im_width, im_height))
eyes = eye_cascade.detectMultiScale(face)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(face_resize ,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('OpenCV', frame)
key = cv2.waitKey(10)
if key == 27:
break
You can try following things(These area based on my experience)
1. Decreasing the resolution of the image or selecting the roi from the image.
2. Increasing the detectMultiscale Factor. You will have to tune it because increasing it will have adverse effect on its accuracy.
3. Setting nlevels parameter of HOG, By default it is set to 64 in my case reducing it to 8 had almost no effect on accuracy but improvement in speed by 25-30 %.