-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_recognition_on_video.py
60 lines (45 loc) · 1.56 KB
/
face_recognition_on_video.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
import numpy as np
import cv2
haar_cascade = cv2.CascadeClassifier("haar_face.xml")
webcam = cv2.VideoCapture(0)
people = ["me", "not_me"]
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.read("face_trained.yml")
# img = cv.imread(r"faces/test.jpg")
while True:
check, frame = webcam.read()
cv2.imshow("cam", frame)
key = cv2.waitKey(100)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow("Person", gray)
faces_rect = haar_cascade.detectMultiScale(gray, 1.1, 4)
for x, y, w, h in faces_rect:
faces_roi = gray[y : y + h, x : x + w]
label, confidence = face_recognizer.predict(faces_roi)
print(f"Label = {people[label]} with a confidence of {confidence}")
if confidence > 20:
cv2.putText(
frame,
str(people[label] + " " + str(confidence)),
(20, 20),
cv2.FONT_HERSHEY_COMPLEX,
1.0,
(0, 255, 0),
thickness=2,
)
else:
cv2.putText(
frame,
str("none" + " " + str(confidence)),
(20, 20),
cv2.FONT_HERSHEY_COMPLEX,
1.0,
(0, 255, 0),
thickness=2,
)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), thickness=2)
cv2.imshow("Detected Face", frame)
if key == ord("q"):
webcam.release()
cv2.destroyAllWindows()
break