-2

I have recently used OpenCV to do face recognition with the raspi. I have done it using a haar cascade. It is not fast at updating the live video with the box around a face. It freezes on a certain frame which is about 10 seconds behind and then updates to another frozen image which is still behind. I was wondering how I would speed it up so it looks like live video with the boxes around the face.

Code:

import cv2
import numpy as np
cap = cv2.VideoCapture(0)

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

while True:

    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(frame, (x,y), (x+w,y+h), (255,0,0),2)



    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
Matthew Haywood
  • 80
  • 3
  • 13
  • You can skip previous frames & run detection on last frame only – Dmitrii Z. Dec 12 '17 at 12:47
  • and we guess the code? – api55 Dec 12 '17 at 13:50
  • 2
    You are probably doing something really wrong... haar cascades are pretty fast. However there's nothing we can do if you don't show your code. Please see how to do a [mcve] – Miki Dec 12 '17 at 17:34
  • Can you change your down vote if it comes to a conclusion? – Matthew Haywood Dec 12 '17 at 21:33
  • Actually nothing really wrong ;) (except missing checks if `cap` is open, or `ret` value). What's frame size and frame rate? You're probably working on big images (you could resize them to make detection faster) or with high frame rate (you can skip some frame) – Miki Dec 12 '17 at 23:21
  • Is there anyway of only detecting one face as I have a feeling that face_cascade.detectMultiScale is trying to detect loads of faces at once which would slow the speed down. Also how would I resize the Picam and how would I change the frame rate? – Matthew Haywood Dec 13 '17 at 12:16

1 Answers1

0

I used your code, but I didn't find the problem you said.Can you find a video instead of a camera for testing?

cv2.VideoCapture(0)

change to

cv2.VideoCapture('E:/testVideo.mp4')

If there's no problem in the video, maybe your camera is broken.