COCO格式json切分为labelme可识别json

coco数据格式相关内容参考之前博客

切分的关键在于将coco_json中的annotation信息转化为labelme中shape的坐标信息

labelme中shape需要的是多边形的点坐标,存储格式为[[x1,y1], [x2,y2]......]

import os 
import json
import pycocotools.mask as mask_utils
from pycocotools.coco import COCO
import cv2

json_path = ''

# 读取json(这里重复读取了,懒得改)
with open(json_path, 'r') as f:
    coco_data = json.load(f)
coco = COCO(json_path)

output_json = ''
os.makdirs(output_json, exist_ok=True)

for image_data in coco_data['images']:
    image_id = image_data['id']
    image_file_name = image_data['file_name']

    # 创建labelme的json数据结构,这里也可以读一个labelme的json直接替换
    labelme_data = {
        'version': '4.5.7',
        'flags':{},
        'shapes':[],
        'imagePath':image_file_name,
        'imageData':None,
        'imageHeight':image_data['height'],
        'imageWidth':image_data['width']
    }
    
    # 查找当前图像的标签数据
    for annotation in coco_data['annotations']:
        if annotation['image_id'] == image_id:
            category_id = annotation['category_id']
            
            # 构建labelme多边形点
            rle = coco.annToRLE(annotation)
            mask = mask_utils.decode(rle)
            mask[mask == 1] = 255
            
            # mask轮廓
            contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            for contour in contours:
                if len(contour) < 3:
                    continue
                seg_xy = [[int(x), int(y)] for x,y in contour.reshape(-1, 2)]

            # 创建labelme的shape结构
            shape = {
                'label': str(category_id),
                'points': seg_xy,
                'group_id': None,
                'shape_type': 'polygon',
                'flags': {}
            }
            # 将shape添加到labelme结构中
            labelme_data['shapes'].append(shape)
    # 将labelme结构写入json文件中
    labelme_json_file = os.path.join(output_json, os.path.splitext(image_file_name)[0]+'.json')

    with open(labelme_json_file, 'w') as labelme_f:
        json.dump(labelme_data, labelme_f, indent=2)

你可能感兴趣的:(Working,python,人工智能)