NCIT-Developer-Network
/
Classroom-Attendance-System-Utilizing-Viola-Jones-for-Face-Detection-and-LBPH-for-Face-Recognition
Public
forked from theonlyNischal/Classroom-Attendance-System-Utilizing-Viola-Jones-for-Face-Detection-and-LBPH-for-Face-Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaceRecognition.py
65 lines (43 loc) · 1.82 KB
/
faceRecognition.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
# Importing the required libraries
import cv2 as cv
import numpy as np
import json
from helper.preprocess import preprocess_image, face_detector
font = cv.FONT_HERSHEY_COMPLEX
with open('labels_to_name.json') as json_file:
labels_to_name = json.load(json_file)
# Load the model from our assets
model = cv.face.LBPHFaceRecognizer_create()
model.read("trainer/model.xml")
# Initializing the webcam to capture live video
video_capture = cv.VideoCapture(0)
while True:
_, img = video_capture.read()
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
face, coord = face_detector(gray_img)
x,y,w,h = coord
if face is not None:
face = preprocess_image(face)
# Pass the face to model
# "results" comprises of a tuple
# containing label and confidence value
results = model.predict(face)
if results[1] < 500:
confidence = int(100 * (1-(results[1]/300)))
matched_id = str(results[0])
student_name = labels_to_name[matched_id]["full_name"]
student_id = labels_to_name[matched_id]["student_id"]
display_string = "Name: {} ID: {}".format(student_name, student_id)
if confidence > 70:
cv.putText(img, display_string, (x+10, y-30), font, 1, (255, 120, 150), 2)
cv.imshow("Face Cropper", img)
else:
cv.putText(img, "Unknown", (x+10, y-30), font, 1, (255, 120, 150), 2)
cv.imshow("Face Cropper", img)
else:
cv.imshow("Face Cropper", img)
if (cv.waitKey(1) & 0xFF == ord("q")):
break
video_capture.release()
cv.destroyAllWindows()
# Our face recognition is finally working