update samples/test/eval_mAP_on_coco_val/pd2cocojson.py.

Signed-off-by: jario-jin <jariof@foxmail.com>
This commit is contained in:
jario-jin 2023-08-14 01:57:52 +00:00 committed by Gitee
parent 5c30296b73
commit 1859cd66eb
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 36 additions and 36 deletions

View File

@ -6,57 +6,57 @@ import cv2
# revert prediction results to coco_json
path = os.path.abspath(os.path.join(os.getcwd(),"../../.."))
# all files dir
images_path = path+'/val2017/val2017'
preds_path = path+'/val2017/preds'
images_path = path + '/val2017/val2017'
preds_path = path + '/val2017/preds'
coco_json_save ='pd_coco.json'
coco_json_save = 'pd_coco.json'
# config coco_json
coco_json = []
# remap the id of the coco dataset
id_map = {1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7, 9: 8, 10: 9, 11: 10, 13: 11, 14: 12, 15: 13, 16: 14, 17: 15, 18: 16, 19: 17, 20: 18, 21: 19, 22: 20, 23: 21, 24: 22, 25: 23, 27: 24, 28: 25, 31: 26, 32: 27, 33: 28, 34: 29, 35: 30, 36: 31, 37: 32, 38: 33, 39: 34, 40: 35, 41: 36, 42: 37, 43: 38, 44: 39, 46: 40, 47: 41, 48: 42, 49: 43, 50: 44, 51: 45, 52: 46, 53: 47, 54: 48, 55: 49, 56: 50, 57: 51, 58: 52, 59: 53, 60: 54, 61: 55, 62: 56, 63: 57, 64: 58, 65: 59, 67: 60, 70: 61, 72: 62, 73: 63, 74: 64, 75: 65, 76: 66, 77: 67, 78: 68, 79: 69, 80: 70, 81: 71, 82: 72, 84: 73, 85: 74, 86: 75, 87: 76, 88: 77, 89: 78, 90: 79}
reid_mp = { value: key for key, value in id_map.items()}
reid_mp = {value: key for key, value in id_map.items()}
# 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
# 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': int(reid_mp[category_id]),
'bbox': [xmin, ymin, w, h],
'score': float(pred[5]),
'area': w * h})
coco_json.append({
'image_id': int(image_name),
'category_id': int(reid_mp[category_id]),
'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!')