-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
164 lines (128 loc) · 4.38 KB
/
Main.py
File metadata and controls
164 lines (128 loc) · 4.38 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import cv2
import numpy as np
from pathlib import Path
from insightface.app import FaceAnalysis
from sklearn.metrics.pairwise import cosine_similarity
import argparse
import pickle
import queue
import threading
import pygame
BASE_DIR = Path(__file__).parent
DATA_DIR = BASE_DIR / "person_images"
DB_PATH = BASE_DIR / "face_db.pkl"
MATCH_THRESHOLD = 0.65
UNCERTAIN_THRESHOLD = 0.55
def init_face_model():
app = FaceAnalysis(
name="buffalo_l",
providers=["CPUExecutionProvider"]
)
app.prepare(ctx_id=0, det_size=(640, 640))
return app
def train():
app = init_face_model()
database = {}
for person_dir in DATA_DIR.iterdir():
if not person_dir.is_dir():
continue
embeddings = []
for img_path in person_dir.glob("*"):
img = cv2.imread(str(img_path))
if img is None:
continue
faces = app.get(img)
if not faces:
continue
emb = faces[0].embedding
embeddings.append(emb)
if embeddings:
database[person_dir.name] = np.vstack(embeddings)
with open(DB_PATH, "wb") as f:
pickle.dump(database, f)
print("Training complete.")
print("Stored identities:", list(database.keys()))
def classify(embedding, database):
best_name = None
best_score = -1
for name, embs in database.items():
sims = cosine_similarity([embedding], embs)[0]
score = sims.mean()
if score > best_score:
best_score = score
best_name = name
if best_score >= MATCH_THRESHOLD:
return best_name, best_score, "MATCH"
elif best_score >= UNCERTAIN_THRESHOLD:
return best_name, best_score, "UNCERTAIN"
else:
return "UNKNOWN", best_score, "REJECT"
speech_queue = queue.Queue()
def speech_worker():
import pythoncom
import win32com.client
import pygame
import time
pythoncom.CoInitialize()
speaker = win32com.client.Dispatch("SAPI.SpVoice")
while True:
text = speech_queue.get()
if text is None:
break
# Smooth fade-down (volume ducking)
for v in range(50, 5, -5):
pygame.mixer.music.set_volume(v / 100)
time.sleep(0.03)
speaker.Speak(text)
# Smooth fade-up (resume exactly where it left off)
for v in range(5, 50, 5):
pygame.mixer.music.set_volume(v / 100)
time.sleep(0.03)
def webcam():
if not DB_PATH.exists():
print("Run training first.")
return
with open(DB_PATH, "rb") as f:
database = pickle.load(f)
app = init_face_model()
cap = cv2.VideoCapture(0)
displayed_names = set()
while True:
ret, frame = cap.read()
if not ret:
break
faces = app.get(frame)
for face in faces:
box = face.bbox.astype(int)
emb = face.embedding
name, score, status = classify(emb, database)
color = (0,255,0) if status=="MATCH" else (0,255,255) if status=="UNCERTAIN" else (0,0,255)
label = f"Welcome {name} | {status} | {score:.2f}" if name != "UNKNOWN" else f"{name} | {status} | {score:.2f}"
display_name = name.replace("_", " ")
if (display_name not in displayed_names and display_name != "UNKNOWN"):
print(f"\nWelcome {display_name}\n")
speech_queue.put(f"Welcome {display_name}")
displayed_names.add(display_name)
cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), color, 2)
cv2.putText(frame, label, (box[0], box[1]-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
cv2.imshow("Face Recognition", frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()
def start_background_music():
pygame.mixer.init()
pygame.mixer.music.load("background.mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--mode", choices=["train", "webcam"], required=True)
args = parser.parse_args()
if args.mode == "train":
train()
else:
threading.Thread(target=start_background_music, daemon=True).start()
threading.Thread(target=speech_worker, daemon=True).start()
webcam()