3

I have just installed opencv and am following this tutorial to detect faces while displaying a stream. The frame rate is horrible - around 1.5 fps. Why?

Code:

from time import sleep
from picamera import PiCamera
from picamera.array import PiRGBArray
import cv2
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 34
rawCapture = PiRGBArray(camera, size = (640, 480))

window = cv2.namedWindow("Faces")
face_cascade = cv2.CascadeClassifier("/home/pi/opencv-3.1.0/data/lbpcascades/lbpcascades_frontalface.xml")

sleep(0.1)

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    image = frame.array
    grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(grey, 1.1, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
    cv2.imshow("Faces", image)
    key = cv2.waitKey(1)

    rawCapture.truncate(0)

    if key == 27:
        camera.close()
        cv2.destroyAllWindows()
        break        

I have updated the code to the faster algorithm suggested in an answer.

video of framerate

4
  • 1
    sleep(0.1) ?? Always my first bet for bad performing loop problems but that is not looped is it? Try and comment out areas of code until you get more desirable FPS.. that way you can then try and optimize where the bottleneck is.. if possible at all. But at least you will pin point the problem. Technically with just the preview code you should get a smooth 25FPS (or 30FPS if 720p) - Must be post processing slowing it down?
    – Piotr Kula
    Commented Jan 9, 2017 at 14:07
  • @ppumkin Thankyou. I will try it but I believe that it is the face recognition that is causing the delay. Without that section I get a good frame rate just the stream is behind. Commented Jan 10, 2017 at 0:20
  • Looking at video .. I suppose it is not really that bad is it? As long as it can detect a face / faces every 1 second you are sorted? If you using that faster algorithm then stick with it. Not sure why you would want more FPS? I would have thought you wanted the face recognition to work properly instead :D
    – Piotr Kula
    Commented Jan 10, 2017 at 7:48
  • 1
    Yeah I'm happy enough with it as it is. Thanks for ur help. :D Commented Jan 10, 2017 at 7:54

1 Answer 1

2

The performance of the RPi is very bad for this calculations. The detection with haar-features slows down by the resolution of the image. It is recommended to shrink the image before searching for features.

For private research I experimented with "Local Binary Patterns Histograms". These algorithm is quite faster. Look here for xml files: https://github.com/opencv/opencv/tree/master/data/lbpcascades

Also, you can build OpenCV with OpenGL support on the RPi. But I am not sure which version is currently in the raspbian repository. Maybe it is not neccessary anymore. More: http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/

2
  • Thanks for your answer. I already followed the tutorial to install opencv. I will try your alogrithm Commented Jan 10, 2017 at 0:22
  • It makes it a lot better but still slow. I have posted a video in my question. Commented Jan 10, 2017 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.