从网站上下载数据集并解压——以coco数据集为例

最近在阅读源码时发现了一段值得学习的源码,实现从网址下载coco数据集到本地,并解压,在这里做下记录,以备借鉴使用。

###以下载annotations为例
# Setup annotations data paths
annDir = "{}/annotations".format(dataDir)
if dataType == "minival": #给出不同子文件夹的文件夹名、文件名和网址
   annZipFile = "{}/instances_minival2014.json.zip".format(dataDir)
   annFile = "{}/instances_minival2014.json".format(annDir)
   annURL = "https://dl.dropboxusercontent.com/s/o43o90bna78omob/instances_minival2014.json.zip?dl=0"
   unZipDir = annDir
elif dataType == "valminusminival":
   annZipFile = "{}/instances_valminusminival2014.json.zip".format(dataDir)
   annFile = "{}/instances_valminusminival2014.json".format(annDir)
   annURL = "https://dl.dropboxusercontent.com/s/s3tw5zcg7395368/instances_valminusminival2014.json.zip?dl=0"
   unZipDir = annDir
else:
   annZipFile = "{}/annotations_trainval{}.zip".format(dataDir, dataYear)
   annFile = "{}/instances_{}{}.json".format(annDir, dataType, dataYear)
   annURL = "http://images.cocodataset.org/annotations/annotations_trainval{}.zip".format(dataYear)
   unZipDir = dataDir
# print("Annotations paths:"); print(annDir); print(annFile); print(annZipFile); print(annURL)

# Download annotations if not available locally
if not os.path.exists(annDir): #生成文件夹
   os.makedirs(annDir)
if not os.path.exists(annFile):
   if not os.path.exists(annZipFile):
       print("Downloading zipped annotations to " + annZipFile + " ...")
       with urllib.request.urlopen(annURL) as resp, open(annZipFile, 'wb') as out: 
           shutil.copyfileobj(resp, out) #使用urllib包下载压缩包
       print("... done downloading.")
   print("Unzipping " + annZipFile)
   with zipfile.ZipFile(annZipFile, "r") as zip_ref: #解压操作
       zip_ref.extractall(unZipDir)
   print("... done unzipping")
print("Will use annotations in " + annFile)

参考地址

你可能感兴趣的:(小技巧,coco,coco下载,网站下载数据集,shutil,urlib)