forked from goberoi/cloudy_vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_vision.py
158 lines (118 loc) · 5.44 KB
/
cloud_vision.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
import cv2
from jinja2 import FileSystemLoader, Environment
import json
import numpy
import os
import pprint
import shutil
import time
import vendors.google
import vendors.microsoft
import vendors.clarifai_
import vendors.ibm
import vendors.cloudsight_
SETTINGS = None
def settings(name):
"""Fetch a settings parameter."""
# Initialize settings if necessary.
global SETTINGS
if SETTINGS is None:
# Change this dict to suit your taste.
SETTINGS = {
'api_keys_filepath' : './api_keys.json',
'input_images_dir' : 'input_images',
'output_dir' : 'output',
'static_dir' : 'static',
'output_image_height' : 200,
'vendors' : {
'google' : vendors.google,
'msft' : vendors.microsoft,
'clarifai' : vendors.clarifai_,
'ibm' : vendors.ibm,
'cloudsight' : vendors.cloudsight_
}
}
# Load API keys
with open(SETTINGS['api_keys_filepath']) as data_file:
SETTINGS['api_keys'] = json.load(data_file)
return SETTINGS[name]
def log_status(filepath, vendor_name, msg):
filename = os.path.basename(filepath)
print("%s -> %s" % ((filename + ", " + vendor_name).ljust(40), msg))
def resize_and_save(input_image_filepath, output_image_filepath):
image = cv2.imread(input_image_filepath)
height = image.shape[0]
width = image.shape[1]
aspect_ratio = float(width) / float(height)
new_height = settings('output_image_height')
new_width = int(aspect_ratio * new_height)
output_image = cv2.resize(image, (new_width, new_height))
cv2.imwrite(output_image_filepath, output_image)
def render_from_template(directory, template_name, **kwargs):
loader = FileSystemLoader(directory)
env = Environment(loader=loader)
template = env.get_template(template_name)
return template.render(**kwargs)
def process_all_images():
image_results = []
# Create the output directory
if not os.path.exists(settings('output_dir')):
os.makedirs(settings('output_dir'))
# Loop through all input images.
for filename in os.listdir(settings('input_images_dir')):
# Only process files that have these image extensions.
if not filename.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
continue
# Create a full path so we can read these files.
filepath = os.path.join(settings('input_images_dir'), filename)
# Create an output object for the image
image_result = {
'input_image_filepath' : filepath,
'output_image_filepath' : filename,
'vendors' : []
}
image_results.append(image_result)
# Walk through all vendor APIs to call.
for vendor_name, vendor_module in sorted(settings('vendors').iteritems(), reverse=True):
# Figure out filename to store and retrive cached JSON results.
output_json_filename = filename + "." + vendor_name + ".json"
output_json_path = os.path.join(settings('output_dir'), output_json_filename)
# And where to store the output image
output_image_filepath = os.path.join(settings('output_dir'), filename)
# Check if the call is already cached.
if os.path.isfile(output_json_path):
# If so, read the result from the .json file stored in the output dir.
log_status(filepath, vendor_name, "skipping API call, already cached")
with open(output_json_path, 'r') as infile:
raw_api_result = infile.read()
else:
# If not, make the API call for this particular vendor.
log_status(filepath, vendor_name, "calling API")
raw_api_result = vendor_module.call_vision_api(filepath, settings('api_keys'))
# And cache the result in a .json file
log_status(filepath, vendor_name, "success, storing result in %s" % output_json_path)
with open(output_json_path, 'w') as outfile:
outfile.write(raw_api_result)
# Resize the original image and write to an output filename
log_status(filepath, vendor_name, "writing output image in %s" % output_image_filepath)
resize_and_save(filepath, output_image_filepath)
# Sleep so we avoid hitting throttling limits
time.sleep(1)
# Parse the JSON result we fetched (via API call or from cache)
api_result = json.loads(raw_api_result)
standardized_result = vendor_module.get_standardized_result(api_result)
image_result['vendors'].append({
'api_result' : api_result,
'vendor_name' : vendor_name,
'standardized_result' : standardized_result,
'output_json_filename' : output_json_filename
})
# Render HTML file with all results.
output_html = render_from_template('.', os.path.join(settings('static_dir'), 'template.html'), image_results=image_results)
# Write HTML output.
output_html_filepath = os.path.join(settings('output_dir'), 'output.html')
with open(output_html_filepath, 'w') as output_html_file:
output_html_file.write(output_html)
if __name__ == "__main__":
process_all_images()
# print vendors.clarifai_.get_acces_token(settings('api_keys'))