-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
191 lines (154 loc) · 4.67 KB
/
app.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
import flask
import numpy as np
from cv2 import cv2
import base64
import json
#####
def nothing(x):
pass
image_x, image_y = 64,64
from keras.models import load_model
classifier = load_model('Trained_model.h5')
from keras.preprocessing import image
# graph = tf.get_default_graph()
def predictor():
test_image = image.load_img('1.png', target_size=(64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
import keras.backend.tensorflow_backend as tb
tb._SYMBOLIC_SCOPE.value = True
result = classifier.predict(test_image)
if result[0][0] == 1:
return 'A'
elif result[0][1] == 1:
return 'B'
elif result[0][2] == 1:
return 'C'
elif result[0][3] == 1:
return 'D'
elif result[0][4] == 1:
return 'E'
elif result[0][5] == 1:
return 'F'
elif result[0][6] == 1:
return 'G'
elif result[0][7] == 1:
return 'H'
elif result[0][8] == 1:
return 'I'
elif result[0][9] == 1:
return 'J'
elif result[0][10] == 1:
return 'K'
elif result[0][11] == 1:
return 'L'
elif result[0][12] == 1:
return 'M'
elif result[0][13] == 1:
return 'N'
elif result[0][14] == 1:
return 'O'
elif result[0][15] == 1:
return 'P'
elif result[0][16] == 1:
return 'Q'
elif result[0][17] == 1:
return 'R'
elif result[0][18] == 1:
return 'S'
elif result[0][19] == 1:
return 'T'
elif result[0][20] == 1:
return 'U'
elif result[0][21] == 1:
return 'V'
elif result[0][22] == 1:
return 'W'
elif result[0][23] == 1:
return 'X'
elif result[0][24] == 1:
return 'Y'
elif result[0][25] == 1:
return 'Z'
else:
return ' '
#####
app=flask.Flask(__name__)
@app.route('/')
def home():
return'<h1>WELCOME to 0.0.0.0</h1>';
@app.route('/predict',methods=["POST"])
def start():
data={'str_image':""}
if (flask.request.method == "POST"):
count=0
d_img=flask.request.json
#.encode is to convert string to bytes again
imageString = base64.b64decode(d_img['image'].encode())
imageString1=d_img['image'].encode()
print(len(imageString1))
print(len(imageString))
#print(imageString)
if len(imageString)==78400:
nparr = np.fromstring(imageString, np.uint8)
img = nparr.reshape(280,280)
cv2.imwrite("input.jpg",img)
#print(nparr.shape)
#img = (decode_img['image'])
#jpg_as_np = np.frombuffer(jpg_original, dtype=np.uint8)
#img_decode = cv2.imdecode(jpg_as_np, cv2.IMREAD_COLOR)
#img=cv2.imread(img_decode)
# img_HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# HSV_mask = cv2.inRange(img_HSV, (0, 15, 0), (17,170,255))
# HSV_mask = cv2.morphologyEx(HSV_mask, cv2.MORPH_OPEN, np.ones((3,3), np.uint8))
# img_YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
# YCrCb_mask = cv2.inRange(img_YCrCb, (0, 135, 85), (255,180,135))
# YCrCb_mask = cv2.morphologyEx(YCrCb_mask, cv2.MORPH_OPEN, np.ones((3,3), np.uint8))
# global_mask=cv2.bitwise_and(YCrCb_mask,HSV_mask)
# global_mask=cv2.medianBlur(global_mask,3)
# global_mask = cv2.morphologyEx(global_mask, cv2.MORPH_OPEN, np.ones((4,4), np.uint8))
# HSV_result = cv2.bitwise_not(global_mask)
# cv2.waitKey(1)
# jpgt = base64.b64encode(global_mask)
# jpg_as_text=(jpgt).decode()
# #print("asdsasadsads")
# data['str_image']=jpg_as_text
#######
# img_counter = 0
# res=''
# img_text = ''
# # while True:
# # ret, frame = cam.read()
# # frame = cv2.flip(frame,1)
# l_h =130
# l_s = 0
# l_v =30
# u_h = 179
# u_s = 255
# u_v = 255
# frame = img
# img = cv2.rectangle(frame, (425,100),(625,300), (0,255,0), thickness=2, lineType=8, shift=0)
# lower_blue = np.array([l_h, l_s, l_v])
# upper_blue = np.array([u_h, u_s, u_v])
# imcrop = img#[102:298, 427:623]
# hsv = cv2.cvtColor(imcrop, cv2.COLOR_BGR2HSV)
# mask = cv2.inRange(hsv, lower_blue, upper_blue)
# cv2.putText(frame, img_text, (30, 400), cv2.FONT_HERSHEY_TRIPLEX, 1.5, (0, 255, 0))
# cv2.imshow("test", frame)
# cv2.imshow("mask", mask)
# #if cv2.waitKey(1) == ord('c'):
img_name = "1.png"
# save_img = cv2.resize(mask, (image_x, image_y))
cv2.imwrite(img_name,img)
# cv2.imwrite("2.png",frame)
# print("image", len(save_img))
img_text = predictor()
print(img_text)
data['predicted']=img_text
else:
data['predicted']=' '
return flask.jsonify(data)
if __name__ == '__main__':
print("sagar")
# app.run('0.0.0.0',debug=True)
app.run("0.0.0.0", threaded=False)