CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hukaixuan19970627

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: hukaixuan19970627/yolov5_obb
Path: blob/master/tools/TestJson2VocClassTxt.py
Views: 475
1
"""
2
Yolov5-obb检测结果Json 文件转Voc Class Txt
3
--json_path 输入的json文件路径
4
--save_path 输出文件夹路径
5
"""
6
7
import os
8
import json
9
from tqdm import tqdm
10
import argparse
11
import shutil
12
13
parser = argparse.ArgumentParser()
14
parser.add_argument('--json_path', default='runs/val/exp/last_predictions.json',type=str, help="input: coco format(json)")
15
parser.add_argument('--save_path', default='runs/val/exp/last_predictions_Txt', type=str, help="specify where to save the output dir of labels")
16
arg = parser.parse_args()
17
18
# For DOTA-v2.0
19
dotav2_classnames = [ 'plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship',
20
'tennis-court', 'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor',
21
'swimming-pool', 'helicopter', 'container-crane', 'airport', 'helipad']
22
# For DOTA-v1.5
23
dotav15_classnames = ['plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship', 'tennis-court',
24
'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor', 'swimming-pool', 'helicopter', 'container-crane']
25
# For DOTA-v1.0
26
datav1_classnames = ['plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship', 'tennis-court',
27
'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor', 'swimming-pool', 'helicopter']
28
29
DOTA_CLASSES = dotav15_classnames
30
if __name__ == '__main__':
31
json_file = arg.json_path # COCO Object Instance 类型的标注
32
ana_txt_save_path = arg.save_path # 保存的路径
33
34
data = json.load(open(json_file, 'r'))
35
if os.path.exists(ana_txt_save_path):
36
shutil.rmtree(ana_txt_save_path) # delete output folderX
37
os.makedirs(ana_txt_save_path)
38
39
for data_dict in data:
40
img_name = data_dict["file_name"]
41
score = data_dict["score"]
42
poly = data_dict["poly"]
43
classname = DOTA_CLASSES[data_dict["category_id"]-1] # COCO's category_id start from 1, not 0
44
45
lines = "%s %s %s %s %s %s %s %s %s %s\n" % (img_name, score, poly[0],poly[1],poly[2],poly[3],poly[4],poly[5],poly[6],poly[7])
46
with open(str(ana_txt_save_path + '/Task1_' + classname) + '.txt', 'a') as f:
47
f.writelines(lines)
48
pass
49
print("Done!")
50