Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,18 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
return
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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()
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
def detect_face(image):
|
| 6 |
+
# Load Haar cascade from OpenCV
|
| 7 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 8 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 9 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 10 |
+
|
| 11 |
+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
|
| 12 |
+
for (x, y, w, h) in faces:
|
| 13 |
+
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
| 14 |
+
|
| 15 |
+
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 16 |
+
|
| 17 |
+
iface = gr.Interface(fn=detect_face, inputs=gr.Image(), outputs=gr.Image())
|
| 18 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|