-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
77 lines (60 loc) · 2.74 KB
/
utils.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
# Numpy and pandas by default assume a narrow screen - this fixes that
from fastai.vision.all import *
from nbdev.showdoc import *
from ipywidgets import widgets
from pandas.api.types import CategoricalDtype
import matplotlib as mpl
# mpl.rcParams['figure.dpi']= 200
mpl.rcParams['savefig.dpi']= 200
mpl.rcParams['font.size']=12
set_seed(42)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
pd.set_option('display.max_columns',999)
np.set_printoptions(linewidth=200)
torch.set_printoptions(linewidth=200)
import graphviz
def gv(s): return graphviz.Source('digraph G{ rankdir="LR"' + s + '; }')
def get_image_files_sorted(path, recurse=True, folders=None): return get_image_files(path, recurse, folders).sorted()
# +
# pip install azure-cognitiveservices-search-imagesearch
from azure.cognitiveservices.search.imagesearch import ImageSearchClient as api
from msrest.authentication import CognitiveServicesCredentials as auth
def search_images_bing(key, term, min_sz=128, max_images=150):
params = {'q':term, 'count':max_images, 'min_height':min_sz, 'min_width':min_sz}
headers = {"Ocp-Apim-Subscription-Key":key}
search_url = "https://api.bing.microsoft.com/v7.0/images/search"
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()
return L(search_results['value'])
from itertools import chain
from azure.cognitiveservices.search.imagesearch import ImageSearchClient as api
from msrest.authentication import CognitiveServicesCredentials as auth
def search_images_bing_many(key, term, total_count=150, min_sz=224):
"""Search for images using the Bing API
:param key: Your Bing API key
:type key: str
:param term: The search term to search for
:type term: str
:param total_count: The total number of images you want to return (default is 150)
:type total_count: int
:param min_sz: the minimum height and width of the images to search for (default is 128)
:type min_sz: int
:returns: An L-collection of ImageObject
:rtype: L
"""
headers = {"Ocp-Apim-Subscription-Key":key}
search_url = "https://api.bing.microsoft.com/v7.0/images/search"
max_count = 150
imgs = []
for offset in range(0, total_count, max_count):
if ((total_count - offset) > max_count):
count = max_count
else:
count = total_count - offset
params = {'q':term, 'count':count, 'min_height':min_sz, 'min_width':min_sz, 'offset': offset}
response = requests.get(search_url, headers=headers, params=params)
search_results = response.json()
imgs.append(L(search_results['value']))
return L(chain(*imgs)).attrgot('contentUrl').unique()