-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette_sandboxed.py
98 lines (85 loc) · 3.06 KB
/
palette_sandboxed.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
from flask import Flask, request, jsonify
import urllib
import base64
import json
import requests
import os
app = Flask(__name__)
default_port = 52892
GOOGLE_CLOUD_VISION_API_URL = 'https://vision.googleapis.com/v1/images:annotate?key=AIzaSyD6_ObTCpA5dB8Ep8CO_5Xj-ozgVdI0IU0'
CSE = '006499618149113892410:le0v1x-qmng'
api_key = 'AIzaSyD6_ObTCpA5dB8Ep8CO_5Xj-ozgVdI0IU0'
def goog_cloud_vison (image_content):
api_url = GOOGLE_CLOUD_VISION_API_URL
req_body = json.dumps({
'requests': [{
'image': {
'content': image_content
},
'features': [{
'type': 'LABEL_DETECTION',
'maxResults': 10,
},{
'type': 'LOGO_DETECTION',
'maxResults': 10}]
}]
})
res = requests.post(api_url, data=req_body)
return res.json()
@app.route('/', methods=['GET'])
def hello():
api_dict = {
'classify route(Get, Post)' : '/api/classify/'
}
return api_dict, 200
@app.route('/api/classify/', methods=['GET', 'POST'])
def classify():
if request.form:
res_json = goog_cloud_vison(request.form['data'])
print(res_json)
res_json['description'] = 'Label Detection (Google Cloud Vision)'
descriptions = [None] * 10
index = 0
for i in res_json['responses'][0]['labelAnnotations']:
descriptions[index] = i['description']
index += 1
descriptions = {'descriptions': descriptions}
return searchParses(descriptions)
else:
print("in the if")
f = open("vie.jpg",'r+')
img_jpg = f.read()
image_content = base64.b64encode(img_jpg)
res_json = goog_cloud_vison(image_content)
res_json['description'] = 'Label Detection (Google Cloud Vision)'
descriptions = [None] * 10
index = 0
for i in res_json['responses'][0]['labelAnnotations']:
descriptions [index] = i['description']
index += 1
descriptions = {'descriptions':descriptions}
return searchParses(descriptions)
def google_search(search_term, api_key, cse_id, **kwargs):
service = build("customsearch", "v1", developerKey=api_key)
res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
return res['items']
def searchParses(descriptions):
query = ""
for i in range(3):
query += (descriptions['descriptions'][i] + ' ')
subscription_key = "e9dbf70ca16c4533932bd31b5bb204bb"
assert subscription_key
search_url = "https://api.cognitive.microsoft.com/bing/v7.0/search"
headers = {"Ocp-Apim-Subscription-Key" : subscription_key}
params = {"q": query, "textDecorations":True, "textFormat":"HTML"}
results = google_search(query, my_api_key, my_cse_id, num=10)
response = requests.get(search_url, headers = headers, params = params)
response.raise_for_status()
search_results = response.json()
webSites = [None] * 5
for i in range(5):
webSites[i] = (search_results['webPages']['value'][i]['url'])
webSites = {'webSites' : webSites}
return jsonify(webSites)
if __name__ == '__main__':
app.run()