-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacerec.py
59 lines (51 loc) · 1.79 KB
/
facerec.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
import numpy as np
from picamera.array import PiRGBArray
from picamera import PiCamera
import cv2
import time
from PIL import Image
#from bob.ip.base.Flandmark import Flandmark
# Initializes the Camera
camera = PiCamera()
camera.resolution = (1296,736) # Reduces the resolution to increase performance
rawCapture = PiRGBArray(camera, size=(1296,736))
# Need to allow the camera to initilize
time.sleep(0.1)
# Returns True if a rectangle was made on the image.
def face_detected(image):
return True if image is not None else False
def detected_person():
filters = ['eye.xml', 'full_frontal.xml', 'profile.xml', 'profile_flipped.xml', 'eye_rotated.xml', 'eye_rotated2.xml', 'eye_rotated3.xml', 'eye_rotated4.xml']
for file in filters:
# Rotates the camera to account for a variety of postures.
if file == 'profile_flipped.xml':
camera.hflip = True
if file == 'eye_rotated.xml':
camera.rotation = 90
if file == 'eye_rotated2.xml':
camera.rotation = 270
if file == 'eye_rotated3.xml':
camera.rotation = 45
if file == 'eye_rotated4.xml':
camera.rotation = 315
camera.capture('image.jpg')
pil_image = Image.open('image.jpg').convert('RGB')
open_cv_image = np.array(pil_image)
open_cv_image = open_cv_image[:, :, ::-1].copy()
person = False
# Define the Facial Recognition
gray = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2GRAY)
#print file
face_cascade = cv2.CascadeClassifier(file)
faces = face_cascade.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in faces:
r = cv2.rectangle(open_cv_image,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = open_cv_image[y:y+h, x:x+w]
person = face_detected(r)
cv2.imwrite('result.jpg', r)
if person == True:
return person
camera.rotation = 0
camera.hflip = False
return False