From cf7fad3b2b1dda84d3f4bf27102043cd393b5ba5 Mon Sep 17 00:00:00 2001 From: jario-jin Date: Fri, 11 Aug 2023 11:31:51 +0000 Subject: [PATCH] update samples/test/eval_mAP_on_coco_val/pd2cocojson.py. Signed-off-by: jario-jin --- .../test/eval_mAP_on_coco_val/pd2cocojson.py | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/samples/test/eval_mAP_on_coco_val/pd2cocojson.py b/samples/test/eval_mAP_on_coco_val/pd2cocojson.py index 05f002a..3d74acb 100644 --- a/samples/test/eval_mAP_on_coco_val/pd2cocojson.py +++ b/samples/test/eval_mAP_on_coco_val/pd2cocojson.py @@ -17,42 +17,42 @@ coco_json = [] # load images dir images = os.listdir(images_path) for image in images: - print(image) - # get image name - image_name, image_suffix = os.path.splitext(image) - # get image W and H - image_path = images_path + '/' + image - img = cv2.imread(image_path) - height, width, _ = img.shape + print(image) + # get image name + image_name, image_suffix = os.path.splitext(image) + # get image W and H + image_path = images_path + '/' + image + img = cv2.imread(image_path) + height, width, _ = img.shape - # read pred's txt - pred_path = preds_path + '/' + image_name + '.txt' - if not os.path.exists(pred_path): - continue - with open(pred_path, 'r') as f: - preds = f.readlines() - preds = [l.strip() for l in preds] - for j,pred in enumerate(preds): - pred = pred.split(' ') - category_id = int(pred[0]) - x = float(pred[1]) * width - y = float(pred[2]) * height - w = float(pred[3]) * width - h = float(pred[4]) * height - xmin = x - w / 2 - ymin = y - h / 2 - xmax = x + w / 2 - ymax = y + h / 2 - - coco_json.append({ - 'image_id': int(image_name), - 'category_id': category_id + 1, - 'bbox': [xmin, ymin, w, h], - 'score': float(pred[5]), - 'area': w * h}) + # read pred's txt + pred_path = preds_path + '/' + image_name + '.txt' + if not os.path.exists(pred_path): + continue + with open(pred_path, 'r') as f: + preds = f.readlines() + preds = [l.strip() for l in preds] + for j, pred in enumerate(preds): + pred = pred.split(' ') + category_id = int(pred[0]) + x = float(pred[1]) * width + y = float(pred[2]) * height + w = float(pred[3]) * width + h = float(pred[4]) * height + xmin = x - w / 2 + ymin = y - h / 2 + xmax = x + w / 2 + ymax = y + h / 2 + + coco_json.append({ + 'image_id': int(image_name), + 'category_id': category_id + 1, + 'bbox': [xmin, ymin, w, h], + 'score': float(pred[5]), + 'area': w * h}) # save json with open(os.path.join(coco_json_save), 'w') as f: - json.dump(coco_json, f, indent=2) + json.dump(coco_json, f, indent=2) print(len(coco_json), 'Done!')