Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +83 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
import insightface
|
| 4 |
+
import numpy as np
|
| 5 |
+
from collections import deque
|
| 6 |
+
|
| 7 |
+
# Initialize face detector and recognizer
|
| 8 |
+
model = insightface.app.FaceAnalysis(allowed_modules=['detection', 'recognition'])
|
| 9 |
+
model.prepare(ctx_id=0, det_size=(640, 640)) # Use ctx_id=-1 for CPU
|
| 10 |
+
|
| 11 |
+
def normalize(v):
|
| 12 |
+
return v / np.linalg.norm(v)
|
| 13 |
+
|
| 14 |
+
def cosine_similarity(a, b):
|
| 15 |
+
return np.dot(a, b)
|
| 16 |
+
|
| 17 |
+
# Load known faces
|
| 18 |
+
known_embs = []
|
| 19 |
+
names = []
|
| 20 |
+
for fname in os.listdir("images"):
|
| 21 |
+
if fname.lower().endswith(('.jpg', '.png')):
|
| 22 |
+
img = cv2.imread(os.path.join("images", fname))
|
| 23 |
+
faces = model.get(img)
|
| 24 |
+
if faces:
|
| 25 |
+
emb = normalize(faces[0].embedding)
|
| 26 |
+
known_embs.append(emb)
|
| 27 |
+
names.append(os.path.splitext(fname)[0])
|
| 28 |
+
print(f"Loaded {fname}")
|
| 29 |
+
else:
|
| 30 |
+
print(f"No face in {fname}")
|
| 31 |
+
|
| 32 |
+
# Smoothing buffers for each face
|
| 33 |
+
face_buffers = {} # key: face_id, value: deque of embeddings
|
| 34 |
+
|
| 35 |
+
# Start video
|
| 36 |
+
cap = cv2.VideoCapture(0)
|
| 37 |
+
face_id_counter = 0
|
| 38 |
+
|
| 39 |
+
while True:
|
| 40 |
+
ret, frame = cap.read()
|
| 41 |
+
if not ret:
|
| 42 |
+
break
|
| 43 |
+
|
| 44 |
+
faces = model.get(frame)
|
| 45 |
+
current_buffers = {}
|
| 46 |
+
|
| 47 |
+
for i, face in enumerate(faces):
|
| 48 |
+
x1, y1, x2, y2 = face.bbox.astype(int)
|
| 49 |
+
emb = normalize(face.embedding)
|
| 50 |
+
|
| 51 |
+
# Use a temporary ID for each face based on bbox location
|
| 52 |
+
face_id = f"{x1}-{y1}-{x2}-{y2}"
|
| 53 |
+
if face_id not in face_buffers:
|
| 54 |
+
face_buffers[face_id] = deque(maxlen=5)
|
| 55 |
+
|
| 56 |
+
face_buffers[face_id].append(emb)
|
| 57 |
+
current_buffers[face_id] = face_buffers[face_id] # mark as active this frame
|
| 58 |
+
|
| 59 |
+
# Smooth embedding
|
| 60 |
+
avg_emb = normalize(np.mean(face_buffers[face_id], axis=0))
|
| 61 |
+
|
| 62 |
+
# Find best match
|
| 63 |
+
sims = [cosine_similarity(avg_emb, known) for known in known_embs]
|
| 64 |
+
max_idx = np.argmax(sims)
|
| 65 |
+
name = "Unknown"
|
| 66 |
+
if sims[max_idx] > 0.5:
|
| 67 |
+
name = names[max_idx]
|
| 68 |
+
|
| 69 |
+
# Draw result
|
| 70 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 200, 0), 2)
|
| 71 |
+
cv2.putText(frame, name, (x1, y1 - 10),
|
| 72 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 200, 0), 2)
|
| 73 |
+
|
| 74 |
+
# Remove stale buffers
|
| 75 |
+
face_buffers = {fid: buf for fid, buf in face_buffers.items() if fid in current_buffers}
|
| 76 |
+
|
| 77 |
+
# Show frame
|
| 78 |
+
cv2.imshow("InsightFace Multi-Face Recognition", frame)
|
| 79 |
+
if cv2.waitKey(1) == 27: # ESC key
|
| 80 |
+
break
|
| 81 |
+
|
| 82 |
+
cap.release()
|
| 83 |
+
cv2.destroyAllWindows()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
face_recognition
|
| 3 |
+
opencv-python
|
| 4 |
+
numpy
|