json 轉(zhuǎn) mot17數(shù)據(jù)格式的實現(xiàn)代碼 (親測有效)
代碼使用說明
1970-2270文件夾是保存圖像和json文件(也就是需要進行轉(zhuǎn)換的文件)
det文件夾是保存單個json對應的txt(因為np.savetxt函數(shù)只能進行單個數(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))#獲取路徑最后的/或者\后的文件名 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個目標,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()
代碼執(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)文章
利用Selenium添加cookie實現(xiàn)自動登錄的示例代碼(fofa)
這篇文章主要介紹了利用Selenium添加cookie實現(xiàn)自動登錄的示例代碼(fofa),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05Python調(diào)用百度AI實現(xiàn)圖片上文字識別功能實例
百度AI功能還是很強大的,百度AI開放平臺真的是測試接口的天堂,免費接口很多,當然有量的限制,但個人使用是完全夠用的,下面這篇文章主要給大家介紹了關(guān)于Python調(diào)用百度AI實現(xiàn)圖片上文字識別功能的相關(guān)資料,需要的朋友可以參考下2021-09-09python3實現(xiàn)字符串的全排列的方法(無重復字符)
這篇文章主要介紹了python3實現(xiàn)字符串的全排列的方法(無重復字符),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07