-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDExTool.py
More file actions
231 lines (180 loc) · 9.85 KB
/
DExTool.py
File metadata and controls
231 lines (180 loc) · 9.85 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# -*- coding: utf-8 -*-
# Created by Paola Cañas with <3
import glob
import os
import re
from pathlib import Path
from accessDMDAnn import exportClass
from group_split_material import splitClass, groupClass
from statistics import get_statistics
print("Welcome :)")
opt = int(input("What do you whish to do?: export material for training:[0] group exported material by classes:[1] create train and test split:[2] get statistics:[3] : "))
if opt == 0:
# export material for training
print("To change export settings go to config_DEx.json and change control variables.")
destination_path = input("Enter destination path: ")
selec = input("How do you want to read annotations, by: Group:[g] Sessions:[f] One OpenLABEL:[v] : ")
if selec == "g":
#By group
folder_path = input("Enter DMD group's path (../dmd/g#): ")
#e.g /home/pncanas/Desktop/consumption/dmd/gA
selec_session = input("Enter the session you wish to export in this group: all:[0] S1:[1] S2:[2] S3[3] S4[4] S5[5] S6[6] : ")
subject_paths = glob.glob(folder_path + '/*')
subject_paths.sort()
for subject in subject_paths:
print(subject)
session_path = glob.glob(subject + '/*')
session_path.sort()
for session in session_path:
if "s"+str(selec_session) in session or selec_session == "0":
print(session)
annotation_paths = glob.glob(session + '/*.json')
annotation_paths.sort()
for annotation in annotation_paths:
print(annotation)
dmd_folder=Path(annotation).parents[3]
exportClass(annotation,str(dmd_folder),destination_path)
print("Oki :) ----------------------------------------")
elif selec == "f":
#By session
folder_path = input("Enter root dmd folder path(../dmd): ")
#e.g /home/pncanas/Desktop/dmd/
selec_session = input("Enter the session you wish to export in this group: all:[0] S1:[1] S2:[2] S3[3] S4[4] S5[5] S6[6] : ")
group_paths = glob.glob(folder_path + '/*')
group_paths.sort()
for group in group_paths:
print(group)
subject_paths = glob.glob(group + '/*')
subject_paths.sort()
for subject in subject_paths:
print(subject)
session_path = glob.glob(subject + '/*')
session_path.sort()
for session in session_path:
if "s"+str(selec_session) in session or selec_session == "0":
print(session)
annotation_paths = glob.glob(session + '/*.json')
annotation_paths.sort()
for annotation in annotation_paths:
print(annotation)
dmd_folder=Path(annotation).parents[3]
exportClass(annotation,str(dmd_folder),destination_path)
print("Oki :) ----------------------------------------")
elif selec == "v":
vcd_path = input("Paste the OpenLABEL file path (..._ann.json): ")
# e.g: /Desktop/consumption/dmd/gA/1/s2/gA_1_s2_2019-03-08T09;21;03+01;00_rgb_ann.json
regex_internal = '(?P<subject>[1-9]|[1-2][0-9]|[3][0-7])_(?P<session>[a-z]{1,})_'\
'(?P<date>(?P<month>0[1-9]|1[012])-(?P<day>0[1-9]|[12][0-9]|3[01]))'
regex_external = '(?P<group>g[A-z]{1,})_(?P<subject>[1-9]|[1-2][0-9]|[3][0-7])_'\
'(?P<session>s[1-9]{1,})_(?P<timestamp>(?P<date>(?P<year>\d{4})-(?P<month>0[1-9]|1[012])-'\
'(?P<day>0[1-9]|[12][0-9]|3[01]))T(?P<time>(?P<hour>\d{1,2});(?P<minute>\d{1,2});'\
'(?P<second>\d{1,2}))\+\d{1,2};\d{1,2})_(?P<channel>rgb|depth|ir)_(?P<stream>ann)'
regex_internal = re.compile(regex_internal)
regex_external = re.compile(regex_external)
match_internal = regex_internal.search(str(vcd_path))
match_external = regex_external.search(str(vcd_path))
if match_internal or match_external:
#dmd annotation
dmd_folder=Path(vcd_path).parents[3]
datasetDMD = True
else:
#not a dmd annotation
dmd_folder=Path(vcd_path).parents[1]
datasetDMD = False
exportClass(vcd_path,str(dmd_folder),destination_path, datasetDMD)
print("Oki :) ----------------------------------------")
else:
print("__Please, select a valid option__")
elif opt == 1:
# group exported material by classes
material_path = input("Enter exported DMD material path (inside must be sessions folders(s#) e.g:../dmd_rgb/): ")
groupClass(material_path)
print("Oki :) ----------------------------------------")
elif opt == 2:
# create train and test split
print("This function only works with dmd material structure when exporting with DEx tool.")
material_path = input("Enter exported material path (inside must be classes folders e.g.: /safe_driving/*.jpg): ")
destination_path = input("Enter destination path (a new folder to store train and test splits): ")
test_proportion = input("Enter test proportion for split [0-1] (e.g. 0.20): ")
splitClass(material_path,destination_path,test_proportion)
print("Oki :) ----------------------------------------")
elif opt == 3:
# Get statistics
print("This function only works with dmd material structure when exporting with DEx tool.")
destination_path = input("Enter filename for a report file (e.g. report.txt): ")
selec = input("How do you want to read annotations, by: Group:[g] Sessions:[f] One OpenLABEL:[v] : ")
#Delete destination_path to avoid redundancies
if os.path.exists(destination_path.replace(".txt","-actions.txt")):
os.remove(destination_path.replace(".txt","-actions.txt"))
if os.path.exists(destination_path.replace(".txt","-frames.txt")):
os.remove(destination_path.replace(".txt","-frames.txt"))
if selec == "g":
#By group
folder_path = input("Enter DMD group's path (../dmd/g#): ")
#e.g /home/pncanas/Desktop/consumption/dmd/gA
selec_session = input("Enter the session you wish to export in this group: all:[0] S1:[1] S2:[2] S3[3] S4[4] S5[5] S6[6] : ")
subject_paths = glob.glob(folder_path + '/*')
subject_paths.sort()
for subject in subject_paths:
print(subject)
session_path = glob.glob(subject + '/*')
session_path.sort()
for session in session_path:
if "s"+str(selec_session) in session or selec_session == "0":
print(session)
annotation_paths = glob.glob(session + '/*.json')
annotation_paths.sort()
for annotation in annotation_paths:
print(annotation)
get_statistics(annotation,destination_path)
print("Oki :) ----------------------------------------")
elif selec == "f":
#By session
folder_path = input("Enter root dmd folder path(../dmd): ")
#e.g /home/pncanas/Desktop/dmd/
selec_session = input("Enter the session you wish to export in this group: all:[0] S1:[1] S2:[2] S3[3] S4[4] S5[5] S6[6] : ")
group_paths = glob.glob(folder_path + '/*')
group_paths.sort()
for group in group_paths:
print(group)
subject_paths = glob.glob(group + '/*')
subject_paths.sort()
for subject in subject_paths:
print(subject)
session_path = glob.glob(subject + '/*')
session_path.sort()
for session in session_path:
if "s"+str(selec_session) in session or selec_session == "0":
print(session)
annotation_paths = glob.glob(session + '/*.json')
annotation_paths.sort()
for annotation in annotation_paths:
print(annotation)
dmd_folder=Path(annotation).parents[3]
get_statistics(annotation,destination_path)
print("Oki :) ----------------------------------------")
elif selec == "v":
vcd_path = input("Paste the OpenLABEL file path (..._ann.json): ")
# e.g: /Desktop/consumption/dmd/gA/1/s2/gA_1_s2_2019-03-08T09;21;03+01;00_rgb_ann.json
regex_internal = '(?P<subject>[1-9]|[1-2][0-9]|[3][0-7])_(?P<session>[a-z]{1,})_'\
'(?P<date>(?P<month>0[1-9]|1[012])-(?P<day>0[1-9]|[12][0-9]|3[01]))'
regex_external = '(?P<group>g[A-z]{1,})_(?P<subject>[1-9]|[1-2][0-9]|[3][0-7])_'\
'(?P<session>s[1-9]{1,})_(?P<timestamp>(?P<date>(?P<year>\d{4})-(?P<month>0[1-9]|1[012])-'\
'(?P<day>0[1-9]|[12][0-9]|3[01]))T(?P<time>(?P<hour>\d{1,2});(?P<minute>\d{1,2});'\
'(?P<second>\d{1,2}))\+\d{1,2};\d{1,2})_(?P<channel>rgb|depth|ir)_(?P<stream>ann)'
regex_internal = re.compile(regex_internal)
regex_external = re.compile(regex_external)
match_internal = regex_internal.search(str(vcd_path))
match_external = regex_external.search(str(vcd_path))
if match_internal or match_external:
#dmd annotation
dmd_folder=Path(vcd_path).parents[3]
else:
#not a dmd annotation
dmd_folder=Path(vcd_path).parents[1]
get_statistics(vcd_path,destination_path)
print("Oki :) ----------------------------------------")
else:
print("__Please, select a valid option__")
else:
print("__Please, put a valid option.__")