当前位置:   article > 正文

将LabelMe标注的json文件转成txt文件形式_lebelimg的json转txt

lebelimg的json转txt

一般只需要更改两处:1、自己的json文件存放路径 2、自己标注的类型名

import json
import os

"""
Computer: 2
Headdown: 1
Headup:   0
Text:     3
"""


def convert(img_size, box):
    x1 = box[0]
    y1 = box[1]
    x2 = box[2]
    y2 = box[3]

    center_x = (x1 + x2) * 0.5 / img_size[0]
    center_y = (y1 + y2) * 0.5 / img_size[1]
    w = abs((x2 - x1)) * 1.0 / img_size[0]
    h = abs((y2 - y1)) * 1.0 / img_size[1]

    return (center_x, center_y, w, h)


def decode_json(jsonfloder_path, json_name):
    txt_name = '/home/dxc/apple_datasets/Scifresh-apple/new_text_labels/' + json_name[0:-5] + '.txt'
    txt_file = open(txt_name, 'w')  # te files

    json_path = os.path.join(json_folder_path, json_name)
    data = json.load(open(json_path, 'r'))

    img_w = data['imageWidth']
    img_h = data['imageHeight']
    for i in data['shapes']:

        if (i['shape_type'] == 'rectangle'):

            x1 = int(i['points'][0][0])
            y1 = int(i['points'][0][1])
            x2 = int(i['points'][1][0])
            y2 = int(i['points'][1][1])
            if x1 < 0 or x2 < 0 or y1 < 0 or y2 < 0:
                continue
            else:
                bb = (x1, y1, x2, y2)
                bbox = convert((img_w, img_h), bb)
            if i['label'] == "0":                                                  # 修改自己标注的类型名
                 txt_file.write("0 " + " ".join([str(a) for a in bbox]) + '\n')
            elif i['label'] == "1":
                 txt_file.write("1 " + " ".join([str(a) for a in bbox]) + '\n')
            elif i['label'] == "2":
                 txt_file.write("2 " + " ".join([str(a) for a in bbox]) + '\n')
            else:
                 txt_file.write("3 " + " ".join([str(a) for a in bbox]) + '\n')


if __name__ == "__main__":

    json_folder_path = '/home/dxc/apple_datasets/Scifresh-apple/json_labels/'        # 修改自己的json文件存放位置
    json_names = os.listdir(json_folder_path)  # file name
    for json_name in json_names:  # output all files
        if json_name[-5:] == '.json':  # just work for json files
            decode_json(json_folder_path, json_name)
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/474285
推荐阅读
相关标签
  

闽ICP备14008679号