-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognize_face.py
More file actions
145 lines (106 loc) · 2.59 KB
/
Copy pathrecognize_face.py
File metadata and controls
145 lines (106 loc) · 2.59 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import cv2
from datetime import datetime
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("trainer.yml")
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades +
"haarcascade_frontalface_default.xml"
)
people = {
0: "Devi",
1: "Mama",
2: "Dada"
}
cap = cv2.VideoCapture(0)
alert_sent = False
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(
frame,
cv2.COLOR_BGR2GRAY
)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=5
)
for (x, y, w, h) in faces:
face = gray[y:y+h, x:x+w]
face = cv2.resize(
face,
(200, 200)
)
label, confidence = recognizer.predict(face)
print(
f"Label: {label} | Raw Confidence: {confidence}"
)
match_percent = max(
0,
min(
100,
round(100 - confidence, 1)
)
)
if confidence < 55:
name = people.get(
label,
"Known User"
)
color = (0, 255, 0)
alert_sent = False
else:
name = "INTRUDER ALERT"
color = (0, 0, 255)
if not alert_sent:
timestamp = datetime.now().strftime(
"%Y%m%d_%H%M%S"
)
filename = (
f"snapshots/intruder_{timestamp}.jpg"
)
cv2.imwrite(
filename,
frame
)
with open(
"intrusion_log.txt",
"a"
) as log:
log.write(
f"{datetime.now()} - Unknown Person Detected - {filename}\n"
)
print(
"ALERT: Unknown Person Detected!"
)
alert_sent = True
display_text = (
f"{name} {match_percent}%"
)
cv2.rectangle(
frame,
(x, y),
(x+w, y+h),
color,
2
)
cv2.putText(
frame,
display_text,
(x, y-10),
cv2.FONT_HERSHEY_SIMPLEX,
0.8,
color,
2
)
if len(faces) == 0:
alert_sent = False
cv2.imshow(
"SentinelAI",
frame
)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()