eyad222 commited on
Commit
74a6c37
·
verified ·
1 Parent(s): df6b26c

Upload 25 files

Browse files
.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

  • SHA256: a9965c493b17d1aaf16b0a2f698bf0927987e529b15168958082873d51493766
  • Pointer size: 131 Bytes
  • Size of remote file: 115 kB
images/Dr.Khayri.png ADDED
images/EYAD1.jpg ADDED

Git LFS Details

  • SHA256: a29cd3a8e1e783ef199829706dbd8b02d431f5a2a8e1980ec2d7a323c3d6ef7f
  • Pointer size: 131 Bytes
  • Size of remote file: 138 kB
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

  • SHA256: 5dbee58b3e515b056e755d3f179bea13a57132a9a715e6ea7a56a53fc1a35ace
  • Pointer size: 131 Bytes
  • Size of remote file: 177 kB
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

  • SHA256: 9117425f53856eb27d8041ba5d90fab364a989c10ea1e6d4fc69809df8e08000
  • Pointer size: 131 Bytes
  • Size of remote file: 279 kB
images/Prof. Ayman Ezzat.png ADDED

Git LFS Details

  • SHA256: 037fcf255beaf2e419f39d72b27bb4146bded71142040085ee18538d06f48fb7
  • Pointer size: 131 Bytes
  • Size of remote file: 320 kB
images/Prof. Wael Gomaa.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ insightface
2
+ opencv-python
3
+ gradio
4
+ numpy