37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
import json
|
|
path='/data2/doris/coco/annotations/instances_val2017.json'
|
|
save_path = 'doc/coco_val2017_GT.json'
|
|
with open(path, newline='') as jsonfile:
|
|
data = json.load(jsonfile)
|
|
|
|
def coco80_to_coco91_class():
|
|
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34,
|
|
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
|
|
64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90]
|
|
return x
|
|
coco91class = coco80_to_coco91_class()
|
|
img_info = data["images"]
|
|
ann_info = data["annotations"]
|
|
all_info = {}
|
|
for img_i in img_info:
|
|
id = img_i["id"]
|
|
path = img_i["file_name"]
|
|
all_info[id]={'path':path,'bbox':[]}
|
|
|
|
for ann_i in ann_info:
|
|
id = ann_i["image_id"]
|
|
bbox = ann_i["bbox"]
|
|
iscrowd = ann_i["iscrowd"]
|
|
category_id = coco91class.index(ann_i["category_id"]) + 1
|
|
bbox.append(category_id)
|
|
if iscrowd!=0:continue
|
|
all_info[id]["bbox"].append(bbox)
|
|
|
|
gt = []
|
|
for k, info_i in all_info.items():
|
|
gt.append({'img_path': info_i['path'], 'bbox': info_i['bbox']})
|
|
|
|
|
|
with open(save_path, 'w') as f:
|
|
json.dump(gt, f)
|