-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaceRecognizer.py
88 lines (66 loc) · 2.62 KB
/
FaceRecognizer.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
import cv2
import numpy as np
from PIL import Image
import pickle
import sqlite3
# Khởi tạo bộ phát hiện khuôn mặt
faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
# Khởi tạo bộ nhận diện khuôn mặt
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('recognizer/trainner.yml')
id=0
#set text style
fontface = cv2.FONT_HERSHEY_SIMPLEX
fontscale = 1
fontcolor = (0,255,0)
fontcolor1 = (0,0,255)
# Hàm lấy thông tin người dùng qua ID
def getProfile(id):
conn=sqlite3.connect("FaceBaseNew.db")
cursor=conn.execute("SELECT * FROM People WHERE ID="+str(id))
profile=None
for row in cursor:
profile=row
conn.close()
return profile
# Khởi tạo camera
cam=cv2.VideoCapture(0);
while(True):
# Đọc ảnh từ camera
ret,img=cam.read();
# Lật ảnh cho đỡ bị ngược
img = cv2.flip(img, 1)
# Vẽ khung chữ nhật để định vị vùng người dùng đưa mặt vào
centerH = img.shape[0] // 2;
centerW = img.shape[1] // 2;
sizeboxW = 300;
sizeboxH = 400;
cv2.rectangle(img, (centerW - sizeboxW // 2, centerH - sizeboxH // 2),
(centerW + sizeboxW // 2, centerH + sizeboxH // 2), (255, 255, 255), 5)
# Chuyển ảnh về xám
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Phát hiện các khuôn mặt trong ảnh camera
faces=faceDetect.detectMultiScale(gray,1.3,5);
# Lặp qua các khuôn mặt nhận được để hiện thông tin
for(x,y,w,h) in faces:
# Vẽ hình chữ nhật quanh mặt
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
# Nhận diện khuôn mặt, trả ra 2 tham số id: mã nhân viên và dist (dộ sai khác)
id,dist=recognizer.predict(gray[y:y+h,x:x+w])
dist = round(dist,2)
profile=None
# Nếu độ sai khác < 25% thì lấy profile
if (dist<=100):
profile=getProfile(id)
# Hiển thị thông tin tên người hoặc Unknown nếu không tìm thấy
if(profile!=None):
cv2.putText(img, "Name: " + str(profile[1]), (x,y+h+30), fontface, fontscale, fontcolor ,2)
cv2.putText(img, "Dist: " + str(dist), (x,y+h+60), fontface, fontscale, fontcolor ,2)
else:
cv2.putText(img, "Name: Unknown", (x, y + h + 30), fontface, fontscale, fontcolor1, 2)
cv2.imshow('Face',img)
# Nếu nhấn q thì thoát
if cv2.waitKey(1)==ord('q'):
break;
cam.release()
cv2.destroyAllWindows()