import json
import os
def read_xanylabeling_annotations(folder_path,coco_file_path):
new_data = {'images': [], 'annotations': [],'categories': []}
new_annotations=[]
new_images=[]
new_categories=[]
###############记录图像的标签
count=1
###############记录1张图像有多个目标框
flag=1
for filename in os.listdir(folder_path):
if filename.endswith(".json"): # 检查文件是否为.json文件
# 拼接文件完整路径
file_path = os.path.join(folder_path, filename)
# 在这里实现读取和解析X-AnyLabeling文件的逻辑
# 返回标注数据的字典,例如:{'images': [], 'annotations': [], 'categories': []}
with open(file_path, 'r') as f:
data = json.load(f)
file_name=data['imagePath']
height=data['imageHeight']
width=data['imageWidth']
id=count
new_images.append({
'id': id,
'file_name': file_name,
'height': height,
'width': width
})
if count ==1:
new_categories.append({
'id': 1,
'name': "person",
"supercategory": '' })
new_categories.append({
'id': 2,
'name': "pallet",
"supercategory": '' })
for ann in data['shapes']:
area=(ann['points'][1][0]-ann['points'][0][0])*(ann['points'][2][1]-ann['points'][0][1])
print("area:"+str(area))
bbox=[]
bbox.append(ann['points'][0][0])
bbox.append(ann['points'][0][1])
bbox.append(ann['points'][2][0])
bbox.append(ann['points'][2][1])
print(bbox)
if ann['label']=="person":
category_id=1
elif ann['label']=="pallet":
category_id=2
else:
print("------------------")
id=flag
image_id=count
iscrowd=False
segmentation=[]
new_annotations.append({
'area': area,
'bbox': bbox,
'category_id': category_id,
'id': id,
'image_id':image_id,
'iscrowd':iscrowd,
'segmentation':[]
})
flag=flag+1
count=count+1
new_data['annotations']=new_annotations
new_data['images']=new_images
new_data['categories']=new_categories
with open(coco_file_path, 'w') as f:
json.dump(new_data, f)
folder_path="/mnt/d/09AnyLabeling/DataCreate/coco/combinedjson"
coco_file_path="/mnt/d/09AnyLabeling/DataCreate/coco/instances_train2017_person.json"
read_xanylabeling_annotations(folder_path,coco_file_path)