cc1234 commited on
Commit
a3296a3
1 Parent(s): 07843a4

update on 2023-11-25

Browse files
Files changed (5) hide show
  1. app.py +66 -1
  2. face.db +2 -2
  3. face.json +2 -2
  4. persons.zip +2 -2
  5. requirements.txt +1 -0
app.py CHANGED
@@ -2,6 +2,8 @@ import os
2
  import json
3
  import math
4
  import base64
 
 
5
 
6
  os.environ["DEEPFACE_HOME"] = "."
7
 
@@ -142,6 +144,60 @@ def face_distance_to_conf(face_distance, face_match_threshold=20.0):
142
  return linear_val + ((1.0 - linear_val) * math.pow((linear_val - 0.5) * 2, 0.2))
143
 
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  image_search = gr.Interface(
146
  fn=image_search_performer,
147
  inputs=[
@@ -178,4 +234,13 @@ vector_search = gr.Interface(
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")
 
 
 
 
 
 
 
 
 
2
  import json
3
  import math
4
  import base64
5
+ from uuid import uuid4
6
+ from PIL import Image as PILImage
7
 
8
  os.environ["DEEPFACE_HOME"] = "."
9
 
144
  return linear_val + ((1.0 - linear_val) * math.pow((linear_val - 0.5) * 2, 0.2))
145
 
146
 
147
+ def find_faces_in_sprite(image, vtt):
148
+ vtt = base64.b64decode(vtt.replace("data:text/vtt;base64,", ""))
149
+ sprite = PILImage.fromarray(image)
150
+
151
+ results = []
152
+ for i, (left, top, right, bottom, time_seconds) in enumerate(getVTToffsets(vtt)):
153
+ cut_frame = sprite.crop((left, top, left + right, top + bottom))
154
+ faces = DeepFace.extract_faces(np.asarray(cut_frame), detector_backend="mediapipe", enforce_detection=False, align=False)
155
+ faces = [face for face in faces if face['confidence'] > 0.75]
156
+ if faces:
157
+ size = faces[0]['facial_area']['w'] * faces[0]['facial_area']['h']
158
+ data = {'id': str(uuid4()), "offset": (left, top, right, bottom), "frame": i, "time": time_seconds, 'size': size}
159
+ results.append(data)
160
+
161
+ # sort by size
162
+ results = sorted(results, key=lambda x: x['size'], reverse=True)
163
+ return results
164
+
165
+
166
+ def getVTToffsets(vtt):
167
+ time_seconds = 0
168
+ left = top = right = bottom = None
169
+ for line in vtt.decode("utf-8").split("\n"):
170
+ line = line.strip()
171
+
172
+ if "-->" in line:
173
+ # grab the start time
174
+ # 00:00:00.000 --> 00:00:41.000
175
+ start = line.split("-->")[0].strip().split(":")
176
+ # convert to seconds
177
+ time_seconds = (
178
+ int(start[0]) * 3600
179
+ + int(start[1]) * 60
180
+ + float(start[2])
181
+ )
182
+ left = top = right = bottom = None
183
+ elif "xywh=" in line:
184
+ left, top, right, bottom = line.split("xywh=")[-1].split(",")
185
+ left, top, right, bottom = (
186
+ int(left),
187
+ int(top),
188
+ int(right),
189
+ int(bottom),
190
+ )
191
+ else:
192
+ continue
193
+
194
+ if not left:
195
+ continue
196
+
197
+ yield left, top, right, bottom, time_seconds
198
+
199
+
200
+
201
  image_search = gr.Interface(
202
  fn=image_search_performer,
203
  inputs=[
234
  description="512 vector created with deepface of a person and we'll tell you who it is.",
235
  )
236
 
237
+ faces_in_sprite = gr.Interface(
238
+ fn=find_faces_in_sprite,
239
+ inputs=[
240
+ gr.Image(),
241
+ gr.Textbox(label="VTT file")
242
+ ],
243
+ outputs=gr.JSON(label=""),
244
+ )
245
+
246
+ gr.TabbedInterface([image_search, image_search_multiple, vector_search, faces_in_sprite]).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:fa2e948f81e03173f911f677315110792c5aa186c4d8ab6b00b84c7aedcf1541
3
- size 648537696
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:83ddc89e7e2f7f9db6eccd759f2dbcafa9527fd8f48c2e1f0c513cb7efe3fa0a
3
+ size 653004192
face.json CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:b8042e9467c7ca01a3daeca9e7558976c957324c0bd39e9dbb8b02714e29551e
3
- size 8123440
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0872787307365930fef6283cb45ab9883a09e56ed39e0403182831472b73eebe
3
+ size 8180960
persons.zip CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:ad2dc27be78018ad91f3c71a18e80728f634858cbb2383f06c651f084dc5a134
3
- size 3864732
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fa6d657dff6f902d4ecd9e004ee81dea78bd0d1720de30b4218f93b58f5eb85c
3
+ size 3885315
requirements.txt CHANGED
@@ -103,4 +103,5 @@ websockets==10.4
103
  Werkzeug==2.2.3
104
  wrapt==1.15.0
105
  yarl==1.8.2
 
106
  gradio
103
  Werkzeug==2.2.3
104
  wrapt==1.15.0
105
  yarl==1.8.2
106
+ mediapipe==0.10.8
107
  gradio