-
Notifications
You must be signed in to change notification settings - Fork 49
/
facerec.py
83 lines (64 loc) · 2.9 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# facerec.py
import cv2
import numpy
import os
size = 2
haar_cascade = cv2.CascadeClassifier(r'C:\Users\hp\Documents\GitHub\Face-Recognition-For-Criminal-Detection-GUi\face_cascade.xml')
# Part 1: Create fisherRecognizer
def train_model():
model = cv2.face.LBPHFaceRecognizer_create()
fn_dir = 'face_samples'
print('Training...')
(images, lables, names, id) = ([], [], {}, 0)
for (subdirs, dirs, files) in os.walk(fn_dir):
# Loop through each folder named after the subject in the photos
for subdir in dirs:
names[id] = subdir
subjectpath = os.path.join(fn_dir, subdir)
# Loop through each photo in the folder
for filename in os.listdir(subjectpath):
# Skip non-image formates
f_name, f_extension = os.path.splitext(filename)
if(f_extension.lower() not in ['.png','.jpg','.jpeg','.gif','.pgm']):
print("Skipping "+filename+", wrong file type")
continue
path = subjectpath + '/' + filename
lable = id
# Add to training data
images.append(cv2.imread(path, 0))
lables.append(int(lable))
id += 1
# Create a Numpy array from the two lists above
(images, lables) = [numpy.array(lis) for lis in [images, lables]]
# OpenCV trains a model from the images
model.train(images, lables)
return (model, names)
# Part 2: Use fisherRecognizer on camera stream
def detect_faces(gray_frame):
global size, haar_cascade
# Resize to speed up detection (optinal, change size above)
mini_frame = cv2.resize(gray_frame, (int(gray_frame.shape[1] / size), int(gray_frame.shape[0] / size)))
# Detect faces and loop through each one
faces = haar_cascade.detectMultiScale(mini_frame)
return faces
def recognize_face(model, frame, gray_frame, face_coords, names):
(img_width, img_height) = (112, 92)
recognized = []
recog_names = []
for i in range(len(face_coords)):
face_i = face_coords[i]
# Coordinates of face after scaling back by `size`
(x, y, w, h) = [v * size for v in face_i]
face = gray_frame[y:y + h, x:x + w]
face_resize = cv2.resize(face, (img_width, img_height))
# Try to recognize the face
(prediction, confidence) = model.predict(face_resize)
# print(prediction, confidence)
if (confidence<95 and names[prediction] not in recog_names):
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
recog_names.append(names[prediction])
recognized.append((names[prediction].capitalize(), confidence))
elif (confidence >= 95):
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
return (frame, recognized)
#train_model()