AnnasBlackHat commited on
Commit
68a20ec
1 Parent(s): 5d3f355
Files changed (2) hide show
  1. app.py +20 -6
  2. photo_search.py +8 -6
app.py CHANGED
@@ -6,12 +6,13 @@ from photo_search import PhotoSearch
6
  from PIL import Image
7
 
8
  load_dotenv()
9
- # tree_path = download.gdrive(os.getenv('TREE_GDRIVE_ID'), dir='temp')
10
- # data_path = download.gdrive(os.getenv('DATA_GDRIVE_ID'), dir='temp')
11
- tree_path= 'temp/trees.ann'
12
- data_path = 'temp/result_torch.npz'
13
 
14
  ai = PhotoSearch(tree_path, data_path)
 
15
 
16
  def search_photo(image):
17
  try:
@@ -25,7 +26,16 @@ def search_photo(image):
25
  - Jangan upload foto yang menggunakan aksesoris yang tidak kamu gunakan saat lari (semisal kacamata)
26
  '''
27
 
28
- result = f"{len(found_ids)} foto kamu berhasil ditemukan!"
 
 
 
 
 
 
 
 
 
29
  except Exception as e:
30
  result = f"Gagal: {e}"
31
  return result
@@ -35,6 +45,10 @@ gr_inputs = [
35
  gr.Image(type='pil')
36
  ]
37
 
 
 
 
 
38
  gr.Markdown('Upload Foto Wajah Kamu Ya (Pastikan hanya terdapat SATU wajah pada foto yang kamu upload)')
39
- iface = gr.Interface(fn=search_photo, inputs=gr_inputs, outputs="markdown")
40
  iface.launch()
 
6
  from PIL import Image
7
 
8
  load_dotenv()
9
+ tree_path = download.gdrive(os.getenv('TREE_GDRIVE_ID'), dir='temp')
10
+ data_path = download.gdrive(os.getenv('DATA_GDRIVE_ID'), dir='temp')
11
+ # tree_path= 'temp/trees-pocari.ann'
12
+ # data_path = 'temp/drive_ids_pocari.json'
13
 
14
  ai = PhotoSearch(tree_path, data_path)
15
+ htmlPref = '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Image Gallery</title><style>body{font-family:Arial,sans-serif;margin:0;padding:0}.gallery{display:grid;grid-template-columns:repeat(3,1fr);gap:10px}.gallery img{width:100%;height:auto;display:block}</style></head><body>'
16
 
17
  def search_photo(image):
18
  try:
 
26
  - Jangan upload foto yang menggunakan aksesoris yang tidak kamu gunakan saat lari (semisal kacamata)
27
  '''
28
 
29
+ # result = f"{len(found_ids)} foto kamu berhasil ditemukan!"
30
+ link_format = [f'<a href="{link}" target="_blank"> <img src="{link}" height="150"></img></a>' for i, link in enumerate(found_ids)]
31
+
32
+ return htmlPref + f'''
33
+ <h2> {len(found_ids)} Foto Ditemukan! </h2>
34
+ <p> Klik foto untuk memperbesar atau download </p>
35
+ <div class="gallery">
36
+ {"".join(link_format)}
37
+ </div></body></html>
38
+ '''
39
  except Exception as e:
40
  result = f"Gagal: {e}"
41
  return result
 
45
  gr.Image(type='pil')
46
  ]
47
 
48
+ gallery = gr.Gallery(
49
+ label="Generated images", show_label=False, elem_id="gallery"
50
+ ).style(columns=[2], rows=[2], object_fit="contain", height="auto")
51
+
52
  gr.Markdown('Upload Foto Wajah Kamu Ya (Pastikan hanya terdapat SATU wajah pada foto yang kamu upload)')
53
+ iface = gr.Interface(fn=search_photo, inputs=gr_inputs, outputs="html")
54
  iface.launch()
photo_search.py CHANGED
@@ -2,6 +2,7 @@ from annoy import AnnoyIndex
2
  from face_recognition import FacenetPytorch
3
  import numpy as np
4
  from numpy.linalg import norm
 
5
 
6
  class PhotoSearch:
7
  def __init__ (self, tree_path, data_path, index_size = 512):
@@ -10,9 +11,10 @@ class PhotoSearch:
10
 
11
  self.t = AnnoyIndex(index_size, 'angular')
12
  self.t.load(tree_path)
13
-
14
- data = np.load(data_path, allow_pickle=True)
15
- self.face_grive_id = data.f.id
 
16
  self.extractor = FacenetPytorch()
17
 
18
  def search(self, image):
@@ -21,11 +23,11 @@ class PhotoSearch:
21
  result = []
22
  for n in nns:
23
  face_origin = self.t.get_item_vector(n)
24
- print(self.face_grive_id[n])
25
  if self.cosine(face_emb, face_origin) < 0.7: continue
26
 
27
- result.append('get..')
28
-
29
  return result
30
 
31
  def cosine(self, x, y):
 
2
  from face_recognition import FacenetPytorch
3
  import numpy as np
4
  from numpy.linalg import norm
5
+ import json
6
 
7
  class PhotoSearch:
8
  def __init__ (self, tree_path, data_path, index_size = 512):
 
11
 
12
  self.t = AnnoyIndex(index_size, 'angular')
13
  self.t.load(tree_path)
14
+
15
+ self.grive_id = []
16
+ with open(data_path) as f :
17
+ self.grive_id = json.load(f)
18
  self.extractor = FacenetPytorch()
19
 
20
  def search(self, image):
 
23
  result = []
24
  for n in nns:
25
  face_origin = self.t.get_item_vector(n)
26
+ # print(self.grive_id[n])
27
  if self.cosine(face_emb, face_origin) < 0.7: continue
28
 
29
+ result.append(f'https://drive.google.com/uc?export=view&id={self.grive_id[n]}')
30
+
31
  return result
32
 
33
  def cosine(self, x, y):