-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_recognition.py
67 lines (47 loc) · 1.94 KB
/
face_recognition.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
import sys
import os
import numpy
import time
from PIL import Image
import classification_utils
sys.path.append('/usr/local/lib/python2.7/site-packages')
import cv2
frontal_face_path = "./cascades/haarcascade_frontalface_default.xml"
frontal_face_cascade = cv2.CascadeClassifier(frontal_face_path)
recognizer = cv2.createLBPHFaceRecognizer()
#recognizer = cv2.face.createLBPHFaceRecognizer()
# takes a PIL image format
def classify_image(image):
image_pil = image.convert('L')
image = numpy.array(image_pil, 'uint8')
faces = frontal_face_cascade.detectMultiScale(image)
faces = classification_utils.group_faces(faces)
(x, y, w, h) = faces[0]
predicted_label_code, confidence = recognizer.predict(image[y: y+h, x: x+w])
print(predicted_label_code)
print("Classified:", classification_utils.lookup_label(predicted_label_code), "with confidence", confidence)
print("")
return classification_utils.lookup_label(predicted_label_code)
def test_images(path):
for image in filter( lambda f: not f.startswith('.'), os.listdir(path)):
print(image)
pil_image = Image.open(path + "/" + image)
classify_image(pil_image)
def load_training():
recognizer.load("recognizer.dat")
def classify_from_webcam():
while True:
video_capture = cv2.VideoCapture(0)
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = frontal_face_cascade.detectMultiScale(gray)
faces = classification_utils.group_faces(faces)
for (x, y, w, h) in faces:
cv2.rectangle(gray , (x, y), (x+w, y+h), (0, 255, 0), 2)
predicted_label_code, confidence = recognizer.predict(gray[y: y+h, x: x+w])
print("******************************", classification_utils.lookup_label(predicted_label_code))
cv2.imshow('ImageWindow', gray)
cv2.waitKey(1)
time.sleep(1)
if __name__ == '__main__':
load_training()