update samples/test/eval_mAP_on_coco_val/pd2cocojson.py.
Signed-off-by: jario-jin <jariof@foxmail.com>
This commit is contained in:
parent
7a01223db4
commit
cf7fad3b2b
|
@ -17,42 +17,42 @@ coco_json = []
|
||||||
# load images dir
|
# load images dir
|
||||||
images = os.listdir(images_path)
|
images = os.listdir(images_path)
|
||||||
for image in images:
|
for image in images:
|
||||||
print(image)
|
print(image)
|
||||||
# get image name
|
# get image name
|
||||||
image_name, image_suffix = os.path.splitext(image)
|
image_name, image_suffix = os.path.splitext(image)
|
||||||
# get image W and H
|
# get image W and H
|
||||||
image_path = images_path + '/' + image
|
image_path = images_path + '/' + image
|
||||||
img = cv2.imread(image_path)
|
img = cv2.imread(image_path)
|
||||||
height, width, _ = img.shape
|
height, width, _ = img.shape
|
||||||
|
|
||||||
# read pred's txt
|
# read pred's txt
|
||||||
pred_path = preds_path + '/' + image_name + '.txt'
|
pred_path = preds_path + '/' + image_name + '.txt'
|
||||||
if not os.path.exists(pred_path):
|
if not os.path.exists(pred_path):
|
||||||
continue
|
continue
|
||||||
with open(pred_path, 'r') as f:
|
with open(pred_path, 'r') as f:
|
||||||
preds = f.readlines()
|
preds = f.readlines()
|
||||||
preds = [l.strip() for l in preds]
|
preds = [l.strip() for l in preds]
|
||||||
for j,pred in enumerate(preds):
|
for j, pred in enumerate(preds):
|
||||||
pred = pred.split(' ')
|
pred = pred.split(' ')
|
||||||
category_id = int(pred[0])
|
category_id = int(pred[0])
|
||||||
x = float(pred[1]) * width
|
x = float(pred[1]) * width
|
||||||
y = float(pred[2]) * height
|
y = float(pred[2]) * height
|
||||||
w = float(pred[3]) * width
|
w = float(pred[3]) * width
|
||||||
h = float(pred[4]) * height
|
h = float(pred[4]) * height
|
||||||
xmin = x - w / 2
|
xmin = x - w / 2
|
||||||
ymin = y - h / 2
|
ymin = y - h / 2
|
||||||
xmax = x + w / 2
|
xmax = x + w / 2
|
||||||
ymax = y + h / 2
|
ymax = y + h / 2
|
||||||
|
|
||||||
coco_json.append({
|
coco_json.append({
|
||||||
'image_id': int(image_name),
|
'image_id': int(image_name),
|
||||||
'category_id': category_id + 1,
|
'category_id': category_id + 1,
|
||||||
'bbox': [xmin, ymin, w, h],
|
'bbox': [xmin, ymin, w, h],
|
||||||
'score': float(pred[5]),
|
'score': float(pred[5]),
|
||||||
'area': w * h})
|
'area': w * h})
|
||||||
|
|
||||||
# save json
|
# save json
|
||||||
with open(os.path.join(coco_json_save), 'w') as f:
|
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!')
|
print(len(coco_json), 'Done!')
|
||||||
|
|
Loading…
Reference in New Issue