-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestmodel.py
More file actions
50 lines (35 loc) · 1.37 KB
/
testmodel.py
File metadata and controls
50 lines (35 loc) · 1.37 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
import cv2
video = cv2.VideoCapture(0)
facedetect = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
if not video.isOpened():
print("Error: Could not open camera.")
exit()
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("Trainer.yml")
name_list = ["", "Unique"]
while True:
ret, frame = video.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = facedetect.detectMultiScale(gray, 1.3, 5)
if not ret:
print("Failed to capture frame")
break
for (x, y, w, h) in faces:
serial, conf = recognizer.predict(gray[y:y+h, x:x+w])
if 0 <= serial < len(name_list):
name = name_list[serial]
else:
name = "Unknown"
if conf < 50:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 1) # Green frame
cv2.putText(frame, name, (x, y-40), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) # Green text
else:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 1) # Red frame
cv2.putText(frame, "Unknown", (x, y-40), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2) # Red text
cv2.imshow("UNIK RECOGNIZER", frame)
k = cv2.waitKey(1)
if k == ord('e'):
break
video.release()
cv2.destroyAllWindows()
print("Dataset Collection Done.............")