-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathface_recognition.py
executable file
·64 lines (50 loc) · 1.77 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
import cv2
import numpy as np
import time
import os
from face_detection import *
from buzzer import alertor, stopAlertor
global thres
thres = 0
##############################################################################
def label_reading(filename):
s = filename.split('.jpg')[0]
return ''.join([i for i in s if not i.isdigit()])
names = [label_reading(name) for name in os.listdir('images')]
class Face_recog:
def __init__(self):
self.face_recognizer = cv2.face.LBPHFaceRecognizer_create()
self.path = 'images/'
def train(self):
images = os.listdir(self.path)
faces = []
labels = []
for image in images:
img = cv2.imread(self.path+image)
labels.append(names.index(label_reading(image)))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces.append(img)
#print(img.shape)
faces = np.array(faces)
self.face_recognizer.train(faces, np.array(labels))
def recognition(self,img):
face, rect = detect_faces(img)
global thres
for i in range(0, len(face)) :
label,confidence = self.face_recognizer.predict(face[i])
#print(confidence)
label_text = names[label]
if confidence > 65:
thres += 1
label_text = 'Unknown'
print(thres)
(x, y, w, h) = rect[i]
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0))
cv2.putText(img, label_text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
if label_text == 'Unknown':
if thres > 10:
alertor()
else:
thres = 0
stopAlertor()
return img