Reputation: 191
I currently have some code which captures a still image from the Pi Camera Module and then identifies faces using the haarcascade xml file provided with OpenCV for Python. The code that I am using is the code shown towards the end of this blog post: http://rpihome.blogspot.co.uk/2015/03/face-detection-with-raspberry-pi.html, however it is slightly modified (fully working).
The only problem is that it currently only recognises faces on still images. Is there any way to make it so that I can continuously stream from the Pi Camera directly to OpenCV and then process the faces and display boxes around faces live in a window instead of saving a single frame to a file? I have tried several different tutorials online, but they all seem to not work for me.
Upvotes: 3
Views: 11403
Reputation: 7036
Haven't tried it, but this should work.
from picamera.array import PiRGBArray
from picamera import PiCamera
import cv2
import time
camera = PiCamera()
camera.resolution = (320, 240)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(320, 240))
display_window = cv2.namedWindow("Faces")
face_cascade = cv2.CascadeClassifier('path_to_my_face_cascade.xml')
time.sleep(1)
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image = frame.array
#FACE DETECTION STUFF
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
#DISPLAY TO WINDOW
cv2.imshow("Faces", image)
key = cv2.waitKey(1)
rawCapture.truncate(0)
if key == 27:
camera.close()
cv2.destroyAllWindows()
break
Take a look at the documentation for picamera here.
Upvotes: 3