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-11 11:31:51 +00:00 committed by Gitee
parent 7a01223db4
commit cf7fad3b2b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 33 additions and 33 deletions

View File

@ -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
# 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})
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!')