cc1234 commited on
Commit
61c9bd4
1 Parent(s): 3c29f58

update on 2023-10-21

Browse files
Files changed (5) hide show
  1. app.py +41 -2
  2. face.db +2 -2
  3. face.json +2 -2
  4. persons.zip +2 -2
  5. temp.jpg +0 -0
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import json
3
  import math
 
4
 
5
  os.environ["DEEPFACE_HOME"] = "."
6
 
@@ -40,6 +41,32 @@ def image_search_performer(image, threshold=20.0, results=3):
40
  return search_performer(face, threshold, results)
41
 
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  def vector_search_performer(vector_json, threshold=20.0, results=3):
44
  """Search for a performer from a vector
45
 
@@ -88,7 +115,7 @@ def search_performer(vector, threshold=20.0, results=3):
88
  persons[id].update(PERFORMER_DB.get(id))
89
 
90
  persons = sorted(persons.values(), key=lambda x: x["distance"])
91
- persons = [p for p in persons if p["distance"] < threshold]
92
  return persons[:results]
93
 
94
 
@@ -127,6 +154,18 @@ image_search = gr.Interface(
127
  description="Upload an image of a person and we'll tell you who it is.",
128
  )
129
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  vector_search = gr.Interface(
131
  fn=vector_search_performer,
132
  inputs=[
@@ -139,4 +178,4 @@ vector_search = gr.Interface(
139
  description="512 vector created with deepface of a person and we'll tell you who it is.",
140
  )
141
 
142
- gr.TabbedInterface([image_search, vector_search]).launch(enable_queue=True, server_name="0.0.0.0")
 
1
  import os
2
  import json
3
  import math
4
+ import base64
5
 
6
  os.environ["DEEPFACE_HOME"] = "."
7
 
 
41
  return search_performer(face, threshold, results)
42
 
43
 
44
+
45
+ def image_search_performers(image, threshold=20.0, results=3):
46
+ image_array = np.array(image)
47
+
48
+ response = []
49
+ faces = DeepFace.represent(img_path = image_array, detector_backend='retinaface', model_name='Facenet512', normalization="Facenet2018")
50
+ # faces = DeepFace.represent(img_path = image_array, detector_backend='yolov8', model_name='SFace')
51
+ for face in faces:
52
+ embedding = face['embedding']
53
+ area = face['facial_area']
54
+ confidence = face['face_confidence']
55
+ cimage = image.crop((area['x'], area['y'], area['x'] + area['w'], area['y'] + area['h']))
56
+ # return image base64 encoded for display
57
+ with open('temp.jpg', 'wb') as f:
58
+ cimage.save(f, format='JPEG')
59
+ im_b64 = base64.b64encode(open('temp.jpg', 'rb').read())
60
+
61
+ response.append({
62
+ 'image': im_b64.decode('ascii'),
63
+ 'confidence': confidence,
64
+ 'performers': search_performer(embedding, threshold, results)
65
+ })
66
+
67
+ return response
68
+
69
+
70
  def vector_search_performer(vector_json, threshold=20.0, results=3):
71
  """Search for a performer from a vector
72
 
 
115
  persons[id].update(PERFORMER_DB.get(id))
116
 
117
  persons = sorted(persons.values(), key=lambda x: x["distance"])
118
+ # persons = [p for p in persons if p["distance"] < threshold]
119
  return persons[:results]
120
 
121
 
 
154
  description="Upload an image of a person and we'll tell you who it is.",
155
  )
156
 
157
+ image_search_multiple = gr.Interface(
158
+ fn=image_search_performers,
159
+ inputs=[
160
+ gr.components.Image(type='pil'),
161
+ gr.components.Slider(label="threshold",minimum=0.0, maximum=30.0, value=20.0),
162
+ gr.components.Slider(label="results", minimum=0, maximum=50, value=3, step=1),
163
+ ],
164
+ outputs=gr.outputs.JSON(label=""),
165
+ title="Who is in the photo?",
166
+ description="Upload an image of a person(s) and we'll tell you who it is.",
167
+ )
168
+
169
  vector_search = gr.Interface(
170
  fn=vector_search_performer,
171
  inputs=[
 
178
  description="512 vector created with deepface of a person and we'll tell you who it is.",
179
  )
180
 
181
+ gr.TabbedInterface([image_search, image_search_multiple, vector_search]).launch(enable_queue=True, server_name="0.0.0.0")
face.db CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:14866ae4466e9beadd2e9bab69508c4e665f51f35c273c8109d13e32af3e2400
3
- size 630139200
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a00dd4d0faee9b0bc2b33968d41e7db8d0acb4b2971695c4782926432461b8eb
3
+ size 634081440
face.json CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:92d94c2e3538056a5924f0647a3b239c3585257075b553b9f5abfe37a93524e1
3
- size 7876960
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a4d19429102a70aa88f298e51ed821523d92d7a836fc6aac7c3aca8583505a90
3
+ size 7936400
persons.zip CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:716498ffa78d2f3116402f179c3b74bd404923e971845926018b9d307d37904f
3
- size 3788836
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f20040291e460866c497f1077a3df127498a7968602b6dfb2ce5e2db762ce606
3
+ size 3808624
temp.jpg ADDED