0

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
Optimus 1072
  • 309
  • 4
  • 18
Irum Zahra Awan
  • 276
  • 1
  • 5
  • 16
  • increase the scaleFactor in detectMultiScale function. If you have option then try to choose ROI so as to minimise the region in which you have to do face detection – Optimus 1072 Dec 26 '16 at 09:03
  • @Optimus1072 if i increase the scale factor will it be able to detect faces from 15 feet ? – Irum Zahra Awan Dec 26 '16 at 09:14
  • with scaleFactor = 1.2 and image resolution of 360 * 288, I am able to and on my machine(I5) it is taking 15ms per frame. – Optimus 1072 Dec 26 '16 at 09:25

1 Answers1

0

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 %.

Optimus 1072
  • 309
  • 4
  • 18
  • i need to detect faces in real time which is why i am running detection on every 3rd frame i get. by decreasing the resolution of camera i can not detect from larger distance which i need to. Secondly am not using hog i am using haar cascades.... is there anything i am not understanding... i am actually new to open cv so plese help if i didnt get something correct. – Irum Zahra Awan Dec 27 '16 at 08:53