-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathycb_downloader.py
123 lines (99 loc) · 5.13 KB
/
ycb_downloader.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
#Copyright 2015 Yale University - Grablab
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Modified to work with Python 3 by Sebastian Castro, 2020
import os
import sys
import json
import urllib
from urllib.request import Request, urlopen
# Define an output folder
output_directory = os.path.join("asset", "ycb")
# Define a list of objects to download from
# http://ycb-benchmarks.s3-website-us-east-1.amazonaws.com/
objects_to_download = "all"
# objects_to_download = ["001_chips_can",
# "002_master_chef_can",
# "003_cracker_box",
# "004_sugar_box"]
# You can edit this list to only download certain kinds of files.
# 'berkeley_rgbd' contains all of the depth maps and images from the Carmines.
# 'berkeley_rgb_highres' contains all of the high-res images from the Canon cameras.
# 'berkeley_processed' contains all of the segmented point clouds and textured meshes.
# 'google_16k' contains google meshes with 16k vertices.
# 'google_64k' contains google meshes with 64k vertices.
# 'google_512k' contains google meshes with 512k vertices.
# See the website for more details.
#files_to_download = ["berkeley_rgbd", "berkeley_rgb_highres", "berkeley_processed", "google_16k", "google_64k", "google_512k"]
files_to_download = ["berkeley_processed", "google_16k"]
# Extract all files from the downloaded .tgz, and remove .tgz files.
# If false, will just download all .tgz files to output_directory
extract = True
base_url = "http://ycb-benchmarks.s3-website-us-east-1.amazonaws.com/data/"
objects_url = "https://ycb-benchmarks.s3.amazonaws.com/data/objects.json"
if not os.path.exists(output_directory):
os.makedirs(output_directory)
def fetch_objects(url):
""" Fetches the object information before download """
response = urlopen(url)
html = response.read()
objects = json.loads(html)
return objects["objects"]
def download_file(url, filename):
""" Downloads files from a given URL """
u = urlopen(url)
f = open(filename,"wb")
file_size = int(u.getheader("Content-Length"))
print("Downloading: {} ({} MB)".format(filename, file_size/1000000.0))
file_size_dl = 0
block_sz = 65536
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl/1000000.0, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print(status)
f.close()
def tgz_url(object, type):
""" Get the TGZ file URL for a particular object and dataset type """
if type in ["berkeley_rgbd", "berkeley_rgb_highres"]:
return base_url + "berkeley/{object}/{object}_{type}.tgz".format(object=object,type=type)
elif type in ["berkeley_processed"]:
return base_url + "berkeley/{object}/{object}_berkeley_meshes.tgz".format(object=object,type=type)
else:
return base_url + "google/{object}_{type}.tgz".format(object=object,type=type)
def extract_tgz(filename, dir):
""" Extract a TGZ file """
tar_command = "tar -xzf {filename} -C {dir}".format(filename=filename,dir=dir)
os.system(tar_command)
os.remove(filename)
def check_url(url):
""" Check the validity of a URL """
try:
request = Request(url)
request.get_method = lambda : 'HEAD'
response = urlopen(request)
return True
except Exception as e:
return False
if __name__ == "__main__":
# Grab all the object information
objects = fetch_objects(objects_url)
# Download each object for all objects and types specified
for object in objects:
if objects_to_download == "all" or object in objects_to_download:
for file_type in files_to_download:
url = tgz_url(object, file_type)
if not check_url(url):
continue
filename = "{path}/{object}_{file_type}.tgz".format(
path=output_directory,
object=object,
file_type=file_type)
download_file(url, filename)
if extract:
extract_tgz(filename, output_directory)