• 将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-转换成功")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
  • 相关阅读:
    Java计算机网络篇-HTTP
    Leetcode刷题笔记5
    AD360荣获2023 Fortress奖:卓越的身份验证和身份管理解决方案
    ssm项目启动,控制台tomcat相关日志乱码
    MH/T 6040航空材料烟密度试验
    SpringBoot属性说明与RESTful开发
    【EAI 026】RoboGen: 通过自动数据生成管线实现机器人技能学习
    python类属性和实例属性的注意点
    国际土壤模型协会 International Soil Modeling Consortium-ISMC
    python操作excel中xlrd模块的一些简单方法
  • 原文地址:https://blog.csdn.net/weixin_45745434/article/details/134427312