json 转 mot17数据格式的实现代码 (亲测有效)
作者:G果 时间:2022-03-06 09:23:53
代码使用说明
1970-2270文件夹是保存图像和json文件(也就是需要进行转换的文件)
det文件夹是保存单个json对应的txt(因为np.savetxt函数只能进行单个数组的保存)
det.txt文件是整合det文件夹所有txt文件(mot17数据集格式)
完整代码
from PIL import Image
import os
import glob
import numpy as np
import json
#读取图片,修改图片,另存图片
def convertjpg(jpgfile,outdir,img_sum=0):
img=Image.open(jpgfile)#提取目录下所有图片
try:
img_sum=str('%08d'%img_sum)#图片保存成00000001格式
img.save(os.path.join(outdir)+img_sum+'.jpg')#保存到另一目录
except Exception as e:
print(e)
#读取文件名
def file_name(file_dir):
L=[]#保存文件名
img_num=0#计算图片总数
for root, dirs, files in os.walk(file_dir):
img_num=img_num+len(files)
one=os.path.basename(str(root))#获取路径最后的/或者\后的文件名
L.append(one)
num=len(L)-1 #获取路径下文件个数
print('%s路径下有%d个文件'%(L[0],num))
return L ,num ,img_num
def json2det(json_dir,out_dir,json_num):
with open(json_dir,'r') as f:
data_dict = json.load(f)
'''5行5个目标,6列(类别索引、框左上角x坐标、框左上角y坐标、框的宽、框的高、目标置信度100)'''
data=np.zeros([5,6])
for i in range(len(data_dict["shapes"])):#遍历一个json中的所有目标
points = np.array(data_dict["shapes"][i]["points"])
xmin=min(points[:,0])
ymin=min(points[:,1])
xmax=max(points[:,0])
ymax=max(points[:,1])
data[i,0]=json_num
data[i,1]=xmin
data[i,2]=ymin
data[i,3]=xmax-xmin
data[i,4]=ymax-ymin
data[i,5]=100
print(data)
a=np.linspace(-1,-1,len(data))
data=np.insert(data,1,a,axis=1)#行矩阵插入
a=a.reshape((len(a),1))
data=np.concatenate((data,a,a,a),axis = 1)#补充-1
print(data,'\n')
np.savetxt(out_dir,data,fmt="%d,%d,%.3f,%.3f,%.3f,%.3f,%.3f,%d,%d,%d",delimiter="\n")
def main():
i=0
data_dir='E:/DL/CSDN-blog/json2mot17'
for jsonfile in glob.glob(data_dir+'/1970-2270/'+'*.json'):
i=i+1
json2det(jsonfile,data_dir+'/det/'+str(i)+'.txt',i)
print('det.txt生成完成')
det=open(data_dir+'/det.txt','w')
for txts in range(300):
print(txts)
one_det=open(data_dir+'/det/'+str(txts+1)+'.txt').read()
det.write(one_det)
det.close()
if __name__ == '__main__':
main()
代码执行结果
来源:https://blog.csdn.net/weixin_42899627/article/details/114851196
标签:json,mot17,格式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用Python中的cookielib模拟登录网站
2022-01-20 08:54:26
py3nvml实现GPU相关信息读取的案例分析
2022-01-11 11:00:52
解决python3.6 右键没有 Edit with IDLE的问题
2023-07-31 17:15:00
![](https://img.aspxhome.com/file/2023/9/61179_0s.jpg)
python机器学习实现决策树
2021-04-21 07:44:34
![](https://img.aspxhome.com/file/2023/1/100911_0s.jpg)
TensorFlow Session会话控制&Variable变量详解
2023-06-21 12:37:42
![](https://img.aspxhome.com/file/2023/0/78650_0s.jpg)
利用python的socket发送http(s)请求方法示例
2022-06-06 08:33:39
![](https://img.aspxhome.com/file/2023/1/68871_0s.png)
PyTorch实现更新部分网络,其他不更新
2022-06-15 21:01:33
python openssl模块安装及用法
2023-07-03 12:01:04
详解Python 装饰器执行顺序迷思
2023-12-30 23:55:23
关于多域名COOKIES作用域的问题
2008-11-18 16:12:00
自定义数据库自动编号初始值和步进值
2009-05-19 10:00:00
python飞机大战pygame碰撞检测实现方法分析
2023-01-05 18:55:52
Python中常见的导入方式总结
2023-03-04 18:22:03
实现一个获取元素样式的函数getStyle
2009-02-10 10:37:00
jQuery入门之–属性(一)
2009-07-12 15:29:00
快速解决Django关闭Debug模式无法加载media图片与static静态文件
2023-05-28 02:54:43
PHP数组交集的优化代码分析
2023-09-29 21:58:59
python+matplotlib绘制3D条形图实例代码
2022-05-03 16:36:56
![](https://img.aspxhome.com/file/2023/1/80721_0s.png)
Python shelve模块实现解析
2023-06-23 16:58:01
![](https://img.aspxhome.com/file/2023/6/92166_0s.png)
python通过urllib2获取带有中文参数url内容的方法
2022-07-26 10:35:58