X-AnyLabeling转换为CVAT COCO1.0 格式

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)

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值