-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRecognize.py
36 lines (26 loc) · 1.03 KB
/
Recognize.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
import pandas as pd
import numpy as np
import cv2 as cv
id_names = pd.read_csv('id-names.csv')
id_names = id_names[['id', 'name']]
faceClassifier = cv.CascadeClassifier('Classifiers/haarface.xml')
lbph = cv.face.LBPHFaceRecognizer_create(threshold=500)
lbph.read('Classifiers/TrainedLBPH.yml')
camera = cv.VideoCapture(0)
while cv.waitKey(1) & 0xFF != ord('q'):
_, img = camera.read()
grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = faceClassifier.detectMultiScale(grey, scaleFactor=1.1, minNeighbors=4)
for x, y, w, h in faces:
faceRegion = grey[y:y + h, x:x + w]
faceRegion = cv.resize(faceRegion, (220, 220))
label, trust = lbph.predict(faceRegion)
try:
name = id_names[id_names['id'] == label]['name'].item()
cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv.putText(img, name, (x, y + h + 30), cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255))
except:
pass
cv.imshow('Recognize', img)
camera.release()
cv.destroyAllWindows()