import os
import cv2
import json
data_dir = '/home/yxq/桌面/falunwen/submit_test'
annotations_info = {'images': [], 'annotations': [], 'categories': []}
categories_map = {'Aortic_enlargement': 1, 'Atelectasis': 2, 'Calcification': 3, 'Cardiomegaly': 4,
'Consolidation': 5, 'ILD': 6, 'Infiltration': 7, 'Lung_Opacity': 8,
'Nodule': 9, 'Other_lesion': 10, 'Pleural_effusion': 11, 'Pleural_thickening': 12,
'Pneumothorax': 13, 'Pulmonary_fibrosis': 14}
for key in categories_map:
categoriy_info = {"id": categories_map[key], "name": key}
annotations_info['categories'].append(categoriy_info)
file_names = [image_file_name.split('.')[0]
for image_file_name in os.listdir(data_dir)]
ann_id = 1
for i, file_name in enumerate(file_names):
image_file_name = file_name + '.png'
xml_file_name = file_name + '.xml'
image_file_path = os.path.join(data_dir, image_file_name)
image_info = dict()
image = cv2.cvtColor(cv2.imread(image_file_path), cv2.COLOR_BGR2RGB)
height, width, _ = image.shape
image_info = {'file_name': image_file_name, 'id': file_name, "image_id": file_name,
'height': height, 'width': width}
annotations_info['images'].append(image_info)
with open('test.json', 'w') as f:
json.dump(annotations_info, f, indent=4)
print('---整理后的标注文件---')
print('所有图片的数量:', len(annotations_info['images']))
print('所有类别的数量:', len(annotations_info['categories']))