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.