KITTI-MOTS 的txt格式的Annotation如何使用cocotools转变成一般形式的mask?
不知道你这个问题是否已经解决, 如果还没有解决的话:目的:将labelme标注的json文件中的坐标和label信息提取到txt文件中
注意:labelme标注时使用“polygon”即画点标注方式,不是“rectangle”和“circle”标注方式,每个点坐标包括x和y,所以总共输出8个坐标值和1个label值
json格式
{
"version": "4.6.0",
"flags": {},
"shapes": [
{
"label": "B4",
"points": [
[
157.25806451612902,
639.516129032258
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "B4",
"points": [
[
156.0483870967742,
689.1129032258065
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "B4",
"points": [
[
278.6290322580645,
683.0645161290323
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
},
{
"label": "B4",
"points": [
[
275.80645161290323,
634.6774193548387
]
],
"group_id": null,
"shape_type": "point",
"flags": {}
}
],
"imagePath": "4496.jpg",
"imageData": "/9j/4AAQSkZJRgABAQAAAQABAAD",
"imageHeight": 1024,
"imageWidth": 1280
}
批量转换:
dir_json为json文件夹
dir_txt为txt文件夹
同级目录下创建json2txt.py文件,文件内容复制如下
# coding:utf-8
import os
import json
import numpy as np
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']: # shapes里面放的是标签的数据,比如四个点的类别和坐标
xy=np.array(shape['points'])# shapes里面的points是点的x,y坐标
label=str(shape['label']) # 类别信息
strxy = ''
for m,n in xy:
strxy+=str(m)+','+str(n)+','
strxy+=label
ftxt.writelines(strxy+"\n")
dir_json = 'json/'
dir_txt = '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)
txt文件输出示例
这个格式不是coco格式,coco格式如下: