-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_recognition.py
29 lines (21 loc) · 937 Bytes
/
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
import cv2 as cv
import numpy as np
haar_cascade = cv.CascadeClassifier('haar_face.xml')
people = ['Tung_Duong', 'Ha_Anh_Tuan']
# features = np.load('features.npy', allow_pickle=True)
# labels = np.load('labels.npy')
face_recognizer = cv.face.LBPHFaceRecognizer_create()
face_recognizer.read('face_trained.yml')
img = cv.imread(r'105180399.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Picture', gray)
#Face detection
faces_rect = haar_cascade.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors=3)
for(x,y,h,w) in faces_rect:
faces_roi = gray[y:y+h, x:x+h]
label, confidence = face_recognizer.predict(faces_roi)
print(f'label = {people[label]} with confidence of {confidence}')
cv.putText(img, str(people[label]), (20,20), cv.FONT_HERSHEY_COMPLEX, 1.0, (0,255,0), thickness=2 )
cv.rectangle(img, (x,y), (x+w, y+h), (0,255,0), thickness=2)
cv.imshow('Recognition face', img)
cv.waitKey()