-
Notifications
You must be signed in to change notification settings - Fork 10
/
retrieve_folder.py
78 lines (62 loc) · 2.21 KB
/
retrieve_folder.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
import os
import io
import urllib
from urllib.request import urlretrieve
from tqdm import tqdm
#global list to track triplets
triplets=[]
def download_files(triplets_url):
'''
Retrieves triplets given in triplets_url using urlib/urlib3 and store it in "file_dir".
It stores by creating separate folder for each triplet pair in file_dir.
'''
names = ['query','positive','negative']
index = 0
flag=0
file_dir = './'
for triplet in tqdm(triplets_url):
file_path = file_dir+str(index)
#To check if path exists with given "file_path" name.
if not os.path.exists(file_path):
os.makedirs(file_path)
else:
print('path exists for triplet: ',index)
final_path = []
for image,name in zip(triplet,names):
#Some triplets are not publically available thereby try, catch.
try:
urllib.request.urlretrieve(image, file_path+'/'+name+".jpg")
final_path.append(file_path+'/'+name+".jpg")
except:
flag=1
if flag == 0:
triplets.append(final_path)
flag = 0
index+=1
if(index%100==0 and index>=100):
print(index,"triplets added..")
def dataset_loader(query):
'''
Prepares data for download_files by parsing through 'query_and_triplets.txt' and storing triplet pairs as list.
'''
f = open(query,'r+')
length = len(f.readlines())
with open(query,'r+') as fp:
triplet_return = []
index = 0
for j in range(length//4):
triplet_return.append([])
for i in range(0,4):
if i == 0:
_ = fp.readline()
else:
ans = fp.readline()
ans = ans[:-1]
triplet_return[index].append(ans)
if i==3:
index+=1
return triplet_return
if __name__ == "__main__":
triplets_url = dataset_loader("query_and_triplets.txt")
download_files(triplets_url)
print("Triplets downloaded!")