-
Notifications
You must be signed in to change notification settings - Fork 3
/
AU.py
90 lines (60 loc) · 2.9 KB
/
AU.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Mads Dyrmann
# AU format: # a JSON-string of the following format
# {
# 'label':<classlabel>, # a label without spaces
# 'imagepath':<imagepath>, # relative path to image
# 'imagewidth':<image width>, # width of image
# 'imageheight':<image height>, # height of image
# 'xmin': <xmin>, # minimum column coordinate staring from upper left corner
# 'ymin': <ymin>, # maximum column coordinate staring from upper left corner
# 'xmax': <xmax>, # minimum row coordinate staring from upper left corner
# 'ymax': <ymax>, # maximum row coordinate staring from upper left corner
# 'orientation':<rotation [0:1]>,
# 'occlusion':<occlusion>
# }
# CSV format: # (see retinanet)
# path/to/image.jpg,x1,y1,x2,y2,class_name
############# Arguments:
# AUAnnotationList list of rows from kitti annotation
# imsize: size of image
"""
#import xml.etree.cElementTree as ET
from concurrent.futures import ThreadPoolExecutor
import os
import pandas as pd
executor = ThreadPoolExecutor(max_workers=30)
def AU2csvfile(AUAnnotationList, outputpath=None):
df = pd.DataFrame(AUAnnotationList)
#get unique file-names, as we want a file per image
uniquefilenames = df['imagepath'].unique().tolist()
for filename in uniquefilenames:
dftmp = df[['label', 'labelnumeric', 'imagewidth', 'imageheight','xmin', 'ymin', 'xmax', 'ymax', 'occlusion','objectix']].loc[df['imagepath']==filename]
filename, ext = os.path.splitext(filename)
outoutfilepath = os.path.join(outputpath,filename+'.csv')
dftmp.to_csv(outoutfilepath,index=False, sep=',')
def AU2csv(AUAnnotationList):
csvAnnotationList = []
for anno in AUAnnotationList:
#Split the string at spaces, but respect quotes in specie's names
csvline = [anno['imagepath'],anno['xmin'],anno['ymin'],anno['xmax'],anno['ymax'],anno['label'],str(anno['labelnumeric'])]
csvline = [x if ',' not in x else '"'+x+'"' for x in csvline] # add '"' if comma exist in entry
csvAnnotationList.append(csvline)
return csvAnnotationList
def csvlist2file(csvAnnotationList,outputpath=None):
assert outputpath is not None
#get unique file-names, as we want a file per image
imagefiles = [csvline[0] for csvline in csvAnnotationList]
uniquefilenames = list(set(imagefiles))
for filename in uniquefilenames:
annotationlistForImage=[csvline for csvline in csvAnnotationList if csvline[0]==filename]
annotationStringlist=[csvanno2string(csvline) for csvline in annotationlistForImage]
filename, ext = os.path.splitext(filename)
outoutfilepath = os.path.join(outputpath,filename+'.csv')
with open(outoutfilepath,'w') as fid:
fid.write('\n'.join(csvAnnotationList))
#fid.writelines(annotationStringlist)
def csvanno2string(csvline, sep=','):
return sep.join(csvline)