提示:本文用于学习记录
文章目录
- 前言
- 一、样例1
- 二、样例2
- 三、样例3
- 总结
前言
将 json 文件转化为 txt 文件的几种情况
一、样例1
数据集结构(batch 中存放图片)
json 文件内容如下:
代码转化前先创建所需 labels 文件夹与 batch_1~batch_15 文件夹
代码转化如下:
from pycocotools.coco import COCO import numpy as np import tqdm import argparse def arg_parser(): parser = argparse.ArgumentParser('code by rbj') parser.add_argument('--annotation_path', type=str, default='data/garbage1/annotations.json') #生成的txt文件保存的目录 parser.add_argument('--save_base_path', type=str, default='data/garbage1/labels/') args = parser.parse_args() return args if __name__ == '__main__': args = arg_parser() annotation_path = args.annotation_path save_base_path = args.save_base_path data_source = COCO(annotation_file=annotation_path) catIds = data_source.getCatIds() categories = data_source.loadCats(catIds) categories.sort(key=lambda x: x['id']) classes = {} coco_labels = {} coco_labels_inverse = {} for c in categories: coco_labels[len(classes)] = c['id'] coco_labels_inverse[c['id']] = len(classes) classes[c['name']] = len(classes) img_ids = data_source.getImgIds() for index, img_id in tqdm.tqdm(enumerate(img_ids), desc='change .json file to .txt file'): img_info = data_source.loadImgs(img_id)[0] file_name = img_info['file_name'].split('.')[0] height = img_info['height'] width = img_info['width'] save_path = save_base_path + file_name + '.txt' with open(save_path, mode='w') as fp: annotation_id = data_source.getAnnIds(img_id) boxes = np.zeros((0, 5)) if len(annotation_id) == 0: fp.write('') continue annotations = data_source.loadAnns(annotation_id) lines = '' for annotation in annotations: box = annotation['bbox'] # some annotations have basically no width / height, skip them if box[2] < 1 or box[3] < 1: continue #top_x,top_y,width,height---->cen_x,cen_y,width,height box[0] = round((box[0] + box[2] / 2) / width, 6) box[1] = round((box[1] + box[3] / 2) / height, 6) box[2] = round(box[2] / width, 6) box[3] = round(box[3] / height, 6) label = coco_labels_inverse[annotation['category_id']] lines = lines + str(label) for i in box: lines += ' ' + str(i) lines += 'n' fp.writelines(lines) print('finish')
二、样例2
数据集结构(转换单个 json 文件)
json 文件内容如下:
{ "exist": [1,1,1,1,1,1,1,1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,35], [300,248,40,35], [300,251,42,31], [301,249,40,35], [300,251,39,32], [299,251,41,32], [301,251,41,34], [300,251,41,32], [301,252,39,31], [301,250,40,35], [299,250,43,32], [298,251,43,32], [298,251,44,33], [297,250,42,34], [298,248,40,35], [299,248,40,35], [297,250,42,33], [296,250,46,34], [298,251,42,30], [297,250,42,34], [296,251,39,32], [296,250,39,33], [297,251,40,32], [296,249,41,34], [295,250,44,33], [294,249,42,34], [295,250,41,33], [295,248,41,36], [294,252,41,32], [295,249,39,34], [291,251,44,33], [292,251,42,31], [293,251,41,32], [293,251,42,32], [293,251,41,32], [293,252,43,31], [293,252,41,31], [295,252,41,31], [295,252,40,32], [293,250,43,34], [294,252,42,32], [293,252,43,32], [294,252,40,33], [293,254,41,32], [293,253,43,32], [294,253,43,34], [296,253,41,33], [296,254,41,31], [297,254,41,32], [297,254,42,32], [298,254,40,33], [298,255,40,31], [299,255,39,30], [299,254,37,31], [297,253,40,33], [298,254,38,30], [299,253,37,32], [300,253,37,32], [300,253,36,32], [300,253,38,31], [300,253,37,30], [300,253,36,29], [300,252,37,31], [300,253,38,30], [301,252,40,30], [301,253,38,29], [302,252,37,31], [303,253,36,29], [302,253,37,30], [302,251,36,31], [302,252,37,30], [302,252,38,30], [303,252,36,31], [302,252,37,30], [303,253,36,30], [302,252,38,32], [303,254,37,30], [303,253,35,32], [303,252,35,32], [301,252,38,32], [301,254,37,29], [300,253,36,32], [301,253,35,31], [300,253,37,30], [299,253,36,30], [298,252,37,30], [298,253,36,29], [296,251,35,30], [296,250,36,32], [295,251,37,31], [293,249,38,34], [292,250,36,31], [292,250,36,31], [291,249,36,32], [289,250,38,31], [287,250,41,31], [289,250,37,32], [288,249,37,33], [289,250,37,32], [289,249,36,33], [290,250,37,32], [290,249,39,33], [291,249,37,33], [291,250,39,32], [291,249,40,31], [292,248,38,33], [292,249,37,32], [291,248,38,34], [292,250,40,31], [292,249,40,31], [292,249,40,33], [293,250,38,32], [295,251,37,30], [297,250,38,32], [299,250,39,32], [299,251,38,32], [301,251,39,30], [302,250,37,32], [302,250,38,32], [301,250,38,31], [302,251,39,31], [301,251,40,32], [302,250,37,31], [302,251,37,31], [302,251,37,30], [301,251,38,30], [300,250,40,33], [301,250,40,32], [300,252,38,30], [300,251,38,31], [300,250,39,32], [299,251,39,30], [301,250,37,33], [301,250,36,30], [301,251,35,29], [301,250,35,30], [301,250,38,30], [301,249,36,31], [300,249,37,31], [301,248,37,32], [300,249,37,31], [300,249,39,30], [300,248,36,31], [299,249,38,30], [298,248,39,31], [299,249,37,31], [299,248,36,31], [299,248,37,31], [299,249,37,30], [299,248,36,32], [299,248,36,32], [299,249,36,30], [298,248,37,33], [299,248,38,31], [299,248,36,32], [297,249,38,32], [297,249,38,31], [297,249,37,31], [296,249,38,30], [296,248,37,31], [296,247,36,32], [296,247,36,32], [295,246,38,32], [295,246,38,31], [294,242,39,33], [295,244,38,31], [296,241,36,32], [295,240,36,32], [295,239,39,33], [296,239,37,31], [295,238,38,31], [296,239,37,29], [297,237,37,30], [296,235,37,31], [296,235,37,30], [296,237,39,31], [296,237,39,30], [296,241,38,30], [296,246,38,30], [297,249,38,32], [298,250,36,31], [298,255,37,30], [297,257,39,32], [298,258,38,30], [298,258,39,29], [298,260,38,30], [299,260,38,30], [299,261,41,30], [299,259,43,32], [300,259,42,32], [300,257,40,32], [298,255,42,33], [299,253,42,32], [300,252,40,32], [301,249,41,34], [300,250,40,32], [300,247,42,34], [301,248,39,33], [301,247,41,33], [301,247,41,33], [301,247,40,33], [298,246,44,34], [297,247,48,33], [296,247,47,32], [294,247,49,32], [297,247,44,34], [296,247,47,33], [298,246,43,33], [293,244,49,34], [295,244,47,35], [296,244,44,35], [297,246,43,33], [296,246,44,32], [295,242,44,36], [294,242,45,36], [294,243,44,34], [293,243,47,35], [293,244,46,34], [291,243,49,36], [291,245,48,34], [292,245,48,35], [292,247,47,34], [292,248,48,34], [292,248,46,33], [293,248,45,33], [292,247,47,35], [292,247,48,36], [292,248,49,35], [292,248,46,34], [292,248,47,34], [293,247,46,36], [292,248,47,34], [293,248,47,35], [291,249,49,33], [293,248,48,35], [293,249,48,35], [294,250,46,34], [294,250,46,34], [293,250,48,35], [293,250,48,35], [294,250,47,35], [293,250,47,36], [294,251,48,35], [293,249,48,36], [294,250,48,35], [293,250,49,34], [293,249,51,34], [295,248,49,34], [294,248,49,35], [295,247,48,35], [296,246,48,36], [297,246,47,36], [298,246,46,36], [297,246,48,35], [297,247,47,34], [297,247,47,33], [296,246,48,34], [297,245,48,35], [297,246,48,34], [297,247,48,34], [298,245,48,36], [297,245,49,35], [298,246,48,34], [297,246,49,34], [297,245,50,36], [298,246,49,35], [296,245,50,35], [297,245,51,37], [297,246,50,35], [297,246,50,36], [296,246,52,36], [297,247,50,36], [297,246,50,37], [296,247,50,36], [295,247,48,37], [295,247,48,37], [294,248,49,36], [294,248,47,36], [292,247,49,37], [293,248,47,36], [293,247,47,37], [293,247,48,38], [292,248,47,37], [293,249,47,35], [293,248,47,36], [292,247,47,38], [291,246,52,37], [291,246,46,37], [291,247,47,36], [291,247,48,36], [291,247,48,36], [291,245,48,39], [291,247,46,35], [292,244,46,39], [292,244,46,40], [291,245,49,39], [291,247,46,37], [291,245,49,39], [291,245,47,39], [291,246,48,38], [290,247,48,38], [291,246,48,39], [290,246,50,39], [291,246,47,39], [290,245,49,41], [290,246,50,39], [291,246,47,39], [291,246,50,40], [291,247,49,40], [292,247,47,40], [290,247,50,41], [290,247,51,42], [290,247,49,41], [292,249,45,39], [291,249,46,38], [291,247,46,41], [291,250,46,37], [291,250,46,39], [289,249,48,39], [290,252,44,36], [289,251,47,36], [289,250,47,37], [289,251,45,36], [291,251,43,36], [290,252,44,35], [290,253,44,34], [290,253,43,34], [290,253,43,33], [289,252,44,33], [291,253,43,32], [292,250,41,35], [291,250,42,35], [292,252,42,33], [292,251,43,34], [293,250,42,35], [292,251,43,34], [294,251,41,33], [292,250,42,36], [294,248,39,34], [293,249,40,34], [296,248,42,33], [296,247,44,33], [300,247,40,32], [300,246,40,33], [302,246,41,33], [303,245,41,34], [304,247,41,32], [303,247,43,33], [303,246,42,34], [303,247,42,33], [303,248,41,32], [305,248,38,32], [303,247,38,34], [302,249,40,33], [300,248,42,34], [300,249,40,36], [299,249,40,34], [298,251,38,34], [296,251,40,33], [297,252,41,32], [298,253,39,31], [298,254,37,31], [298,253,37,31], [296,253,40,32], [298,255,35,29], [296,252,37,33], [296,254,34,31], [295,253,38,32], [294,254,36,30], [295,254,34,30], [295,254,32,30], [293,255,35,30], [293,254,35,30], [295,255,32,30], [294,255,33,29], [295,255,35,29], [294,255,35,30], [295,255,34,29], [295,254,35,29], [297,253,35,31], [298,253,35,30], [300,254,33,28], [301,254,34,28], [302,252,34,30], [303,254,34,28], [303,254,34,29], [304,253,34,29], [305,254,32,27], [306,254,31,28], [305,253,33,28], [304,252,33,29], [305,253,31,28], [304,252,34,29], [302,252,38,29], [301,252,36,27], [300,252,37,27], [302,252,32,28], [302,251,32,28], [300,252,34,26], [301,251,34,29], [301,251,34,28], [300,251,34,26], [300,249,34,29], [301,250,34,27], [301,251,33,27], [299,250,34,28], [300,252,31,25], [300,251,31,27], [301,252,30,26], [300,252,31,27], [300,253,31,26], [300,253,31,27], [299,254,32,25], [301,254,30,26], [299,254,32,25], [301,254,31,27], [301,255,30,25], [300,254,31,26], [300,253,33,27], [300,254,33,26], [300,254,34,26], [302,254,32,26], [301,254,33,26], [303,254,33,27], [301,254,34,27], [302,255,30,26], [302,255,31,26], [302,255,31,25], [302,256,31,25], [303,255,29,25], [303,255,28,25], [304,254,29,26], [304,255,29,24], [303,254,29,25], [304,254,30,26], [303,253,30,26], [305,254,30,24], [305,253,29,25], [305,254,30,24], [305,255,30,24], [305,254,29,25], [305,255,29,23], [306,255,28,24], [305,255,29,25], [305,255,29,24], [305,254,29,25], [305,254,30,25], [306,254,28,24], [306,254,28,25], [306,254,30,25], [306,255,29,24], [306,256,29,24], [306,256,29,24], [307,256,27,25], [306,257,29,24], [307,258,27,24], [307,258,28,23], [306,258,29,23], [306,258,27,23], [306,257,28,24], [305,258,29,23], [306,257,27,24], [305,256,30,25], [306,256,29,25], [306,256,27,25], [304,257,29,23], [304,256,28,24], [304,257,28,24], [301,256,27,25], [300,256,29,25], [298,255,29,25], [297,257,28,24], [297,256,28,25], [295,258,29,23], [295,256,30,25], [294,257,30,24], [295,258,29,23], [296,257,27,24], [296,256,29,25], [298,257,27,24], [300,257,26,24], [300,257,27,25], [301,257,28,24], [302,257,30,23], [302,257,29,24], [305,257,28,23], [308,257,27,22], [308,257,28,22], [308,256,27,23], [310,255,25,24], [310,255,26,23], [311,256,26,22], [311,256,25,22], [311,256,26,23], [311,257,26,22], [311,256,26,23], [312,256,25,22], [310,256,27,23], [312,257,25,23], [312,257,26,22], [312,257,26,23], [312,257,25,23], [311,256,26,24], [310,256,26,25], [310,256,25,23], [309,256,26,24], [307,257,25,22], [304,257,25,23], [304,257,25,22], [302,257,25,22], [297,256,27,24], [298,258,24,22], [295,257,25,23], [295,257,24,22], [294,258,25,22], [292,257,24,21], [291,258,24,22], [291,258,25,22], [291,258,24,22], [291,258,24,22], [293,259,25,21], [295,258,25,23], [298,257,24,23], [299,258,24,22], [299,258,24,21], [299,258,23,21], [299,258,23,22], [300,258,22,22], [299,258,24,22], [300,258,23,22], [301,258,24,22], [303,258,24,22], [306,258,24,21], [309,258,22,21], [309,257,23,22], [310,258,23,21], [310,257,23,22], [310,258,23,21], [310,258,23,21], [309,257,24,21], [310,258,23,21], [309,257,25,22], [310,257,23,22], [310,257,24,22], [310,258,24,21], [310,258,24,21], [311,258,22,21], [310,258,24,21], [310,258,24,22], [310,258,24,21], [309,259,24,21], [309,258,24,21], [309,259,23,21], [309,259,24,21], [308,258,24,21], [307,259,24,21], [306,258,24,21], [307,259,22,20], [306,259,23,20], [306,258,23,23], [304,258,25,22], [304,259,25,21], [305,258,24,22], [304,258,26,22], [304,258,25,22], [305,258,24,22], [304,258,25,22], [304,257,26,23], [305,258,24,21], [305,258,23,21], [305,258,24,21], [304,258,24,21], [305,257,24,22], [304,258,24,21], [304,258,25,21], [305,257,23,22], [305,257,23,22], [305,257,23,22], [305,258,22,20], [305,258,22,21], [305,258,23,20], [305,258,24,20], [305,258,23,21], [305,258,22,20], [304,258,24,20], [304,258,23,20], [304,258,23,20], [304,258,23,19], [305,259,22,20], [305,259,22,19], [305,259,22,18], [305,258,22,19], [305,259,22,19], [306,259,21,18], [306,258,21,19], [306,258,21,18], [306,257,20,21], [306,259,21,19], [305,259,23,20], [306,260,22,21], [306,260,21,20], [307,260,21,21], [307,261,22,20], [306,262,22,19], [307,262,22,19], [308,262,21,18], [308,262,20,19], [307,262,22,19], [307,262,23,19], [307,262,22,18], [308,262,22,19], [307,261,22,19], [309,261,21,20], [308,262,23,19], [308,262,23,19], [308,262,23,19], [309,262,20,20], [308,262,22,20], [308,262,24,20], [309,262,22,20], [309,262,22,19], [309,262,23,20], [308,262,23,19], [308,262,23,19], [308,262,24,19], [309,261,22,19], [308,261,22,19], [308,261,23,20], [306,261,25,19], [308,260,22,19], [307,261,22,19], [306,261,24,19], [307,262,22,18], [307,262,22,18], [307,261,22,19], [307,262,21,18], [306,261,21,19], [306,262,22,19], [306,262,22,18], [306,261,22,18], [307,262,20,17], [305,262,22,18], [306,261,21,18], [304,261,23,18], [304,260,22,19], [305,260,21,18], [305,260,21,18], [304,260,21,17], [305,259,22,19], [305,259,22,18], [306,258,19,20], [306,260,21,18], [306,261,20,18], [306,261,20,19], [306,262,20,18], [305,262,20,19], [305,263,21,18], [306,263,19,18], [305,263,19,17], [305,262,20,19], [306,263,20,18], [306,263,20,18], [306,263,21,18], [306,263,21,18], [306,264,22,17], [305,263,21,18], [306,263,22,18], [306,263,21,18], [306,263,21,18], [306,263,21,17], [306,263,21,18], [306,263,22,18], [307,262,21,18], [307,262,22,19], [309,262,20,18], [310,262,20,18], [312,262,19,18], [311,262,21,18], [312,262,20,17], [312,260,20,19], [312,262,21,19], [312,260,21,20], [312,261,21,19], [311,261,21,19], [311,262,22,18], [312,262,19,17], [311,261,20,17], [311,261,20,18], [310,261,21,19], [309,261,22,18], [308,260,22,19], [308,261,21,18], [308,261,21,18], [308,260,21,18], [309,261,19,18], [309,261,19,17], [308,261,20,17], [308,261,21,17], [308,261,20,18], [307,261,20,18], [306,262,19,17], [306,262,20,17], [305,263,19,16], [304,262,20,18], [304,262,19,18], [304,262,19,17], [303,262,20,17], [303,262,19,17], [303,262,19,18], [303,262,19,18], [304,263,18,16], [304,263,18,16], [304,262,19,17], [305,263,19,16], [306,263,19,16], [307,263,19,17], [308,263,20,16], [307,262,20,17], [308,263,22,16], [311,263,19,17], [310,263,19,17], [311,262,20,18], [312,263,19,17], [311,263,20,17], [310,261,20,18], [311,262,20,18], [312,263,20,16], [315,263,19,16], [313,262,21,17], [316,262,21,18], [318,262,20,17], [317,262,21,18], [319,262,19,18], [318,262,21,18], [319,262,20,17], [319,262,19,18], [319,262,19,16], [318,263,20,17], [317,262,20,18], [317,263,20,17], [315,262,21,18], [312,263,20,16], [312,263,21,16], [309,263,21,16], [307,263,19,16], [303,263,20,16], [303,262,19,18], [300,262,19,17], [297,263,20,17], [293,263,21,17], [294,263,20,17], [292,263,20,17], [291,263,20,18], [290,263,21,17], [291,263,20,17], [293,263,19,16], [295,262,21,17], [300,261,20,18], [299,262,21,16], [306,261,21,16], [313,262,18,15], [312,262,20,15], [318,262,19,15], [322,261,19,15], [327,261,19,17], [327,261,20,17], [330,261,18,17], [332,261,19,16], [334,261,19,18], [334,261,19,17], [335,261,19,17], [335,261,19,17], [334,262,18,17], [334,262,18,16], [331,262,18,16], [327,261,18,17], [327,263,18,15], [320,262,19,17], [314,263,19,16], [308,263,19,16], [308,263,19,16], [303,263,17,16], [298,263,18,16], [297,263,19,16], [293,263,18,17], [289,263,19,17], [288,263,18,17], [288,263,18,18], [288,264,18,17], [288,264,18,17], [289,264,18,16], [289,264,17,16], [291,264,17,16], [294,264,18,15], [298,264,18,15], [298,264,19,15], [302,264,19,15], [307,263,18,15], [307,264,18,15], [309,263,19,16], [310,262,19,16], [310,262,19,16], [311,261,18,17], [310,262,18,16], [309,262,18,15], [309,262,17,15], [308,262,18,16], [307,262,18,16], [307,262,16,15], [306,262,17,16], [306,262,17,16], [306,262,16,16], [306,263,17,15], [305,262,18,16], [305,263,17,15], [305,263,17,16], [305,264,17,15], [305,263,17,16], [304,264,18,13], [305,263,17,16], [305,263,18,15], [305,263,17,16], [306,263,16,15], [306,262,17,16], [307,263,16,16], [306,263,17,16], [306,263,17,16], [307,263,16,16], [306,262,16,16], [306,263,17,16], [307,263,15,15], [307,262,16,17], [306,263,18,16], [307,264,17,14], [307,263,17,15], [308,264,17,15], [308,264,17,15], [308,263,17,16], [309,264,18,14], [310,263,17,16], [310,263,17,16], [310,263,16,16], [311,263,16,15], [310,264,17,14], [311,264,17,14], [309,263,17,16], [311,266,16,14], [310,266,17,15], [311,266,17,15], [312,267,15,15], [312,268,16,15], [311,268,17,15], [312,268,17,15], [312,267,17,15], [312,267,17,15], [312,267,17,15], [313,267,16,16], [313,267,17,15], [313,267,16,15], [312,267,17,14], [313,267,16,14], [312,266,16,16], [313,266,16,15], [313,266,16,15], [312,266,18,15], [312,266,17,15], [312,266,17,16], [312,266,16,15], [311,266,18,15], [311,265,16,15], [311,265,17,15], [310,265,16,16], [308,264,18,15], [307,263,16,16], [306,264,16,15], [304,264,17,14], [302,263,17,15], [302,263,17,15], [300,263,17,16], [298,263,17,16], [297,264,18,14], [297,263,17,16], [297,263,18,16], [297,264,17,16], [298,264,17,16], [298,264,18,16], [299,265,17,16], [303,267,18,14], [308,268,19,13], [308,268,19,12], [315,268,20,12], [321,268,19,12], [327,268,19,14], [327,268,17,13], [330,267,19,13], [333,267,18,13], [334,267,17,14], [334,266,17,15], [334,266,16,14], [332,266,18,15], [329,266,19,15], [329,266,18,15], [325,266,18,14], [320,266,18,15], [320,266,18,16], [315,266,19,15], [311,266,18,15], [309,266,18,14], [308,266,19,15], [308,266,19,15], [310,266,19,15], [313,265,18,15], [314,264,19,15], [314,264,19,15], [317,263,19,14], [319,262,18,13], [322,260,18,14], [321,260,19,14], [323,259,20,13], [326,259,18,14], [327,259,19,14], [327,259,19,15], [327,258,19,15], [328,258,19,15], [328,258,19,15], [328,258,19,14], [328,258,20,15], [325,258,18,15], [320,259,19,15], [315,258,20,16], [315,259,20,15], [312,258,20,17], [307,259,21,15], [307,259,20,16], [304,258,19,15], [302,258,20,16], [298,259,19,15], [298,258,20,16], [294,258,20,17], [292,260,19,16], [292,260,20,16], [291,262,19,15], [291,262,20,18], [293,265,19,16], [293,264,21,17], [294,264,20,17], [295,263,20,18], [296,263,20,18], [296,263,20,18], [296,264,20,17], [296,264,20,16], [297,264,20,17], [297,264,20,16], [298,264,19,16], [300,263,19,16], [300,263,21,16], [301,263,19,17], [303,263,18,16], [304,262,19,17], [304,262,19,17], [305,262,19,16], [306,262,19,16], [306,263,19,16], [306,262,20,16], [306,263,19,16], [306,263,20,16], [307,263,19,17], [307,263,19,17], [306,263,21,17], [306,263,21,17], [306,262,20,18], [306,263,21,17], [306,263,20,17], [306,263,20,17], [307,262,20,18], [307,263,20,17], [307,263,20,17], [306,263,20,17], [306,263,20,18], [305,263,21,17], [305,262,21,18], [306,262,20,18], [306,262,20,19], [306,263,20,18], [307,262,20,19], [306,262,21,19], [307,262,19,19], [306,262,22,19], [305,263,23,18], [305,263,23,19], [305,262,23,19], [304,262,25,18], [304,261,24,21], [304,261,24,20], [304,262,25,19], [305,262,23,20], [304,262,24,20], [304,263,25,18], [305,263,25,19], [305,263,25,18], [306,262,25,21], [306,263,25,19], [306,263,25,19], [307,262,24,20], [305,263,25,18], [304,263,26,19], [306,262,25,20], [306,263,24,19], [306,263,25,19], [305,262,25,19], [305,264,27,18], [305,262,26,21], [307,264,24,19], [307,262,24,21], [306,262,26,22], [305,263,27,21], [305,264,26,21], [305,262,28,23], [304,261,29,23], [305,261,27,23], [305,261,27,22], [305,261,27,22], [304,261,30,22], [303,261,29,22], [304,261,26,21], [304,260,28,22], [302,257,29,24], [303,258,29,24], [301,259,30,22], [300,259,30,22], [303,259,28,22], [303,258,28,23], [302,257,30,23], [300,256,32,24], [302,256,30,25], [299,257,33,25], [299,257,32,25], [301,258,30,25], [299,256,32,25], [299,257,30,26], [299,257,32,25], [300,258,31,23], [299,255,31,26], [301,257,30,25], [300,257,30,24], [299,257,31,24], [300,256,30,25], [300,257,30,23], [299,255,32,26], [299,256,32,25], [299,255,32,26], [301,257,30,23], [301,256,29,25], [300,256,32,25], [301,257,31,24], [300,257,32,25], [303,256,30,25], [304,256,30,26], [302,258,34,24], [306,258,30,22], [306,259,31,22], [306,258,30,23], [306,258,30,23], [306,258,30,23], [306,259,30,23], [305,259,31,23], [306,259,29,23], [304,258,33,24], [304,260,33,22], [303,259,33,23], [305,258,30,24], [303,258,31,24], [303,260,31,22], [303,259,31,23], [301,258,31,23], [302,258,31,24], [301,256,31,25], [302,257,31,25], [302,258,31,22], [302,256,30,25], [302,257,32,24], [302,259,31,24], [302,262,31,23], [302,262,30,23], [302,263,30,24], [302,265,31,23], [302,264,32,25], [300,266,32,23], [301,266,32,23], [300,267,31,23], [298,265,34,25], [299,268,31,22], [300,268,31,23], [300,266,31,24], [300,268,32,22], [299,266,32,24], [299,266,34,23], [298,267,33,22], [300,265,31,24], [301,265,33,24], [301,263,34,25], [301,265,33,23], [301,262,31,26], [301,264,33,24], [301,263,32,26], [302,264,33,22], [301,263,33,23], [302,260,32,25], [302,261,32,23], [303,261,31,25], [305,262,29,23], [306,262,32,23], [306,262,31,23], [309,261,30,23], [309,262,32,23], [310,261,32,23], [311,260,30,24], [312,261,30,23], [312,260,28,24], [312,261,30,22], [311,260,30,23], [312,261,30,23], [310,260,32,24], [312,260,31,24], [310,261,32,24], [309,261,32,24], [307,262,30,23], [306,262,34,24], [306,262,33,24], [303,262,33,25], [303,263,32,23], [302,262,31,25], [300,262,32,24], [297,262,34,26], [297,265,34,22], [294,264,32,23], [294,264,34,23], [292,263,35,24], [291,264,33,24], [291,264,31,25], [291,266,32,23], [289,266,34,23], [287,266,35,22], [288,265,34,24], [287,266,34,23], [288,265,32,23], [286,265,34,23], [286,265,35,22], [287,265,32,22], [288,264,32,23], [290,263,33,24], [292,264,34,24], [292,264,34,23], [296,264,33,23], [302,264,30,22], [305,263,32,23], [305,264,32,21], [310,263,30,22], [312,263,31,22], [314,263,31,21], [313,262,31,23], [315,262,33,23], [317,262,33,24], [318,262,33,24], [319,262,32,23], [319,262,32,23], [319,262,32,24], [318,263,32,22], [318,262,33,24], [319,263,30,24], [318,264,32,22], [317,265,31,21], [317,263,32,24], [315,264,32,22], [315,264,31,22], [314,264,31,22], [313,264,31,24], [308,265,35,21], [305,264,33,22], [304,265,32,21], [296,265,36,22], [292,265,32,22], [288,264,32,22], [288,265,34,22], [285,265,32,23], [278,266,36,21], [275,265,35,22], [275,265,34,24], [273,266,34,22], [274,264,33,24], [273,266,33,22], [275,264,32,24], [277,264,34,24], [283,265,31,23], [282,265,33,24], [285,264,32,23], [289,264,32,24], [290,265,30,23], [292,264,32,24], [296,264,32,23], [299,264,33,23], [299,264,33,23], [304,263,30,24], [307,264,32,23], [311,263,33,23], [311,263,33,23], [316,264,32,22], [320,264,33,21], [324,264,32,22], [326,264,31,21], [326,264,33,22], [327,264,33,23], [328,264,31,24], [323,264,33,22], [323,262,33,25], [320,264,34,25], [314,264,31,25], [313,264,33,24], [305,265,30,21], [295,263,33,27], [285,266,30,22], [287,266,29,22], [278,265,29,24], [270,266,30,24], [264,267,33,23], [263,266,33,23], [263,267,33,23], [266,268,30,22], [265,268,30,21], [267,267,33,23], [271,267,31,23], [275,266,31,23], [276,267,30,23], [285,266,28,23], [295,266,30,23], [303,264,31,25], [303,265,31,24], [312,264,31,24], [318,264,28,23], [319,263,29,23], [319,264,30,23], [318,263,31,23], [316,262,30,23], [316,262,30,23], [314,262,32,23], [314,261,29,23], [314,261,29,22], [314,261,30,22], [314,259,30,24], [317,259,28,24], [318,258,28,25], [317,260,28,22], [316,260,29,23], [315,260,29,23], [313,261,29,22], [309,261,30,23], [310,261,29,23], [296,263,32,24], [282,263,33,24], [267,264,33,25], [267,264,32,24], [255,266,32,24], [244,268,31,23], [243,269,33,22], [232,270,31,23], [221,272,32,22], [207,273,32,23], [207,273,31,23], [194,274,31,24], [183,275,30,23], [183,275,30,23], [189,273,33,22], [211,271,33,22], [234,269,35,23], [234,270,34,22], [258,268,31,22], [279,265,33,23], [300,262,35,23], [300,262,34,22], [335,259,37,23], [370,256,38,22], [404,252,36,23], [405,252,35,22], [437,250,35,23], [450,250,29,23], [445,251,28,23], [445,252,28,22], [440,252,28,22], [434,252,32,24], [437,255,29,24], [436,254,30,27], [453,262,32,25], [473,266,31,25], [474,266,31,24], [491,271,34,26], [509,278,35,28], [525,283,33,25], [525,280,33,25], [538,280,29,24], [547,281,29,24], [550,282,28,24], [551,282,28,24], [549,283,30,22], [546,282,28,23], [524,281,47,24], [523,281,36,22], [492,282,37,22], [460,282,36,23], [458,282,39,22], [432,282,34,22], [413,282,32,23], [400,281,30,23], [399,281,31,23], [392,280,29,22], [385,279,30,22], [381,278,28,22], [381,278,29,23], [369,277,33,23], [344,273,34,22], [318,269,34,22], [318,270,34,21], [295,268,32,22], [270,265,33,18], [270,266,35,19], [228,258,35,21], [231,259,33,20], [208,257,33,19], [183,254,34,19], [159,250,34,20], [159,251,34,18], [137,247,34,20], [115,242,34,21], [91,240,36,20], [92,240,34,20], [68,237,35,20], [46,233,34,20], [47,232,32,21], [24,229,33,20], [1,227,33,19], [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0], [1,215,21,20], [11,214,35,19], [33,214,27,20], [32,213,31,22], [32,214,27,22], [27,214,28,22], [22,214,29,21], [22,214,28,20], [22,214,30,21], [41,214,34,21], [40,214,33,20], [67,213,34,21], [93,213,34,21], [116,212,34,22], [116,213,33,20], [138,212,33,21], [158,212,33,21], [179,212,32,21], [177,213,33,20], [198,212,32,22], [210,213,30,21], [209,213,31,20], [207,213,30,20], [200,212,31,22], [195,212,30,20], [198,212,30,21], [198,213,30,20], [206,211,29,22], [212,212,30,22], [229,212,34,21], [230,213,32,19], [255,213,32,20], [280,212,31,20], [304,211,33,21], [303,212,33,20], [324,211,33,21], [340,211,28,22], [340,211,28,23], [338,213,28,22], [333,225,25,24], [327,238,25,24], [326,238,27,23], [322,248,26,23], [322,247,27,22], [323,248,27,22], [323,249,28,22], [326,249,28,22], [330,255,26,26], [330,256,25,24], [330,268,24,24], [328,280,26,25], [325,285,29,22], [326,285,28,22], [325,284,29,22], [326,286,29,20], [327,288,27,24], [327,288,28,25], [327,301,29,24], [327,313,29,24], [328,313,27,22], [327,318,30,22], [326,316,30,22], [326,318,28,21], [326,319,30,20], [326,319,29,21], [325,319,29,21], [325,319,30,23], [326,319,29,22], [326,320,30,20], [326,320,29,20], [326,319,28,22], [325,319,31,22], [326,319,29,22], [323,318,32,23], [324,320,30,20], [325,319,29,22], [326,319,28,22], [325,319,28,22], [326,319,27,23], [324,321,30,21], [326,319,28,24], [325,319,28,23], [325,319,29,22], [325,320,27,20], [324,319,30,23], [324,320,29,21], [325,320,27,21], [324,320,29,22], [326,320,28,20], [324,319,29,21], [325,320,27,21], [324,320,27,21], [324,320,28,21], [324,319,27,23], [324,319,29,22], [325,320,28,21], [321,320,31,21], [323,320,29,21], [324,320,27,21], [323,320,29,21], [323,319,31,23], [323,319,28,22], [323,319,29,22], [323,320,28,22], [322,320,29,21], [322,319,29,22], [322,320,28,21], [322,321,29,21], [323,320,29,20], [321,321,30,21], [321,316,30,21], [322,309,31,21], [320,309,31,20], [320,303,30,21], [320,298,28,19], [319,293,29,21], [320,292,27,21], [319,285,28,21], [316,278,30,23], [317,279,30,23], [317,275,28,21], [317,268,27,21], [317,262,26,22], [317,262,26,22], [315,259,26,21], [314,259,28,22], [311,259,29,21], [311,259,27,21], [309,259,27,22], [308,259,28,22], [306,259,30,22], [305,259,30,21], [304,259,30,21], [302,259,30,21], [300,260,29,22], [300,260,28,21], [296,260,31,22], [295,261,29,23], [293,260,32,23], [296,260,30,21], [297,260,28,22], [296,259,32,22], [298,260,29,21] ] }
创建所需 labels 文件夹与 IR_1.txt 文件,代码转化如下:
# -*- coding: utf-8 -*- import json import os # 读取json文件 data = json.load(open('1.json', 'r', encoding='utf-8')) data_gt_rect = data['gt_rect'] print(data_gt_rect[0]) # 保存成txt文件 def text_save(filename, data_gt_rect): # filename为写入CSV文件的路径,data为要写入数据列表. file = open(filename, 'a') for i in range(len(data_gt_rect)): s = data_gt_rect[i] strNums=[str(x) for x in s] strNums = ",".join(strNums) print(strNums) file.write(strNums) file.write('n') file.close() print("保存文件成功") text_save('data/Anti_UAV_test_dev/01_1667_0001-1500/labels/IR_1.txt', data_gt_rect)
三、样例3
数据集结构(批量转换文件夹下 json 文件)
json 文件内容(同上)如下:
代码转化如下:
# -*- coding: utf-8 -*- import json import os # 保存成txt文件 def text_save(file_name, data_gt_rect): # filename为写入CSV文件的路径,data为要写入数据列表. file = open(file_name, 'a') for i in range(len(data_gt_rect)): s = data_gt_rect[i] strNums = [str(x) for x in s] strNums = ",".join(strNums) print(strNums) file.write(strNums) file.write('n') file.close() print("保存文件成功") def get_jsonfile(path, file_list): dir_list = os.listdir(path) for x in dir_list: new_x = os.path.join(path, x) if os.path.isdir(new_x): get_jsonfile(new_x, file_list) else: file_tuple = os.path.splitext(new_x) if file_tuple[1] == '.json': file_list.append(new_x) return file_list # 读取json文件 files = [] path = 'data/Anti_UAV_test_dev1' get_jsonfile(path, files) for file_name in files: data = json.load(open(file_name, 'r', encoding='utf-8')) data_gt_rect = data['gt_rect'] print(data_gt_rect[0]) text_save(file_name + ".txt", data_gt_rect)
代码转化后结构:
总结
总结 json 文件转化为 txt 文件的几种情况用于学习记录。