Spaces:
Running
Running
tomas-gajarsky
commited on
Commit
·
0184aed
1
Parent(s):
758a06d
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,9 @@ import json
|
|
2 |
import operator
|
3 |
import gradio as gr
|
4 |
import torchvision
|
|
|
5 |
from facetorch import FaceAnalyzer
|
|
|
6 |
from omegaconf import OmegaConf
|
7 |
from torch.nn.functional import cosine_similarity
|
8 |
|
@@ -10,8 +12,15 @@ from torch.nn.functional import cosine_similarity
|
|
10 |
cfg = OmegaConf.load("config.merged.yml")
|
11 |
analyzer = FaceAnalyzer(cfg.analyzer)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
def inference(path_image):
|
15 |
response = analyzer.run(
|
16 |
path_image=path_image,
|
17 |
batch_size=cfg.batch_size,
|
@@ -27,17 +36,15 @@ def inference(path_image):
|
|
27 |
deepfake_dict_str = str({face.indx: face.preds["deepfake"].label for face in response.faces})
|
28 |
response_str = str(response)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
sim_dict_sort = dict(sorted(sim_dict.items(), key=operator.itemgetter(1),reverse=True))
|
33 |
-
sim_dict_sort_str = str(sim_dict_sort)
|
34 |
|
35 |
-
out_tuple = (pil_image, fer_dict_str, deepfake_dict_str,
|
36 |
return out_tuple
|
37 |
|
38 |
|
39 |
title = "facetorch"
|
40 |
-
description = "Demo of facetorch, a Python library that can detect faces and analyze facial features using deep neural networks. The goal is to gather open-sourced face analysis models from the community and optimize them for performance using
|
41 |
article = "<p style='text-align: center'><a href='https://github.com/tomas-gajarsky/facetorch' target='_blank'>facetorch GitHub repository</a></p>"
|
42 |
|
43 |
demo=gr.Interface(
|
@@ -46,11 +53,12 @@ demo=gr.Interface(
|
|
46 |
[gr.outputs.Image(type="pil", label="Output"),
|
47 |
gr.outputs.Textbox(label="Facial Expression Recognition"),
|
48 |
gr.outputs.Textbox(label="DeepFake Detection"),
|
|
|
49 |
gr.outputs.Textbox(label="Cosine similarity on Face Verification Embeddings"),
|
50 |
gr.outputs.Textbox(label="Response")],
|
51 |
title=title,
|
52 |
description=description,
|
53 |
article=article,
|
54 |
-
examples=[["./test.jpg"], ["./test2.jpg"], ["./
|
55 |
)
|
56 |
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|
|
|
2 |
import operator
|
3 |
import gradio as gr
|
4 |
import torchvision
|
5 |
+
from typing import Tuple, Dict
|
6 |
from facetorch import FaceAnalyzer
|
7 |
+
from facetorch.datastruct import ImageData
|
8 |
from omegaconf import OmegaConf
|
9 |
from torch.nn.functional import cosine_similarity
|
10 |
|
|
|
12 |
cfg = OmegaConf.load("config.merged.yml")
|
13 |
analyzer = FaceAnalyzer(cfg.analyzer)
|
14 |
|
15 |
+
def get_sim_dict_str(response: ImageData, pred_name: str = "verify", index: int = 0)-> str:
|
16 |
+
base_emb = response.faces[index].preds[pred_name].logits
|
17 |
+
sim_dict = {face.indx: cosine_similarity(base_emb, face.preds[pred_name].logits, dim=0).item() for face in response.faces}
|
18 |
+
sim_dict_sort = dict(sorted(sim_dict.items(), key=operator.itemgetter(1),reverse=True))
|
19 |
+
sim_dict_sort_str = str(sim_dict_sort)
|
20 |
+
return sim_dict_sort_str
|
21 |
+
|
22 |
|
23 |
+
def inference(path_image: str) -> Tuple:
|
24 |
response = analyzer.run(
|
25 |
path_image=path_image,
|
26 |
batch_size=cfg.batch_size,
|
|
|
36 |
deepfake_dict_str = str({face.indx: face.preds["deepfake"].label for face in response.faces})
|
37 |
response_str = str(response)
|
38 |
|
39 |
+
sim_dict_str_embed = get_sim_dict_str(response, pred_name="embed", index=0)
|
40 |
+
sim_dict_str_verify = get_sim_dict_str(response, pred_name="verify", index=0)
|
|
|
|
|
41 |
|
42 |
+
out_tuple = (pil_image, fer_dict_str, deepfake_dict_str, sim_dict_str_embed, sim_dict_str_verify, response_str)
|
43 |
return out_tuple
|
44 |
|
45 |
|
46 |
title = "facetorch"
|
47 |
+
description = "Demo of facetorch, a Python library that can detect faces and analyze facial features using deep neural networks. The goal is to gather open-sourced face analysis models from the community and optimize them for performance using TorchScript. Try selecting one of the example images or upload your own."
|
48 |
article = "<p style='text-align: center'><a href='https://github.com/tomas-gajarsky/facetorch' target='_blank'>facetorch GitHub repository</a></p>"
|
49 |
|
50 |
demo=gr.Interface(
|
|
|
53 |
[gr.outputs.Image(type="pil", label="Output"),
|
54 |
gr.outputs.Textbox(label="Facial Expression Recognition"),
|
55 |
gr.outputs.Textbox(label="DeepFake Detection"),
|
56 |
+
gr.outputs.Textbox(label="Cosine similarity on Face Representation Embeddings"),
|
57 |
gr.outputs.Textbox(label="Cosine similarity on Face Verification Embeddings"),
|
58 |
gr.outputs.Textbox(label="Response")],
|
59 |
title=title,
|
60 |
description=description,
|
61 |
article=article,
|
62 |
+
examples=[["./test5.jpg"], ["./test.jpg"], ["./test4.jpg"], ["./test2.jpg"], ["./test8.jpg"], ["./test6.jpg"], ["./test3.jpg"]],
|
63 |
)
|
64 |
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|