-
Notifications
You must be signed in to change notification settings - Fork 1
/
google_image_search_url.py
167 lines (121 loc) · 4.78 KB
/
google_image_search_url.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
import os
import shutil
import pandas as pd
import json
import glob
from PIL import Image
from google_images_search import GoogleImagesSearch
import uuid
from datetime import date
today = date.today()
# If you find our tools useful, please consider accept our paper,
api_key = '' # you need your own key which setup in google account.
cx = ''
api_key_list=[api_key]
import socket
if 'cv' in socket.gethostname():
# # SUN
INIT_IMAGES_DOWNLOAD_DIR = 'DoubleRightDatasetOOD/SUN_dr'
INIT_IMAGES_DOWNLOAD_URL_DIR = 'DoubleRightDatasetOOD/SUN_dr_url'
with open('GPT3/SUN_text.json', 'r') as fp:
attri_dict = json.load(fp)
os.makedirs(INIT_IMAGES_DOWNLOAD_DIR, exist_ok=True)
os.makedirs(INIT_IMAGES_DOWNLOAD_URL_DIR, exist_ok=True)
img_att_url = {}
last_end=""
attribute_end=""
stop_at=""
api_cnt = 0
continue1=True
continue2=True
failure_cnt = 0
for category in attri_dict.keys():
if stop_at == category:
break
#
# # Start from where last time ends
if category == last_end:
continue1 = False
if continue1:
continue
img_att_url[category] = {}
api_key = api_key_list[api_cnt]
att_list = attri_dict[category]
for attributes in att_list:
attributes = attributes.replace('/', ' or ').replace('-', ' ').replace('_', ' ')
if attributes == attribute_end or attribute_end=='': # if not specified, then start from thefirst attribute
continue2 = False
if continue2:
continue
img_att_url[category][attributes] = []
query_list = [f' A photo of {category} because there is {attributes}']
for qi, query in enumerate(query_list):
print('qi', qi, api_key)
failure = True
failure_cnt = 0
while failure:
_search_params = {
'q': query,
'num': 50,
'safe': '',
'fileType': '',
'imgType': '',
'imgSize': '',
'imgDominantColor': '',
'rights': ''
}
img_directory = os.path.join(INIT_IMAGES_DOWNLOAD_DIR, category, attributes, str(qi))
os.makedirs(img_directory, exist_ok=True)
url_directory = os.path.join(INIT_IMAGES_DOWNLOAD_URL_DIR, category, attributes, str(qi))
os.makedirs(url_directory, exist_ok=True)
# DEBUG
print("obj_attr_img_directory", img_directory)
print("query = ", _search_params['q'])
# Init Google Image Search every iteration to avoid repetition error # trying
gis = GoogleImagesSearch(api_key, cx)
try :
gis.search(search_params=_search_params, custom_image_name="foo")
except Exception as Error:
print ('error during query:', Error)
print('Query error with :', query)
name_idx = 0
for image in gis.results():
try:
image.download(img_directory)
# Also save URL
file1 = open(os.path.join(url_directory, f'img_url_{name_idx}.txt'), "w")
file1.writelines([image.path, '\n', image.url])
file1.close()
print('image url', image.url)
print('img path', image.path)
img_att_url[category][attributes].append(image.url)
# list_of_entity_url_tuples.append((entity_name, image.path, image.url))
# print(query, image.path, image.url)
# renamed_img_path = os.path.join(img_directory, f"image_{name_idx}.jpg")
name_idx += 1
except:
print('failed one')
if name_idx > 0:
# success
failure = False
else:
# failure
failure_cnt += 1
api_cnt += 1
api_key = api_key_list[api_cnt]
print("using api #", api_cnt, api_key)
if failure_cnt > 2:
print("the last one is", category, attributes, query)
uuidname = str(uuid.uuid4())[:6]
with open(f'{today}_{uuidname}_unfinished_{category}_{attributes}.json', 'w') as f2:
json.dump(img_att_url, f2)
break
if failure_cnt > 2:
break
if failure_cnt > 2:
break
if failure_cnt > 2:
break
uuidname = str(uuid.uuid4())[:6]
with open(f'{today}_{uuidname}_finished.json', 'w') as f2:
json.dump(img_att_url, f2)