tidalove commited on
Commit
34384ae
·
1 Parent(s): 9823a94

demo.py writes output json

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -1
  2. tools/demo.py +44 -0
requirements.txt CHANGED
@@ -13,6 +13,6 @@ tensorboard
13
 
14
  # verified versions
15
  # pycocotools corresponds to https://github.com/ppwwyyxx/cocoapi
16
- pycocotools>=2.0.2
17
  onnx>=1.13.0
18
  onnx-simplifier==0.4.10
 
13
 
14
  # verified versions
15
  # pycocotools corresponds to https://github.com/ppwwyyxx/cocoapi
16
+ pycocotools==2.0.7 # >=2.0.2 KAI 6/23
17
  onnx>=1.13.0
18
  onnx-simplifier==0.4.10
tools/demo.py CHANGED
@@ -5,6 +5,7 @@
5
  import argparse
6
  import os
7
  import time
 
8
  from loguru import logger
9
 
10
  import cv2
@@ -83,6 +84,11 @@ def make_parser():
83
  action="store_true",
84
  help="Using TensorRT model for testing.",
85
  )
 
 
 
 
 
86
  return parser
87
 
88
 
@@ -183,6 +189,42 @@ class Predictor(object):
183
  vis_res = vis(img, bboxes, scores, cls, cls_conf, self.cls_names)
184
  return vis_res
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
  def image_demo(predictor, vis_folder, path, current_time, save_result):
188
  if os.path.isdir(path):
@@ -309,6 +351,8 @@ def main(exp, args):
309
  current_time = time.localtime()
310
  if args.demo == "image":
311
  image_demo(predictor, vis_folder, args.path, current_time, args.save_result)
 
 
312
  elif args.demo == "video" or args.demo == "webcam":
313
  imageflow_demo(predictor, vis_folder, current_time, args)
314
 
 
5
  import argparse
6
  import os
7
  import time
8
+ import json
9
  from loguru import logger
10
 
11
  import cv2
 
84
  action="store_true",
85
  help="Using TensorRT model for testing.",
86
  )
87
+ parser.add_argument(
88
+ "--save_json",
89
+ default=False,
90
+ action="store_true"
91
+ )
92
  return parser
93
 
94
 
 
189
  vis_res = vis(img, bboxes, scores, cls, cls_conf, self.cls_names)
190
  return vis_res
191
 
192
+ def print_json(predictor, path):
193
+ # COCO output format: { images: [{id: 0, filename: "x.jpg"}, ...],
194
+ # annotations: [{id: 0, image_id: 0, bbox: [0 0 0 0], score: 0.35, class: 1}, ... ] }
195
+ if os.path.isdir(path):
196
+ files = get_image_list(path)
197
+ else:
198
+ files = [path]
199
+ files.sort()
200
+
201
+ img_list = []
202
+ ann_list = []
203
+
204
+ for img_id, image_name in enumerate(files):
205
+
206
+ outputs, img_info = predictor.inference(image_name)
207
+ ratio = img_info["ratio"]
208
+
209
+ img_entry = {"id": img_id,
210
+ "filename": image_name }
211
+ img_list.append(img_entry)
212
+
213
+ for id, output in enumerate(outputs):
214
+ ann_entry = {"id": id,
215
+ "image_id": img_id,
216
+ "bbox": output[:4] / ratio,
217
+ "cls": output[6],
218
+ "score": output[4] * output[5] }
219
+ ann_list.append(ann_entry)
220
+
221
+ data_dict = { "images": img_list,
222
+ "annotations": ann_list
223
+ }
224
+
225
+ with open(f"{path}/results.json", w) as f:
226
+ json.dump(data_dict, f)
227
+
228
 
229
  def image_demo(predictor, vis_folder, path, current_time, save_result):
230
  if os.path.isdir(path):
 
351
  current_time = time.localtime()
352
  if args.demo == "image":
353
  image_demo(predictor, vis_folder, args.path, current_time, args.save_result)
354
+ if args.save_json:
355
+ print_json(predictor, args.path)
356
  elif args.demo == "video" or args.demo == "webcam":
357
  imageflow_demo(predictor, vis_folder, current_time, args)
358