sabrinabenas commited on
Commit
026ff32
β€’
1 Parent(s): a4133f9

save json to dlc only

Browse files
Files changed (2) hide show
  1. app.py +6 -7
  2. viz_utils.py +31 -6
app.py CHANGED
@@ -13,7 +13,7 @@ from PIL import Image, ImageColor, ImageFont, ImageDraw
13
  from DLC_models.download_utils import DownloadModel
14
  from dlclive import DLCLive, Processor
15
 
16
- from viz_utils import save_results_as_json, draw_keypoints_on_image, draw_bbox_w_text
17
  from detection_utils import predict_md, crop_animal_detections, predict_dlc
18
  from ui_utils import gradio_inputs_for_MD_DLC, gradio_outputs_for_MD_DLC, gradio_description_and_examples
19
 
@@ -105,8 +105,10 @@ def predict_pipeline(img_input,
105
  font_size=font_size,
106
  keypt_color=keypt_color,
107
  marker_size=marker_size)
108
-
109
- return img_input, []
 
 
110
 
111
  else:
112
  # Compute kpts for each crop
@@ -151,10 +153,7 @@ def predict_pipeline(img_input,
151
 
152
 
153
  # Save detection results as json
154
- download_file = save_results_as_json(md_results,
155
- list_kpts_per_crop,
156
- map_label_id_to_str,
157
- bbox_likelihood_th)
158
 
159
  return img_background, download_file
160
 
 
13
  from DLC_models.download_utils import DownloadModel
14
  from dlclive import DLCLive, Processor
15
 
16
+ from viz_utils import save_results_as_json, draw_keypoints_on_image, draw_bbox_w_text, save_results_only_dlc
17
  from detection_utils import predict_md, crop_animal_detections, predict_dlc
18
  from ui_utils import gradio_inputs_for_MD_DLC, gradio_outputs_for_MD_DLC, gradio_description_and_examples
19
 
 
105
  font_size=font_size,
106
  keypt_color=keypt_color,
107
  marker_size=marker_size)
108
+
109
+ donw_file = save_results_only_dlc(list_kpts_per_crop[0], map_label_id_to_str,dlc_model_input_str)
110
+
111
+ return img_input, donw_file
112
 
113
  else:
114
  # Compute kpts for each crop
 
153
 
154
 
155
  # Save detection results as json
156
+ download_file = save_results_as_json(md_results,list_kpts_per_crop,map_label_id_to_str, bbox_likelihood_th,dlc_model_input_str,mega_model_input)
 
 
 
157
 
158
  return img_background, download_file
159
 
viz_utils.py CHANGED
@@ -5,7 +5,9 @@ from matplotlib import cm
5
  import matplotlib
6
  from PIL import Image, ImageColor, ImageFont, ImageDraw
7
  import numpy as np
8
-
 
 
9
  FONTS = {'amiko': "fonts/Amiko-Regular.ttf",
10
  'nature': "fonts/LoveNature.otf",
11
  'painter':"fonts/PainterDecorator.otf",
@@ -104,11 +106,7 @@ def draw_bbox_w_text(img,
104
  return imgR
105
 
106
  ###########################################
107
- def save_results_as_json(md_results,
108
- dlc_outputs,
109
- map_dlc_label_id_to_str,
110
- thr,
111
- path_to_output_file = 'download_predictions.json'):
112
 
113
  """
114
  Output detections as json file
@@ -116,6 +114,8 @@ def save_results_as_json(md_results,
116
  """
117
  # initialise dict to save to json
118
  info = {}
 
 
119
  # info from megaDetector
120
  info['file']= md_results.files[0]
121
  number_bb = len(md_results.xyxy[0].tolist())
@@ -143,6 +143,7 @@ def save_results_as_json(md_results,
143
  aux['confidence MD'] = confidence
144
 
145
  # DLC output
 
146
  kypts = []
147
  for s in dlc_outputs[i]:
148
  aux1 = []
@@ -161,5 +162,29 @@ def save_results_as_json(md_results,
161
  return path_to_output_file
162
 
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
 
165
  ###########################################
 
5
  import matplotlib
6
  from PIL import Image, ImageColor, ImageFont, ImageDraw
7
  import numpy as np
8
+ import pdb
9
+ from datetime import date
10
+ today = date.today()
11
  FONTS = {'amiko': "fonts/Amiko-Regular.ttf",
12
  'nature': "fonts/LoveNature.otf",
13
  'painter':"fonts/PainterDecorator.otf",
 
106
  return imgR
107
 
108
  ###########################################
109
+ def save_results_as_json(md_results, dlc_outputs, map_dlc_label_id_to_str, thr,model,mega_model_input, path_to_output_file = 'download_predictions.json'):
 
 
 
 
110
 
111
  """
112
  Output detections as json file
 
114
  """
115
  # initialise dict to save to json
116
  info = {}
117
+ info['date'] = str(today)
118
+ info['MD_model'] = str(mega_model_input)
119
  # info from megaDetector
120
  info['file']= md_results.files[0]
121
  number_bb = len(md_results.xyxy[0].tolist())
 
143
  aux['confidence MD'] = confidence
144
 
145
  # DLC output
146
+ info['dlc_model'] = model
147
  kypts = []
148
  for s in dlc_outputs[i]:
149
  aux1 = []
 
162
  return path_to_output_file
163
 
164
 
165
+ def save_results_only_dlc(dlc_outputs,map_label_id_to_str,model,output_file = 'dowload_predictions_dlc.json'):
166
+
167
+ """
168
+ write json dlc output
169
+ """
170
+ info = {}
171
+ info['date'] = today
172
+ labels = [n for n in map_label_id_to_str.values()]
173
+ info['dlc_model'] = model
174
+ kypts = []
175
+ for s in dlc_outputs:
176
+ aux1 = []
177
+ for j in s:
178
+ aux1.append(float(j))
179
+
180
+ kypts.append(aux1)
181
+ info['dlc_pred'] = dict(zip(labels,kypts))
182
+
183
+ with open(output_file, 'w') as f:
184
+ json.dump(info, f, indent=1)
185
+ print('Output file saved at {}'.format(output_file))
186
+
187
+ return output_file
188
+
189
 
190
  ###########################################