json 轉(zhuǎn) mot17數(shù)據(jù)格式的實現(xiàn)代碼 (親測有效)
代碼使用說明
1970-2270文件夾是保存圖像和json文件(也就是需要進(jìn)行轉(zhuǎn)換的文件)
det文件夾是保存單個json對應(yīng)的txt(因為np.savetxt函數(shù)只能進(jìn)行單個數(shù)組的保存)
det.txt文件是整合det文件夾所有txt文件(mot17數(shù)據(jù)集格式)

完整代碼
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#計算圖片總數(shù)
for root, dirs, files in os.walk(file_dir):
img_num=img_num+len(files)
one=os.path.basename(str(root))#獲取路徑最后的/或者\(yùn)后的文件名
L.append(one)
num=len(L)-1 #獲取路徑下文件個數(shù)
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個目標(biāo),6列(類別索引、框左上角x坐標(biāo)、框左上角y坐標(biāo)、框的寬、框的高、目標(biāo)置信度100)'''
data=np.zeros([5,6])
for i in range(len(data_dict["shapes"])):#遍歷一個json中的所有目標(biāo)
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)#補(bǔ)充-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()
代碼執(zhí)行結(jié)果

到此這篇關(guān)于json 轉(zhuǎn) mot17數(shù)據(jù)格式 (親測有效)的文章就介紹到這了,更多相關(guān)json 轉(zhuǎn) mot17數(shù)據(jù)格式 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python3.7 dataclass使用指南小結(jié)
本文將帶你走進(jìn)python3.7的新特性dataclass,通過本文你將學(xué)會dataclass的使用并避免踏入某些陷阱。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
在python中利用GDAL對tif文件進(jìn)行讀寫的方法
今天小編就為大家分享一篇在python中利用GDAL對tif文件進(jìn)行讀寫的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Django urls.py重構(gòu)及參數(shù)傳遞詳解
這篇文章主要介紹了Django urls.py重構(gòu)及參數(shù)傳遞詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
Python實現(xiàn)將圖片批量轉(zhuǎn)為PDF
在日常辦公和處理圖片時,我們常常需要將多張圖片合并成一個PDF文件,所以本文為大家介紹了如何使用Python實現(xiàn)圖片批量轉(zhuǎn)為PDF,感興趣的可以了解下2024-12-12

