1515"""Structure-from-Motion dataset (Sfm120k) download function."""
1616
1717import os
18+ import tarfile
1819
20+ import requests
1921import tensorflow as tf
2022
2123
@@ -40,7 +42,7 @@ def download_train(data_dir):
4042 tf .io .gfile .mkdir (datasets_dir )
4143
4244 # Download folder train/retrieval-SfM-120k/.
43- src_dir = 'http ://cmp.felk.cvut.cz/cnnimageretrieval/data/train/ims'
45+ src_dir = 'https ://cmp.felk.cvut.cz/cnnimageretrieval/data/train/ims'
4446 dst_dir = os .path .join (datasets_dir , 'retrieval-SfM-120k' , 'ims' )
4547 download_file = 'ims.tar.gz'
4648 if not tf .io .gfile .exists (dst_dir ):
@@ -49,24 +51,28 @@ def download_train(data_dir):
4951 print ('>> Image directory does not exist. Creating: {}' .format (dst_dir ))
5052 tf .io .gfile .makedirs (dst_dir )
5153 print ('>> Downloading ims.tar.gz...' )
52- os .system ('wget {} -O {}' .format (src_file , dst_file ))
54+ with open (dst_file , 'wb' ) as f :
55+ f .write (requests .get (src_file , timeout = 60 ).content )
5356 print ('>> Extracting {}...' .format (dst_file ))
54- os .system ('tar -zxf {} -C {}' .format (dst_file , dst_dir ))
57+ with tarfile .open (dst_file , 'r:gz' ) as tar :
58+ safe_members = [m for m in tar .getmembers ()
59+ if not os .path .isabs (m .name ) and '..' not in m .name .split ('/' )]
60+ tar .extractall (dst_dir , members = safe_members )
5561 print ('>> Extracted, deleting {}...' .format (dst_file ))
56- os .system ( 'rm {}' . format ( dst_file ) )
62+ os .remove ( dst_file )
5763
5864 # Create symlink for train/retrieval-SfM-30k/.
5965 dst_dir_old = os .path .join (datasets_dir , 'retrieval-SfM-120k' , 'ims' )
6066 dst_dir = os .path .join (datasets_dir , 'retrieval-SfM-30k' , 'ims' )
6167 if not (tf .io .gfile .exists (dst_dir ) or os .path .islink (dst_dir )):
6268 tf .io .gfile .makedirs (os .path .join (datasets_dir , 'retrieval-SfM-30k' ))
63- os .system ( 'ln -s {} {}' . format ( dst_dir_old , dst_dir ) )
69+ os .symlink ( dst_dir_old , dst_dir )
6470 print (
6571 '>> Created symbolic link from retrieval-SfM-120k/ims to '
6672 'retrieval-SfM-30k/ims' )
6773
6874 # Download db files.
69- src_dir = 'http ://cmp.felk.cvut.cz/cnnimageretrieval/data/train/dbs'
75+ src_dir = 'https ://cmp.felk.cvut.cz/cnnimageretrieval/data/train/dbs'
7076 datasets = ['retrieval-SfM-120k' , 'retrieval-SfM-30k' ]
7177 for dataset in datasets :
7278 dst_dir = os .path .join (datasets_dir , dataset )
@@ -89,15 +95,16 @@ def download_train(data_dir):
8995 if not os .path .isfile (dst_file ):
9096 print ('>> DB file {} does not exist. Downloading...' .format (
9197 download_files [i ]))
92- os .system ('wget {} -O {}' .format (src_file , dst_file ))
98+ with open (dst_file , 'wb' ) as f :
99+ f .write (requests .get (src_file , timeout = 60 ).content )
93100
94101 if download_eccv2020 :
95102 eccv2020_dst_file = os .path .join (dst_dir , download_eccv2020 )
96103 if not os .path .isfile (eccv2020_dst_file ):
97104 eccv2020_src_dir = \
98- "http ://ptak.felk.cvut.cz/personal/toliageo/share/how/dataset/"
105+ "https ://ptak.felk.cvut.cz/personal/toliageo/share/how/dataset/"
99106 eccv2020_dst_file = os .path .join (dst_dir , download_eccv2020 )
100107 eccv2020_src_file = os .path .join (eccv2020_src_dir ,
101108 download_eccv2020 )
102- os . system ( 'wget {} -O {}' . format ( eccv2020_src_file ,
103- eccv2020_dst_file ) )
109+ with open ( eccv2020_dst_file , 'wb' ) as f :
110+ f . write ( requests . get ( eccv2020_src_file , timeout = 60 ). content )
0 commit comments