tomas-gajarsky commited on
Commit
636e0ba
·
1 Parent(s): b0d6c47

Format response

Browse files
Files changed (1) hide show
  1. app.py +23 -2
app.py CHANGED
@@ -3,6 +3,7 @@ import json
3
  import argparse
4
  import operator
5
  import gradio as gr
 
6
  import torchvision
7
  from typing import Tuple, Dict
8
  from facetorch import FaceAnalyzer
@@ -23,6 +24,27 @@ args = parser.parse_args()
23
  cfg = OmegaConf.load(args.path_conf)
24
  analyzer = FaceAnalyzer(cfg.analyzer)
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def gen_sim_dict_str(response: ImageData, pred_name: str = "verify", index: int = 0)-> str:
27
  if len(response.faces) > 0:
28
  base_emb = response.faces[index].preds[pred_name].logits
@@ -34,7 +56,6 @@ def gen_sim_dict_str(response: ImageData, pred_name: str = "verify", index: int
34
 
35
  return sim_dict_sort_str
36
 
37
-
38
  def inference(path_image: str) -> Tuple:
39
  response = analyzer.run(
40
  path_image=path_image,
@@ -50,7 +71,7 @@ def inference(path_image: str) -> Tuple:
50
  fer_dict_str = str({face.indx: face.preds["fer"].label for face in response.faces})
51
  au_dict_str = str({face.indx: face.preds["au"].other["multi"] for face in response.faces})
52
  deepfake_dict_str = str({face.indx: face.preds["deepfake"].label for face in response.faces})
53
- response_str = str(response)
54
 
55
  sim_dict_str_embed = gen_sim_dict_str(response, pred_name="embed", index=0)
56
  sim_dict_str_verify = gen_sim_dict_str(response, pred_name="verify", index=0)
 
3
  import argparse
4
  import operator
5
  import gradio as gr
6
+ import torch
7
  import torchvision
8
  from typing import Tuple, Dict
9
  from facetorch import FaceAnalyzer
 
24
  cfg = OmegaConf.load(args.path_conf)
25
  analyzer = FaceAnalyzer(cfg.analyzer)
26
 
27
+
28
+ def tensor_to_list(tensor: torch.Tensor) -> list:
29
+ return tensor.tolist()
30
+
31
+ def dataclass_to_dict(obj):
32
+ if hasattr(obj, "__dataclass_fields__"):
33
+ return asdict(obj)
34
+ return obj
35
+
36
+ def image_data_to_json(image_data: ImageData) -> str:
37
+ # Convert tensors to lists
38
+ image_data.img = tensor_to_list(image_data.img)
39
+ image_data.tensor = tensor_to_list(image_data.tensor)
40
+
41
+ # Convert dataclass to dictionary
42
+ data_dict = dataclass_to_dict(image_data)
43
+
44
+ # Convert dictionary to JSON string
45
+ json_str = json.dumps(data_dict, indent=4, default=dataclass_to_dict)
46
+ return json_str
47
+
48
  def gen_sim_dict_str(response: ImageData, pred_name: str = "verify", index: int = 0)-> str:
49
  if len(response.faces) > 0:
50
  base_emb = response.faces[index].preds[pred_name].logits
 
56
 
57
  return sim_dict_sort_str
58
 
 
59
  def inference(path_image: str) -> Tuple:
60
  response = analyzer.run(
61
  path_image=path_image,
 
71
  fer_dict_str = str({face.indx: face.preds["fer"].label for face in response.faces})
72
  au_dict_str = str({face.indx: face.preds["au"].other["multi"] for face in response.faces})
73
  deepfake_dict_str = str({face.indx: face.preds["deepfake"].label for face in response.faces})
74
+ response_str = image_data_to_json(response)
75
 
76
  sim_dict_str_embed = gen_sim_dict_str(response, pred_name="embed", index=0)
77
  sim_dict_str_verify = gen_sim_dict_str(response, pred_name="verify", index=0)