-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
317 lines (293 loc) · 11.5 KB
/
server.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import boto3
import os
import requests
#import colorgram
#import webcolors
import time
import numpy as np
#import cv2
#import statistics
import pandas as pd
import shlex
import subprocess
from flask import Flask, render_template, request
from libsoundtouch import soundtouch_device
from libsoundtouch.utils import Source, Type
#from json2table import convert
from werkzeug import secure_filename
os.environ['AWS_DEFAULT_REGION'] = 'us-east-2'
def is_nan(x):
return (x is np.nan or x != x)
app = Flask(__name__)
@app.route('/')
def index():
return render_template('template.html')
def color(file):
colors = colorgram.extract(file, 2)
first_color = colors[1]
rgb = first_color.rgb
return (rgb)
def closest_colour(requested_colour):
min_colours = {}
for key, name in webcolors.css3_hex_to_names.items():
r_c, g_c, b_c = webcolors.hex_to_rgb(key)
rd = (r_c - requested_colour[0]) ** 2
gd = (g_c - requested_colour[1]) ** 2
bd = (b_c - requested_colour[2]) ** 2
min_colours[(rd + gd + bd)] = name
return min_colours[min(min_colours.keys())]
def get_colour_name(requested_colour):
try:
closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
except ValueError:
closest_name = closest_colour(requested_colour)
actual_name = None
return actual_name, closest_name
@app.route('/my-link/', methods = ['GET', 'POST'])
def main():
# Getting the emotions from the IBM Watson Cloud Visual Recognition
# cmd = '''curl -u "apikey:9t_w3I8yzOheBd9syHpyPFEeCN21DSw0NX8tnYJCvdBe" "https://gateway.watsonplatform.net/visual-recognition/api/v3/classify?url=https://us.123rf.com/450wm/bowie15/bowie151401/bowie15140100080/39843138-sad-man.jpg?ver=6&version=2018-03-19&classifier_ids=DefaultCustomModel_1963778161"
# '''
# args = shlex.split(cmd)
# process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# stdout, stderr = process.communicate()
# stdout1 = stdout.decode("utf-8")
url ='https://gateway.watsonplatform.net/visual-recognition/api/v3/classify?version=2016-04-19&classifier_ids=DefaultCustomModel_2222222'
files = {'images_file': open("./XTYZZ.jpg",'rb')}
stdout = requests.post(url, auth=('apikey','XXXXXXXXXXXXXXXX'), files=files)
stdout1 = str(stdout.content)
# Splitting the string returned to get the emotion "Sad" "Happy" "Angry" "Depressed"
emotion = stdout1.split('"class":')[1].split('",')[0].replace(' "','')
print(emotion)
id = ""
if emotion == "Happy":
id = "spotify:playlist:XDXDXDXDXD"
cmd = '''curl -d \\ "<play_info><app_key>XXXXXDXDXDXD</app_key><url>http://www.fromtexttospeech.com/output/111111/222222.mp3</url><service>service text</service><reason>reason text</reason><message>message text</message><volume>25</volume></play_info>" http://192.170.100.160:9090/speaker'''
elif emotion == "Sad":
id = "spotify:playlist:DDDDDDDDDDD"
cmd = '''curl -d \\ "<play_info><app_key>DDDDDDDDDDDD</app_key><url>http://www.fromtexttospeech.com/output/222222/333333.mp3</url><service>service text</service><reason>reason text</reason><message>message text</message><volume>25</volume></play_info>" http://192.170.100.160:9090/speaker'''
elif emotion == "Angry":
id = "spotify:playlist:XXXXXXXXXX"
cmd = '''curl -d \\ "<play_info><app_key>XXXXXXXXXX</app_key><url>http://www.fromtexttospeech.com/output/333333/444444.mp3</url><service>service text</service><reason>reason text</reason><message>message text</message><volume>25</volume></play_info>" http://192.170.100.160:9090/speaker'''
args = shlex.split(cmd)
process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
st, sd = process.communicate()
st1 = st.decode("utf-8")
time.sleep(10)
# Switching on the device
device = soundtouch_device('193.170.100.120')
device.power_on()
# Config object
print(device.config.name)
# Status object
# device.status() will do an HTTP request. Try to cache this value if needed.
#device.play_media(Source.INTERNET_RADIO, '4712')
#device.play()
#device.pause()
#Spotify Username
spot_user_id = 'mnuk68asijvbj6nzapegd4p94' # Should be filled in with your Spotify userID
# This userID can be found by playing Spotify on the
# connected SoundTouch speaker, and calling
#print(device.status().content_item.source_account)
# time.sleep(10)
device.play_media(Source.SPOTIFY,id,spot_user_id)
return ""
#device.play()
# if request.method == 'POST':
# f = request.files['file']
# f.save(secure_filename(f.filename))
# file = str(f.filename)
#
# # print(file)
# #
# # requested_colour = color(file)
# # actual_name, closest_name = get_colour_name(requested_colour)
# # print ("Actual colour name:", actual_name, ", closest colour name:", closest_name)
# # aN = str(actual_name)
# # cN = str(closest_name)
# # return(cN)
#
#
# # @app.route('/my-link/')
# # def main():
# # file="test_image_10.jpg"
# # requested_colour = color(file)
# # actual_name, closest_name = get_colour_name(requested_colour)
# # print ("Actual colour name:", actual_name, ", closest colour name:", closest_name)
# # aN = str(actual_name)
# # cN = str(closest_name)
# # print(cN)
# # return(cN)
#
# s3 = boto3.client('s3')
# bucket = 'avadakadaba'
# photo = file
# s3.upload_file(photo, bucket, photo)
# client = boto3.client('rekognition')
# response = client.detect_text(Image={'S3Object': {'Bucket': 'avadakadaba', 'Name': photo}})
# textDetections = response['TextDetections']
# text2 = ""
# for text in textDetections:
# if text['DetectedText'] not in text2:
# text2 = text2 + text['DetectedText']
# text2 = ''.join(text2.split())
#
# # Getting color
# requested_colour = color(photo)
# actual_name, closest_name = get_colour_name(requested_colour)
# if "grey" in closest_name:
# closest_name = "WHITE"
# if "rose" in closest_name:
# closest_name = "PINK"
# if "red" in closest_name:
# closest_name = "RED"
# if "yellow" in closest_name:
# closest_name = "YELLOW"
# if "blue" in closest_name:
# closest_name = "BLUE"
#
#
# # Getting shape
# shape=""
# img = cv2.imread(photo)
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# gray = cv2.Canny(np.asarray(gray), 50, 250)
#
# _,contours, h = cv2.findContours(gray, 1, 2)
#
# avgArray = []
# for cnt in contours:
# approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True), True)
# avgArray.append(len(approx))
#
# # print((avgArray))
# edges = statistics.median(avgArray)
# # print(edges)
#
# if edges < 15:
# shape = "OVAL"
# # cv2.drawContours(photo, [cnt], 0, 255, -1)
# # elif edges == 3:
# # print("triangle")
# # cv2.drawContours(img, [cnt], 0, (0, 255, 0), -1)
# # elif edges == 4:
# # print("square")
# # cv2.drawContours(img, [cnt], 0, (0, 0, 255), -1)
# # elif edges == 9:
# # print("half-circle")
# # cv2.drawContours(img, [cnt], 0, (255, 255, 0), -1)
# elif edges > 15:
#
# shape = "CIRCLE"
#
# data = {"uploadName":photo,"text":text2,"color":closest_name,"shape":shape}
# print(data)
# # print(data)
#
# dataframe = pd.read_csv("out.csv")
#
# for index, row in dataframe.iterrows():
# name = str(row["Imprint"]).replace(";","")
# if not is_nan(row["Name"]):
# if name == text2 and row["Color"] == color and row["Shape"] == shape:
# return '''<style>
# table, th, td {
# border: 1px solid black;
# border-collapse: collapse;
# }
# th, td {
# padding: 5px;
# text-align: left;
# }
# </style><b>Pill Details</b><table style="width:100%">
# <tr>
# <th>Author</th>
# <th>Name</th>
# <th>Color</td>
# <th>Imprint</td>
# <th>Size</td>
# <th>Shape</td>
# <th>Ingredients</td>
# </tr>
# <tr>
# <td>'''+str(row["Author"])+'''</td>
# <td>'''+str(row["Name"])+'''</td>
# <td>'''+str(row["Color"])+'''</td>
# <td>'''+str(row["Imprint"])+'''</td>
# <td>'''+str(row["Size"])+'''</td>
# <td>'''+str(row["Shape"])+'''</td>
# <td>'''+str(row["Ingredients"])+'''</td>
# </tr>
# </table>'''
#
#
#
# for index, row in dataframe.iterrows():
# name = str(row["Imprint"]).replace(";","")
# if not is_nan(row["Name"]):
# if name == text2 and row["Color"] == color:
# return '''<style>
# table, th, td {
# border: 1px solid black;
# border-collapse: collapse;
# }
# th, td {
# padding: 5px;
# text-align: left;
# }
# </style><b>Pill Details</b><table style="width:100%">
# <tr>
# <th>Author</th>
# <th>Name</th>
# <th>Color</td>
# <th>Imprint</td>
# <th>Size</td>
# <th>Shape</td>
# <th>Ingredients</td>
# </tr>
# <tr>
# <td>'''+str(row["Author"])+'''</td>
# <td>'''+str(row["Name"])+'''</td>
# <td>'''+str(row["Color"])+'''</td>
# <td>'''+str(row["Imprint"])+'''</td>
# <td>'''+str(row["Size"])+'''</td>
# <td>'''+str(row["Shape"])+'''</td>
# <td>'''+str(row["Ingredients"])+'''</td>
# </tr>
# </table>'''
#
# for index, row in dataframe.iterrows():
# name = str(row["Imprint"]).replace(";","")
# if not is_nan(row["Name"]):
# if name == text2:
# return '''<style>
# table, th, td {
# border: 1px solid black;
# border-collapse: collapse;
# }
# th, td {
# padding: 5px;
# text-align: left;
# }
# </style><b>Pill Details</b><table style="width:100%">
# <tr>
# <th>Author</th>
# <th>Name</th>
# <th>Color</td>
# <th>Imprint</td>
# <th>Size</td>
# <th>Shape</td>
# <th>Ingredients</td>
# </tr>
# <tr>
# <td>'''+str(row["Author"])+'''</td>
# <td>'''+str(row["Name"])+'''</td>
# <td>'''+str(row["Color"])+'''</td>
# <td>'''+str(row["Imprint"])+'''</td>
# <td>'''+str(row["Size"])+'''</td>
# <td>'''+str(row["Shape"])+'''</td>
# <td>'''+str(row["Ingredients"])+'''</td>
# </tr>
# </table>'''
if __name__ == "__main__":
app.run(debug=True)