-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detect.py
95 lines (68 loc) · 3.28 KB
/
face_detect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#################################################################################
# #
# file: frame_jump.py #
# #
# authors: Tiantian Feng #
# Original Author: Jingyao Ren - jr386 #
# #
# date: May 20th 2017 #
# #
# brief: Face tracking using OpenCV default API #
# #
#################################################################################
### Imports ###################################################################
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import os
import pygame
### Setup #####################################################################
os.putenv( 'SDL_FBDEV', '/dev/fb1' )
# Setup the camera
camera = PiCamera()
camera.resolution = ( 320, 240 )
camera.framerate = 30
rawCapture = PiRGBArray( camera, size=( 320, 240 ) )
fcounter = 0
facefind = 0
# Load a cascade file for detecting faces
face_cascade = cv2.CascadeClassifier( '/opt/opencv/data/lbpcascades/lbpcascade_frontalface.xml' )
t_start = time.time()
fps = 0
### Main ######################################################################
# Capture frames from the camera
for frame in camera.capture_continuous( rawCapture, format="bgr", use_video_port=True ):
image = frame.array
# Run the face detection algorithm every four frames
if fcounter == 3:
fcounter = 0
# Look for faces in the image using the loaded cascade file
gray = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY )
faces = face_cascade.detectMultiScale( gray )
print "Found " + str( len( faces ) ) + " face(s)"
if str( len( faces ) ) != 0:
facefind = 1
facess = faces
else:
facefind = 0
# Draw a rectangle around every face
for ( x, y, w, h ) in faces:
cv2.rectangle( image, ( x, y ), ( x + w, y + h ), ( 200, 255, 0 ), 2 )
cv2.putText( image, "Face No." + str( len( facess ) ), ( x, y ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
facess = faces
else:
if facefind == 1 and str( len( facess ) ) != 0:
# Continue to draw the rectangle around every face
for ( x, y, w, h ) in facess:
cv2.rectangle( image, ( x, y ), ( x + w, y + h ), ( 200, 255, 0 ), 2 )
cv2.putText( image, "Face No." + str( len( facess ) ), ( x, y ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
fcounter += 1
# Calculate and show the FPS
fps = fps + 1
sfps = fps / ( time.time() - t_start )
cv2.putText( image, "FPS : " + str( int( sfps ) ), ( 10, 10 ), cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( 0, 0, 255 ), 2 )
cv2.imshow( "Frame", image )
cv2.waitKey( 1 )
# Clear the stream in preparation for the next frame
rawCapture.truncate( 0 )