当前位置:   article > 正文

【labelme】labelme的json转换txt_labelme json转txt

labelme json转txt

背景

因项目中使用labelme工具标注可控性比较强,效果可能会更好,就使用了labelme,但是生成出来的是json文件,因为yolov5使用的是txt文件,所以进行了处理

具体代码

import os
import numpy as np
import json


def json2txt(path_json, path_txt):
    with open(path_json, 'r', encoding='gb18030') as path_json:
        jsonx = json.load(path_json)
        with open(path_txt, 'w+') as ftxt:
            for shape in jsonx['shapes']:
                xy = np.array(shape['points'])
                label = str(shape['label'])
                if label == 'QB' :
                    label = '0'
                elif label =='MAM':
                    label = '1'
                else:
                    label = '2'
                label += ' '
                strxy = ''
                for m, n in xy:
                    strxy += str(m) + ' ' + str(n) + ' '
                label += strxy
                ftxt.writelines(label + "\n")


dir_json = '/home/hfg/Desktop/image/ng_labelme/json/'
dir_txt = '/home/hfg/Desktop/image/ng_labelme/txt/'
if not os.path.exists(dir_txt):
    os.makedirs(dir_txt)
list_json = os.listdir(dir_json)
for cnt, json_name in enumerate(list_json):
    print('cnt=%d,name=%s' % (cnt, json_name))
    path_json = dir_json + json_name
    path_txt = dir_txt + json_name.replace('.json', '.txt')
    print(path_json, path_txt)
    json2txt(path_json, path_txt)

  • 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

输出结果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/665814
推荐阅读
相关标签
  

闽ICP备14008679号