Mahmoudmody777 commited on
Commit
3dcfa99
·
verified ·
1 Parent(s): 864f793

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -19
app.py CHANGED
@@ -2,12 +2,19 @@ import gradio as gr
2
  import numpy as np
3
  from PIL import Image
4
  import torch
5
- from facenet_pytorch import InceptionResnetV1, MTCNN
6
-
7
- device = 'cpu'
8
 
 
 
9
  mtcnn = MTCNN(image_size=160, margin=20, device=device)
10
- model = InceptionResnetV1(pretrained='vggface2').eval()
 
 
 
 
 
 
11
 
12
  def get_embedding(img):
13
  face = mtcnn(img)
@@ -18,23 +25,58 @@ def get_embedding(img):
18
  emb = model(face)
19
  return emb[0].numpy()
20
 
21
- def compare(img1, img2):
22
- e1 = get_embedding(img1)
23
- e2 = get_embedding(img2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- if e1 is None or e2 is None:
26
- return "لم يتم اكتشاف وجه واضح في إحدى الصورتين"
 
27
 
28
- sim = np.dot(e1, e2) / (np.linalg.norm(e1) * np.linalg.norm(e2))
29
- return f"نسبة التشابه: {round(sim * 100, 2)} %"
30
 
31
- with gr.Blocks() as demo:
32
- gr.Markdown("## Face Similarity MVP (Children Images)")
33
- img1 = gr.Image(type="pil", label="صورة الطفل الأولى")
34
- img2 = gr.Image(type="pil", label="صورة الطفل الثانية")
35
- btn = gr.Button("قارن")
36
- out = gr.Textbox()
37
 
38
- btn.click(compare, inputs=[img1, img2], outputs=out)
 
 
 
 
 
39
 
40
- demo.launch()
 
2
  import numpy as np
3
  from PIL import Image
4
  import torch
5
+ from facenet_pytorch import MTCNN, InceptionResnetV1
6
+ import os
 
7
 
8
+ # إعدادات
9
+ device = "cpu"
10
  mtcnn = MTCNN(image_size=160, margin=20, device=device)
11
+ model = InceptionResnetV1(pretrained="vggface2").eval()
12
+
13
+ DB_DIR = "db_images"
14
+ os.makedirs(DB_DIR, exist_ok=True)
15
+
16
+ embeddings = []
17
+ images = []
18
 
19
  def get_embedding(img):
20
  face = mtcnn(img)
 
25
  emb = model(face)
26
  return emb[0].numpy()
27
 
28
+ def rebuild_db():
29
+ global embeddings, images
30
+ embeddings, images = [], []
31
+ for fname in os.listdir(DB_DIR):
32
+ path = os.path.join(DB_DIR, fname)
33
+ try:
34
+ img = Image.open(path).convert("RGB")
35
+ emb = get_embedding(img)
36
+ if emb is not None:
37
+ embeddings.append(emb)
38
+ images.append(path)
39
+ except:
40
+ pass
41
+
42
+ def add_to_db(img):
43
+ idx = len(os.listdir(DB_DIR))
44
+ path = os.path.join(DB_DIR, f"img_{idx}.jpg")
45
+ img.save(path)
46
+ rebuild_db()
47
+ return "تمت الإضافة"
48
+
49
+ def search_similar(img):
50
+ if not embeddings:
51
+ return "قاعدة الصور فارغة", []
52
+
53
+ q = get_embedding(img)
54
+ if q is None:
55
+ return "لم يتم اكتشاف وجه", []
56
+
57
+ sims = []
58
+ for i, e in enumerate(embeddings):
59
+ s = np.dot(q, e) / (np.linalg.norm(q) * np.linalg.norm(e))
60
+ sims.append((s, images[i]))
61
 
62
+ sims = sorted(sims, reverse=True)[:5]
63
+ results = [(Image.open(p), f"{round(s*100,2)}%") for s, p in sims]
64
+ return "نتائج البحث", results
65
 
66
+ with gr.Blocks() as app:
67
+ gr.Markdown("## Face Search MVP")
68
 
69
+ with gr.Tab("إضافة صورة لقاعدة البيانات"):
70
+ img_add = gr.Image(type="pil")
71
+ btn_add = gr.Button("إضافة")
72
+ out_add = gr.Textbox()
73
+ btn_add.click(add_to_db, img_add, out_add)
 
74
 
75
+ with gr.Tab("بحث عن شبيه"):
76
+ img_q = gr.Image(type="pil")
77
+ btn_s = gr.Button("بحث")
78
+ out_txt = gr.Textbox()
79
+ out_gallery = gr.Gallery(columns=5)
80
+ btn_s.click(search_similar, img_q, [out_txt, out_gallery])
81
 
82
+ app.launch()