Skip to content

Instantly share code, notes, and snippets.

@jonaslejon
Created February 13, 2013 16:18
Show Gist options
  • Save jonaslejon/4945730 to your computer and use it in GitHub Desktop.
Save jonaslejon/4945730 to your computer and use it in GitHub Desktop.
Face recognition in Raspberry Pi with OpenCV and sound with espeak
#!env python
import cv,pprint,os
from datetime import datetime
HAAR_CASCADE_PATH = "/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml"
CAMERA_INDEX = 0
def detect_faces(image, last):
faces = []
now = datetime.now().strftime("%k:%M:%S")
detected = cv.HaarDetectObjects(image, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (100,100))
if detected:
print now,"detected",last
if last < 10:
last = 0
else:
print now, "came"
t = datetime.now().strftime("%k %M")
h = datetime.now().strftime("%k")
command = "espeak -vsv -a 200 -p 30 -g 12 'Hejsan. Tiden er nu %s' 2>/dev/null > /dev/null" % t
os.system(command)
last = 0
else:
last += 1
return last
if __name__ == "__main__":
capture = cv.CaptureFromCAM(CAMERA_INDEX)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT, 480);
storage = cv.CreateMemStorage()
cascade = cv.Load(HAAR_CASCADE_PATH)
faces = []
last = 0
i = 0
while True:
image = cv.QueryFrame(capture)
# Only run the Detection algorithm every 5 frames to improve performance
last = detect_faces(image, last)
k=cv.WaitKey(20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment