Upload 25 files
Browse files- .gitattributes +5 -0
- app.py +76 -0
- images/Dr. Marwa Solayman.jpg +0 -0
- images/Dr. Mohamed Ghoniemy.jpg +0 -0
- images/Dr.Ahmed-Farouk.jpg +3 -0
- images/Dr.Khayri.png +0 -0
- images/EYAD1.jpg +3 -0
- images/Eng. Abdelrahman Ezzeldin.jpg +0 -0
- images/Eng. Bassendh Hesham.jpg +0 -0
- images/Eng. Farah Darwish.jpg +0 -0
- images/Eng. Habiba Mohamed.jpg +0 -0
- images/Eng. Hanan Tarek.jpg +0 -0
- images/Eng. Menna Khaled.jpg +0 -0
- images/Eng. Moamen Zaher.jpg +0 -0
- images/Eng. Mohamed Ashraf.jpg +0 -0
- images/Eng. Mohamed Seif.jpg +0 -0
- images/Eng. Omnia Fawzi.jpg +0 -0
- images/Eng. Raghda Ali.jpg +0 -0
- images/Eyad.jpg +3 -0
- images/Mrs. Nancy Barakat.jpg +0 -0
- images/Prof. Ahmed Elshafee.jpg +0 -0
- images/Prof. Ali El-Bastawissy.png +0 -0
- images/Prof. Ammar Mohammed.png +3 -0
- images/Prof. Ayman Ezzat.png +3 -0
- images/Prof. Wael Gomaa.jpg +0 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
images/Dr.Ahmed-Farouk.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
images/Eyad.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
images/EYAD1.jpg filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
images/Prof.[[:space:]]Ammar[[:space:]]Mohammed.png filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
images/Prof.[[:space:]]Ayman[[:space:]]Ezzat.png filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import os
|
| 4 |
+
import insightface
|
| 5 |
+
import numpy as np
|
| 6 |
+
from collections import deque
|
| 7 |
+
|
| 8 |
+
# Load InsightFace model
|
| 9 |
+
model = insightface.app.FaceAnalysis(allowed_modules=['detection', 'recognition'])
|
| 10 |
+
model.prepare(ctx_id=0, det_size=(640, 640)) # CPU: ctx_id=-1
|
| 11 |
+
|
| 12 |
+
# Normalize function
|
| 13 |
+
def normalize(v):
|
| 14 |
+
return v / np.linalg.norm(v)
|
| 15 |
+
|
| 16 |
+
# Cosine similarity function
|
| 17 |
+
def cosine_similarity(a, b):
|
| 18 |
+
return np.dot(a, b)
|
| 19 |
+
|
| 20 |
+
# Load known embeddings
|
| 21 |
+
known_embs = []
|
| 22 |
+
names = []
|
| 23 |
+
for fname in os.listdir("images"):
|
| 24 |
+
if fname.lower().endswith(('.jpg', '.png')):
|
| 25 |
+
img = cv2.imread(os.path.join("images", fname))
|
| 26 |
+
faces = model.get(img)
|
| 27 |
+
if faces:
|
| 28 |
+
emb = normalize(faces[0].embedding)
|
| 29 |
+
known_embs.append(emb)
|
| 30 |
+
names.append(os.path.splitext(fname)[0])
|
| 31 |
+
print(f"Loaded {fname}")
|
| 32 |
+
else:
|
| 33 |
+
print(f"No face in {fname}")
|
| 34 |
+
|
| 35 |
+
# Recognition function for an uploaded image
|
| 36 |
+
def recognize(image):
|
| 37 |
+
face_buffers = {}
|
| 38 |
+
frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 39 |
+
faces = model.get(frame)
|
| 40 |
+
current_buffers = {}
|
| 41 |
+
|
| 42 |
+
for face in faces:
|
| 43 |
+
x1, y1, x2, y2 = face.bbox.astype(int)
|
| 44 |
+
emb = normalize(face.embedding)
|
| 45 |
+
face_id = f"{x1}-{y1}-{x2}-{y2}"
|
| 46 |
+
|
| 47 |
+
if face_id not in face_buffers:
|
| 48 |
+
face_buffers[face_id] = deque(maxlen=5)
|
| 49 |
+
|
| 50 |
+
face_buffers[face_id].append(emb)
|
| 51 |
+
current_buffers[face_id] = face_buffers[face_id]
|
| 52 |
+
|
| 53 |
+
avg_emb = normalize(np.mean(face_buffers[face_id], axis=0))
|
| 54 |
+
sims = [cosine_similarity(avg_emb, known) for known in known_embs]
|
| 55 |
+
max_idx = np.argmax(sims)
|
| 56 |
+
name = "Unknown"
|
| 57 |
+
if sims[max_idx] > 0.5:
|
| 58 |
+
name = names[max_idx]
|
| 59 |
+
|
| 60 |
+
# Draw on the frame
|
| 61 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 62 |
+
cv2.putText(frame, name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
|
| 63 |
+
|
| 64 |
+
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 65 |
+
|
| 66 |
+
# Gradio interface
|
| 67 |
+
iface = gr.Interface(
|
| 68 |
+
fn=recognize,
|
| 69 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 70 |
+
outputs=gr.Image(type="numpy", label="Recognized Faces"),
|
| 71 |
+
title="Face Recognition with InsightFace",
|
| 72 |
+
description="Upload an image, and the system will identify known faces from the 'images/' folder."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
iface.launch()
|
images/Dr. Marwa Solayman.jpg
ADDED
|
images/Dr. Mohamed Ghoniemy.jpg
ADDED
|
images/Dr.Ahmed-Farouk.jpg
ADDED
|
Git LFS Details
|
images/Dr.Khayri.png
ADDED
|
images/EYAD1.jpg
ADDED
|
Git LFS Details
|
images/Eng. Abdelrahman Ezzeldin.jpg
ADDED
|
images/Eng. Bassendh Hesham.jpg
ADDED
|
images/Eng. Farah Darwish.jpg
ADDED
|
images/Eng. Habiba Mohamed.jpg
ADDED
|
images/Eng. Hanan Tarek.jpg
ADDED
|
images/Eng. Menna Khaled.jpg
ADDED
|
images/Eng. Moamen Zaher.jpg
ADDED
|
images/Eng. Mohamed Ashraf.jpg
ADDED
|
images/Eng. Mohamed Seif.jpg
ADDED
|
images/Eng. Omnia Fawzi.jpg
ADDED
|
images/Eng. Raghda Ali.jpg
ADDED
|
images/Eyad.jpg
ADDED
|
Git LFS Details
|
images/Mrs. Nancy Barakat.jpg
ADDED
|
images/Prof. Ahmed Elshafee.jpg
ADDED
|
images/Prof. Ali El-Bastawissy.png
ADDED
|
images/Prof. Ammar Mohammed.png
ADDED
|
Git LFS Details
|
images/Prof. Ayman Ezzat.png
ADDED
|
Git LFS Details
|
images/Prof. Wael Gomaa.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
insightface
|
| 2 |
+
opencv-python
|
| 3 |
+
gradio
|
| 4 |
+
numpy
|