weixin_52631106 2023-08-23 20:13 采纳率: 0%
浏览 24
已结题

labelme_to_coco(多个json文件转换成一个)

我是用labelme标注的动物数据集生成多个身体关键点json文件,然後我想转换成coco格式并合并成一個类似animal_keypoints_train.json文件(类似下面的配置信息)

{"image_id": 1448, "bbox": [234, 11, 344, 248], "keypoints": [[298, 45, 1], [274, 49, 1], [300, 59, 1], [294, 29, 1], [259, 32, 1], [293, 150, 1], [256, 156, 1], [330, 166, 1], [307, 173, 1], [296, 207, 1], [267, 206, 1], [325, 212, 1], [0, 0, 0], [292, 239, 1], [261, 239, 1], [320, 232, 1], [292, 234, 1], [274, 106, 1], [0, 0, 0], [0, 0, 0]], "num_keypoints": 20, "category_id": 1}, {"image_id": 1449, "bbox": [140, 26, 500, 333], "keypoints": [[257, 130, 1], [167, 113, 1], [177, 176, 1], [338, 113, 1], [173, 76, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [262, 242, 1], [376, 168, 1], [0, 0, 0]], "num_keypoints": 20, "category_id": 1}, {"image_id": 1450, "bbox": [79, 1, 435, 334], "keypoints": [[336, 144, 1], [208, 124, 1], [298, 183, 1], [357, 101, 1], [147, 71, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [249, 318, 1], [0, 0, 0], [0, 0, 0]], "num_keypoints": 20, "category_id": 2}, {"image_id": 1451, "bbox": [110, 158, 299, 267], "keypoints": [[0, 0, 0], [279, 210, 1], [286, 220, 1], [266, 169, 1], [266, 187, 1], [0, 0, 0], [235, 253, 1], [173, 198, 1], [116, 242, 1], [0, 0, 0], [273, 246, 1], [0, 0, 0], [0, 0, 0], [0, 0, 0], [291, 246, 1], [0, 0, 0], [0, 0, 0], [250, 226, 1], [213, 214, 1], [128, 195, 1]], "num_keypoints": 20, "category_id": 2},
{"supercategory": "animal", "id": 4, "name": "horse", "keypoints": ["left_eye", "right_eye", "nose", "left_ear", "right_ear", "left_front_elbow", "right_front_elbow", "left_back_elbow", "right_back_elbow", "left_front_knee", "right_front_knee", "left_back_knee", "right_back_knee", "left_front_paw", "right_front_paw", "left_back_paw", "right_back_paw", "throat", "withers", "tailbase"], "skeleton": [[0, 1], [0, 2], [1, 2], [0, 3], [1, 4], [2, 17], [18, 19], [5, 9], [6, 10], [7, 11], [8, 12], [9, 13], [10, 14], [11, 15], [12, 16]]},

  • 写回答

10条回答 默认 最新

  • David1055259499 2023-08-23 21:35
    关注

    结全ChatGPT和我自己的想法:

    将从LabelMe标注生成的多个JSON文件转换为单个COCO格式的JSON文件,您可以按照以下步骤使用Python进行操作。我将为您提供一个脚本,该脚本会读取多个LabelMe JSON文件,提取相关信息,然后创建一个单独的COCO格式的JSON文件。

    假设您有一个包含LabelMe JSON文件的目录,以及您提供的COCO格式JSON模式,您可以使用以下脚本:

    import json
    import os
    
    # LabelMe JSON文件列表
    labelme_json_files = [
        'path/to/labelme_file1.json',
        'path/to/labelme_file2.json',
        # 根据需要添加更多文件路径
    ]
    
    # COCO格式骨架和关键点信息
    coco_keypoints_info = {
        # 在此处填写您的COCO关键点信息
    }
    
    # 初始化COCO注释格式
    coco_data = {
        "images": [],
        "annotations": [],
        "categories": [coco_keypoints_info],
    }
    
    # 加载并处理每个LabelMe JSON文件
    image_id_counter = 1
    annotation_id_counter = 1
    
    for labelme_file_path in labelme_json_files:
        with open(labelme_file_path, 'r') as f:
            labelme_data = json.load(f)
    
        # 提取图像信息
        image_info = {
            "id": image_id_counter,
            "width": labelme_data['imageWidth'],
            "height": labelme_data['imageHeight'],
            "file_name": os.path.basename(labelme_file_path).replace('.json', '.jpg'),
        }
        coco_data['images'].append(image_info)
    
        # 处理注释
        for annotation in labelme_data['shapes']:
            # 提取注释信息
            bbox = annotation['points']
            keypoints = []  # 从LabelMe格式提取关键点
            # 将关键点转换为COCO格式
    
            # 创建COCO注释
            coco_annotation = {
                "id": annotation_id_counter,
                "image_id": image_id_counter,
                "category_id": 1,  # 根据需要替换为适当的category_id
                "bbox": [bbox[0][0], bbox[0][1], bbox[1][0] - bbox[0][0], bbox[1][1] - bbox[0][1]],
                "keypoints": keypoints,
                "num_keypoints": len(keypoints) // 3,
            }
            coco_data['annotations'].append(coco_annotation)
    
            annotation_id_counter += 1
    
        image_id_counter += 1
    
    # 保存COCO格式JSON
    output_json_path = 'path/to/animal_keypoints_train.json'
    with open(output_json_path, 'w') as f:
        json.dump(coco_data, f, indent=4)
    
    
    

    请注意,您需要根据实际的目录结构、路径和数据格式调整此脚本。此外,请确保您根据LabelMe数据正确填充脚本中的keypoints列表。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月26日
  • 创建了问题 8月23日

悬赏问题

  • ¥15 matlab使用自定义函数时一直报错输入参数过多
  • ¥15 设计一个温度闭环控制系统
  • ¥100 关于加载卡的问题有能知道这个要怎么处理吗?
  • ¥100 rtmpose姿态评估
  • ¥15 java 通过反射找路径下的类,打包后就找不到
  • ¥15 通联支付网上收银统一下单接口
  • ¥15 angular有偿编写,
  • ¥15 centos7系统下abinit安装时make出错
  • ¥15 hbuildex运行微信小程序报错
  • ¥15 关于#python#的问题:我知道这个问题对你们来说肯定so easy