MDNet结果json文件转成long term visual tracking (oxuva)评估所需的csv文件的python脚本
import json
import pdb
import cv2
import os
import pandas as pd
resultpath='/media/yangrui/yrD/Code/py-MDNet-master/result/'
videopath="/media/yangrui/yrD/Code/py-MDNet-master/dataset/test/"
videos=os.listdir(videopath)
for i in range(len(videos)):
jsonpath=resultpath+videos[i]+'/result.json'
with open(jsonpath,'r') as f:
result=json.load(f)
xmin=[]
xmax=[]
ymin=[]
ymax=[]
video_ids=[]
obj_ids=[]
frame_nums=[]
presents=[]
scores=[]
video_id=videos[i][0:7]
if(len(videos[i])==7):
obj_id='obj0000'
elif(videos[i][-1]=='2'):
obj_id='obj0001'
else:
obj_id='obj0002'
present='True'
score=0.5
l=result['res']
imgs=os.listdir(videopath+videos[i]+'/img/')
for j in range(len(imgs)):
image=cv2.imread(videopath+videos[i]+'/img/'+imgs[j])
imgh=image.shape[0]
imgw=image.shape[1]
x=l[j][0]
y=l[j][1]
w=l[j][2]
h=l[j][3]
x1=x/imgw
x2=(x+w)/imgw
y1=y/imgh
y2=(y+h)/imgh
x1=round(x1,4)
x2=round(x2,4)
y1=round(y1,4)
y2=round(y2,4)
frame=imgs[j][0:6]
if(frame=='000000'):
frame_num=0
else:
frame_num=frame.lstrip('0')
xmin.append(x1)
xmax.append(x2)
ymin.append(y1)
ymax.append(y2)
video_ids.append(video_id)
obj_ids.append(obj_id)
frame_nums.append(frame_num)
presents.append(present)
scores.append(score)
dataframe=pd.DataFrame({'video_id':video_ids,'object_id':obj_ids,'frame_num':frame_nums,'present':presents,\
'score':scores,'xmin':xmin,'xmax':xmax,'ymin':ymin,'ymax':ymax})
savepath='/media/yangrui/yrG/Code/csv/'+videos[i][0:7]+'_'+obj_id+'.csv'
columns=['video_id','object_id','frame_num','present','score','xmin','xmax','ymin','ymax']
dataframe.to_csv(savepath,index=False,columns=columns,header=None)