-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload_vot.py
283 lines (231 loc) · 10.5 KB
/
download_vot.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os
from tqdm import tqdm
import six
import csv
import requests
from urllib.parse import urlparse, urljoin
import tempfile
import shutil
# see: https://github.com/votchallenge/toolkit/tree/master/vot/stack
VOT_DATASETS = {
"vot2013" : "http://data.votchallenge.net/vot2013/dataset/description.json",
"vot2014" : "http://data.votchallenge.net/vot2014/dataset/description.json",
"vot2015" : "http://data.votchallenge.net/vot2015/dataset/description.json",
"vot-tir2015" : "http://www.cvl.isy.liu.se/research/datasets/ltir/version1.0/ltir_v1_0_8bit.zip",
"vot2016" : "http://data.votchallenge.net/vot2016/main/description.json",
"vot-tir2016" : "http://data.votchallenge.net/vot2016/vot-tir2016.zip",
"vot2017" : "http://data.votchallenge.net/vot2017/main/description.json",
"vot-st2018" : "http://data.votchallenge.net/vot2018/main/description.json",
"vot-lt2018" : "http://data.votchallenge.net/vot2018/longterm/description.json",
"vot-st2019" : "http://data.votchallenge.net/vot2019/main/description.json",
"vot-lt2019" : "http://data.votchallenge.net/vot2019/longterm/description.json",
"vot-rgbd2019" : "http://data.votchallenge.net/vot2019/rgbd/description.json",
"vot-rgbt2019" : "http://data.votchallenge.net/vot2019/rgbtir/meta/description.json",
"vot-st2020" : "https://data.votchallenge.net/vot2020/shortterm/description.json",
"vot-rgbt2020" : "http://data.votchallenge.net/vot2020/rgbtir/meta/description.json",
"vot-st2021": "https://data.votchallenge.net/vot2021/shortterm/description.json",
"test" : "http://data.votchallenge.net/toolkit/test.zip",
"segmentation" : "http://box.vicos.si/tracking/vot20_test_dataset.zip",
"vot2022/rgbd": "https://data.votchallenge.net/vot2022/rgbd/description.json",
"vot2022/depth": "https://data.votchallenge.net/vot2022/depth/description.json",
"vot2022/stb": "https://data.votchallenge.net/vot2022/stb/description.json",
"vot2022/sts": "https://data.votchallenge.net/vot2022/sts/description.json",
"vot2022/lt": "https://data.votchallenge.net/vot2022/lt/description.json"
}
def download_dataset(name, path=".", existed=None):
# name: eg. vot-st2020
# path: eg. xxx/workspace/sequences
if not name in VOT_DATASETS:
raise ValueError("Unknown dataset")
url = VOT_DATASETS[name]
# url: "https://data.votchallenge.net/vot2020/shortterm/description.json"
meta = download_json(url)
print(f'Downloading sequence dataset "{meta["name"]}" with {len(meta["sequences"])} sequences.')
base_url = get_base_url(url) + "/" # "http://data.votchallenge.net/vot2019/rgbd/"
with Progress("Downloading", len(meta["sequences"])) as progress:
for sequence in meta["sequences"]:
if existed is not None and sequence["name"] in existed:
continue
sequence_directory = os.path.join(path, sequence["name"])
os.makedirs(sequence_directory, exist_ok=True)
data = {'name': sequence["name"], 'fps': sequence["fps"], 'format': 'default'}
annotations_url = join_url(base_url, sequence["annotations"][
"url"]) # 'https://zenodo.org/record/2640900/files/backpack_blue.zip'
try:
download_uncompress(annotations_url,
sequence_directory) # xxx/workspace/sequences/backpack_robotarm_lab_occ
except Exception as e:
raise Exception("Unable do download annotations bundle")
except IOError as e:
raise Exception(
"Unable to extract annotations bundle, is the target directory writable and do you have enough space?")
for cname, channel in sequence["channels"].items():
channel_directory = os.path.join(sequence_directory, cname)
os.makedirs(channel_directory, exist_ok=True)
channel_url = join_url(base_url, channel["url"])
try:
download_uncompress(channel_url, channel_directory)
except Exception as e:
raise Exception("Unable do download channel bundle")
except IOError as e:
raise Exception(
"Unable to extract channel bundle, is the target directory writable and do you have enough space?")
if "pattern" in channel:
data["channels." + cname] = cname + os.path.sep + channel["pattern"]
else:
data["channels." + cname] = cname + os.path.sep
write_properties(os.path.join(sequence_directory, 'sequence'), data)
progress.relative(1)
with open(os.path.join(path, "list.txt"), "w") as fp:
for sequence in meta["sequences"]:
fp.write('{}\n'.format(sequence["name"]))
def download_json(url):
try:
return requests.get(url).json()
except requests.exceptions.RequestException as e:
raise Exception("Unable to read JSON file {}".format(e))
def get_base_url(url):
return url.rsplit('/', 1)[0]
def is_absolute_url(url):
return bool(urlparse(url).netloc)
def join_url(url_base, url_path):
if is_absolute_url(url_path):
return url_path
return urljoin(url_base, url_path)
def extract_files(archive, destination, callback = None):
from zipfile import ZipFile
with ZipFile(file=archive) as zip_file:
# Loop over each file
total=len(zip_file.namelist())
for file in zip_file.namelist():
# Extract each file to another directory
# If you want to extract to current working directory, don't specify path
zip_file.extract(member=file, path=destination)
if callback:
callback(1, total)
def download_uncompress(url, path):
_, ext = os.path.splitext(urlparse(url).path)
tmp_file = tempfile.mktemp(suffix=ext)
try:
download(url, tmp_file)
extract_files(tmp_file, path)
finally:
if os.path.exists(tmp_file):
os.unlink(tmp_file)
def download(url, output, callback=None, chunk_size=1024 * 32):
with requests.session() as sess:
while True:
res = sess.get(url, stream=True)
if not res.status_code == 200:
raise Exception("File not available")
if 'Content-Disposition' in res.headers:
# This is the file
break
break
if output is None:
output = os.path.basename(url)
output_is_path = isinstance(output, str)
if output_is_path:
tmp_file = tempfile.mktemp()
filehandle = open(tmp_file, 'wb')
else:
tmp_file = None
filehandle = output
try:
total = res.headers.get('Content-Length')
if total is not None:
total = int(total)
for chunk in res.iter_content(chunk_size=chunk_size):
filehandle.write(chunk)
if callback:
callback(len(chunk), total)
if tmp_file:
filehandle.close()
shutil.copy(tmp_file, output)
except IOError:
raise Exception("Error when downloading file")
finally:
try:
if tmp_file:
os.remove(tmp_file)
except OSError:
pass
return output
class Progress(object):
class StreamProxy(object):
def write(self, x):
# Avoid print() second call (useless \n)
if len(x.rstrip()) > 0:
tqdm.write(x)
def flush(self):
# return getattr(self.file, "flush", lambda: None)()
pass
@staticmethod
def logstream():
return Progress.StreamProxy()
def __init__(self, description="Processing", total=100):
silent = False
if not silent:
self._tqdm = tqdm(disable=None,
bar_format=" {desc:20.20} |{bar}| {percentage:3.0f}% [{elapsed}<{remaining}]")
self._tqdm.desc = description
self._tqdm.total = total
if silent or self._tqdm.disable:
self._tqdm = None
self._value = 0
self._total = total if not silent else 0
def _percent(self, n):
return int((n * 100) / self._total)
def absolute(self, value):
if self._tqdm is None:
if self._total == 0:
return
prev = self._value
self._value = max(0, min(value, self._total))
if self._percent(prev) != self._percent(self._value):
print("%d %%" % self._percent(self._value))
else:
self._tqdm.update(value - self._tqdm.n) # will also set self.n = b * bsize
def relative(self, n):
if self._tqdm is None:
if self._total == 0:
return
prev = self._value
self._value = max(0, min(self._value + n, self._total))
if self._percent(prev) != self._percent(self._value):
print("%d %%" % self._percent(self._value))
else:
self._tqdm.update(n) # will also set self.n = b * bsize
def total(self, t):
if self._tqdm is None:
if self._total == 0:
return
self._total = t
else:
if self._tqdm.total == t:
return
self._tqdm.total = t
self._tqdm.refresh()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
def close(self):
if self._tqdm:
self._tqdm.close()
def write_properties(filename, dictionary, delimiter='='):
''' Writes the provided dictionary in key sorted order to a properties
file with each line in the format: key<delimiter>value
filename -- the name of the file to be written
dictionary -- a dictionary containing the key/value pairs.
'''
open_kwargs = {'mode': 'w', 'newline': ''} if six.PY3 else {'mode': 'wb'}
with open(filename, **open_kwargs) as csvfile:
writer = csv.writer(csvfile, delimiter=delimiter, escapechar='\\',
quoting=csv.QUOTE_NONE)
writer.writerows(sorted(dictionary.items()))
if __name__ == '__main__':
NAME = 'vot2022/rgbd'
SAVE_DIR = '/home/zwu/'
#Existed_Seqs = ['agility', 'ants1', 'ball2', 'ball3', 'basketball', 'birds1', 'bolt1', 'book', 'butterfly', 'car1']
download_dataset(name=NAME, path=SAVE_DIR)