将scut-seg标签转化成通用coco标签

行人实例分割

import json
import os


def calculate_bounding_rectangle(coordinates):
    # 提取x和y坐标的列表
    x_coords = [coord[0] for coord in coordinates]
    y_coords = [coord[1] for coord in coordinates]

    # 计算矩形的左上角坐标
    min_x = min(x_coords)
    min_y = min(y_coords)

    # 计算矩形的宽度和高度
    max_x = max(x_coords)
    max_y = max(y_coords)
    width = max_x - min_x
    height = max_y - min_y

    return [min_x, min_y, width, height]

# scut-seg中的标签
folder_path = r"F:\mcj\AdelaiDet\datasets\dataset-scut\gt_instance\val"
#输出到保存位置
save_path = r"F:\mcj\AdelaiDet\datasets\dataset-scut\gt_instance\person_val_json"
#提取只有行人和骑车人的json文件
for file_name in os.listdir(folder_path):
    file_path = os.path.join(folder_path, file_name)
    if file_name.endswith(".json"):
        flag = False
        with open(file_path, "r") as load_f:
            new_dict = json.load(load_f)
            objects = new_dict['objects']
            for object in objects :
                if object['label'] == 'Person' or object['label'] == 'Rider':
                    flag = True

        if flag:
            target_file = os.path.join(save_path, file_name)
            with open(target_file,"w") as f:
                json.dump(new_dict, f)

print("step1 finish——提取只有行人和骑车人的json文件")

# 构建coco数据json中的字典模板
data = {
    'categories': [{"id": 1, "name": "person", "color": [128, 0, 0], "supercategory": ""}],
    'images': [],
    'annotations': [],
    'info': "info",
    'licenses': "licenses"
}
# 图片的标号 从1开始累加
image_id = 1
folder_path = r"F:\mcj\AdelaiDet\datasets\dataset-scut\gt_instance\person_val_json"
save_path = r"F:\mcj\AdelaiDet\datasets\dataset-scut\gt_instance\person_val_json"

# 每个标注对象的标号 从1开始累加(文件夹中所有标注对象)
annotation_id = 1
for file_name in os.listdir(folder_path):
    file_path = os.path.join(folder_path, file_name)
    image_name = file_name.replace("json", "jpg")
    temp = {
        'id': image_id,
        'width': 720,
        'height': 576,
        'file_name': image_name,
        'license': "",
        'flickr_url': "",
        'coco_url': "",
        'data_captured': ""
    }
    data['images'].append(temp)

    with open(file_path, "r") as load_f:
        new_dict = json.load(load_f)
        objects = new_dict['objects']
        for object in objects:
            if object['label'] == 'Person' or object['label'] == 'Rider':
                segmentation = object['polygon']
                t = []
                t1 = []
                for a in segmentation:
                    t.append((a['x'],a['y']))
                    t1.append(a['x'])
                    t1.append(a['y'])

                rectangle = calculate_bounding_rectangle(t)

                annotation = {
                    'iscrowd': 0,
                    'id': annotation_id,
                    'image_id': image_id,
                    'category_id': 1,
                    'segmentation': [],
                    'area': 0,
                    'bbox': rectangle
                }
                annotation['segmentation'].append(t1)
                annotation_id += 1
                data['annotations'].append(annotation)
    image_id += 1

print("step2 finish__转换成coco数据集格式")

save_path = r"F:\mcj\AdelaiDet\datasets\dataset-scut\gt_instance\person_val_coco"
target_file = os.path.join(save_path, "val.json")

with open(target_file, "w") as f:
    json.dump(data, f)

print("finish-转换成功")

你可能感兴趣的:(python,人工智能,计算机视觉,深度学习)