-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathface_recognition.py
129 lines (91 loc) · 4.18 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python3
"""
ROBOVISION
______________
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Project Author/Architect: Navjot Singh <[email protected]>
"""
#
# face detection from an image using Haar cascade classifier
#
import numpy as np
import cv2 as cv
import pickle
from logger import get_logger
log = get_logger()
class FaceRecognition:
def __init__(self, face_cascade_xml, eye_cascade_xml):
# train/initialize face classifier
face_cascade = cv.CascadeClassifier(face_cascade_xml)
# train/initialize eye classifier
eye_cascade = cv.CascadeClassifier(eye_cascade_xml)
# creating a face recognier with pretrained data
log.info("creating recognier from pre-trained data")
recognizer = cv.face.LBPHFaceRecognizer_create()
recognizer.read("dataset/face_trainer.yml")
# reading data from webcam
# for internal webcam on laptop use 0
# for external webcam on laptop/PC use 1
cap = cv.VideoCapture(0)
labels = {}
font = cv.FONT_HERSHEY_SIMPLEX
# load trained labels
with open("dataset/face_trainer_labels.pickle", 'rb') as f:
org_labels = pickle.load(f)
labels = {v:k for k, v in org_labels.items()}
log.info("capturing video data")
while True:
ret, img = cap.read()
# HAAR cascade will need grayscaled image to detect faces and eyes
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# lets find faces in the image and get their positions
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
# drawing rect for face
cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
# define roi for eyes detection,ideally, we should detect
# eyes within the rectangular bounds of a face
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
# identify the face with recognizer
index, conf = recognizer.predict(roi_gray)
if conf > 75 and conf <= 95:
name = labels[index]
# Hurray, we detected a face !!!
logger.info(
"Identified face: Name: %s, index: %d, confidence level: %d" % (name, index, conf))
cv.putText(
img, name, (x, y), font, 1, (255, 255, 255), 1, cv.LINE_AA)
# drawing rects for eyes
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv.putText(
img, 'Press Esc to quit', (10, 450), font, 1, (255, 255, 255), 1, cv.LINE_AA)
# showing image
cv.imshow('Haar Face Detection', img)
# wait for key for 10 mscs, or continue
k = cv.waitKey(10) & 0xff
# if 'esc' pressed, terminate
if k == 27:
break
cap.release()
cv.destroyAllWindows()
if __name__ == "__main__":
# lets use pretrained cascade classifiers of opencv
# for face and eyes detection
# path to Haar face classfier's xml file
face_cascade_xml = './cascades/haarcascades_cuda/haarcascade_frontalface_default.xml'
# path to Haar eye classfier's xml file
eye_cascade_xml = './cascades/haarcascades_cuda/haarcascade_eye.xml'
# run the eye detector with given classfiers
fr = FaceRecognition(face_cascade_xml, eye_cascade_xml)