-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflask_image2text.py
413 lines (382 loc) · 15.9 KB
/
flask_image2text.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import requests
import json
import cv2
import os
from flask import Flask, request, render_template, url_for, session, jsonify, make_response, redirect
import pytesseract
import matplotlib.pyplot as plt
import shutil
import numpy as np
app = Flask(__name__)
@app.route('/',methods=['GET'])
def selectPageMode():
return render_template('select_mode.html')
@app.route('/index',methods=['GET'])
def index():
filename = ''
config_filename = ''
rectnames = ''
rectangles = ''
scale_img= ''
if 'filename' in session:
filename = session['filename']
# if 'config_filename' in session:
# config_filename = session['config_filename']
# if 'rectnames' in session:
# rectnames = session['rectnames']
# if 'rectangles' in session:
# rectangles = session['rectangles']
# if 'scale_img' in session:
# scale_img = session['scale_img']
return render_template('view.html', filename = filename, config_filename = config_filename, rectnames = rectnames, rectangles = rectangles, scale_img=scale_img)
@app.route('/uploadImage', methods=['POST'])
def viewImage():
#print(request)
#session = None
image = request.files['file']
if image.filename == '':
return index()
print(image.filename)
filename = 'static/uploaded/' + image.filename.replace(image.filename[-4:],'.png')
image.save(filename)
print('Write file success!',filename)
session['filename'] = filename
# if 'config_filename' in session:
# config_filename = session['config_filename']
# else:
# config_filename = ''
return render_template('view.html', filename = filename, config_filename = '', rectnames = '', rectangles = '', scale_img= '')
from eastDetect import detect
@app.route('/autoDetect', methods=['POST'])
def detectRects():
rq = request.get_json()
#rectangle = rq['cur_rectangle']
scale_img = rq['scale_img']*0.6
if 'filename' not in session:
return index()
filename = session['filename']
print('Detecting...')
rectangles, rectnames = detect(filename, scale_img)
print('Detect done')
#rectangles = rectangles
print(rectangles, len(rectangles))
print(rectnames, len(rectnames))
resp = jsonify(success=True,rectangles=rectangles, rectnames=rectnames, scale_img=scale_img)
return resp
#return render_template('view.html', filename = filename, config_filename = '', rectnames = rectnames, rectangles = rectangles, scale_img= scale_img)
@app.route('/uploadConfig', methods=['POST'])
def viewConfig():
if 'filename' not in session:
return index()
a = {}
try:
config = request.files['file'].read()
config = json.loads(config)
a = json.dumps(config)
except:
return index()
print(config)
if 'config_filename' not in config:
return index()
print(config['rectangles'])
#config_filename = 'static/uploaded/' + config['config_filename'] + '.json'
config_filename = config['config_filename']
rectnames = []
rectangles = []
scale_img = float(config['scale_img'])
for rect in config['rectangles']:
rectnames.append(rect['rectname'])
rectangles.append(rect['rectangle'])
print(rectnames)
print(rectangles)
print(scale_img)
session['config_filename'] = config_filename
# session['rectnames'] = rectnames
# session['rectangles'] = rectangles
# session['scale_img'] = scale_img
return render_template('view.html', filename = session['filename'], config_filename = session['config_filename'], rectnames = rectnames, rectangles = rectangles, scale_img=scale_img)
@app.route('/save_config', methods=['POST'])
def saveConfig():
rq = request.get_json()
save_filename = rq['config_filename']
rectangles = rq['rectangles']
scale_img = rq['scale_img']
#print(rectangles)
save_filename = 'static/mau/'+save_filename+'.json'
if os.path.exists(save_filename):
print('file is exists:',save_filename)
resp = jsonify(success=False, error='File:'+save_filename+' is exists, try orther name!')
else:
shutil.copy(session['filename'], save_filename.replace('.json','.png'))
with open(save_filename,'w') as f:
json.dump(rq,f)
print('write json success:',save_filename)
resp = jsonify(success=True)
return resp
@app.route('/recognize_one_rectangle',methods=['POST'])
def recognizeCurrectangle():
rq = request.get_json()
rectangle = rq['cur_rectangle']
scale_img = rq['scale_img']*0.6
x = rectangle['x']/scale_img
y = rectangle['y']/scale_img
w = rectangle['width']/scale_img
h = rectangle['height']/scale_img
print('current rect:',rectangle)
filename = session['filename']
#print(filename)
img = cv2.imread(filename)
print(img.shape)
crop_img = img[int(y):int(y+h), int(x):int(x+w)]
#plt.imshow(crop_img[:,:,0])
#plt.show()
print(crop_img.shape)
crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2RGB)
#crop = cv2.cvtColor(crop_img, cv2.COLOR_RGB2GRAY)
#crop = cv2.GaussianBlur(crop, (1, 1), 0)
#_, crop = cv2.threshold(crop, 120 ,255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
#plt.imshow(crop)
#plt.show()
text = pytesseract.image_to_string(crop_img, config='-l vie --oem 1')
print('text_cur_rect:',text)
resp = jsonify(success=True,data=text)
return resp
@app.route('/recognize_all_rectangles',methods=['POST'])
def recognizeAllrectangle():
rq = request.get_json()
rectangles = rq['rectangles']
scale_img = rq['scale_img']*0.6
print('rectangles:',rectangles, len(rectangles))
filename = session['filename']
img = cv2.imread(filename)
print(img.shape)
texts = []
for rectangle in rectangles:
x = rectangle['x']/scale_img
y = rectangle['y']/scale_img
w = rectangle['width']/scale_img
h = rectangle['height']/scale_img
#print(filename)
print(rectangle['x'],rectangle['y'],rectangle['width'],rectangle['height'])
crop_img = img[int(y):int(y+h), int(x):int(x+w)]
#plt.imshow(crop_img[:,:,0])
#plt.show()
print(crop_img.shape)
crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2RGB)
crop_img = unsharp_mask(crop_img)
plt.imshow(crop_img[:,:,0])
plt.show()
#crop = cv2.cvtColor(crop_img, cv2.COLOR_RGB2GRAY)
#crop = cv2.GaussianBlur(crop, (1, 1), 0)
#_, crop = cv2.threshold(crop, 120 ,255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
#plt.imshow(crop)
#plt.show()
text = pytesseract.image_to_string(crop_img, config='-l vie --oem 1')
print('text_cur_rect:',text)
texts.append(text)
print(texts)
resp = jsonify(success=True,data=texts)
return resp
@app.route('/index2')
def index2():
filename = ''
config_filename = ''
rectnames = ''
rectangles = ''
scale_img= ''
list_config_files = []
if 'filename2' in session:
filename = session['filename2']
# if 'config_filename2' in session:
# config_filename = session['config_filename2']
# if 'rectnames2' in session:
# rectnames = session['rectnames2']
# if 'rectangles2' in session:
# rectangles = session['rectangles2']
# if 'scale_img2' in session:
# scale_img = session['scale_img2']
for f in os.listdir('static/mau'):
print(f.split('.')[-1])
if f.split('.')[-1] == 'json':
list_config_files.append(f.split('.')[0])
print(list_config_files)
session['list_config_files'] = list_config_files
return render_template('view2.html', filename = filename, list_config_files = list_config_files, config_filename = config_filename, rectnames = rectnames, rectangles = rectangles, scale_img=scale_img)
@app.route('/uploadImage2', methods=['POST'])
def uploadImage2():
image = request.files['file']
if image.filename == '':
return index()
print(image.filename)
filename = 'static/uploaded/' + image.filename.replace(image.filename[-4:],'.png')
image.save(filename)
#print('Write file success! '+filename)
session['filename2'] = filename
return render_template('view2.html', filename = filename, list_config_files = session['list_config_files'], config_filename = '', rectnames = '', rectangles = '', scale_img= '')
@app.route('/demo_<string:demo_img>', methods=["GET"])
def demo(demo_img):
demo_img = demo_img.replace('_','/')
print(demo_img)
shutil.copy(demo_img,demo_img.replace('demo','uploaded'))
session['filename2'] = demo_img.replace('demo','uploaded')
return index2()
@app.route('/uploadConfig2', methods=['POST'])
def viewConfig2_byupload():
if 'filename2' not in session:
print("filename not in session")
return index2()
a = {}
try:
config = request.files['file2'].read()
print(config)
config = json.loads(config)
a = json.dumps(config)
except:
print('read config request error')
return index2()
print(config)
if 'config_filename' not in config:
print('config_filename not in config')
return index2()
print(config['rectangles'])
config_filename = config['config_filename']
rectnames = []
rectangles = []
scale_img = float(config['scale_img'])
for rect in config['rectangles']:
rectnames.append(rect['rectname'])
rectangles.append(rect['rectangle'])
print(rectnames)
print(rectangles)
print(scale_img)
session['config_filename2'] = config_filename
print(rectnames)
return render_template('view2.html', filename = session['filename2'], list_config_files = session['list_config_files'], config_filename = session['config_filename2'], rectnames = rectnames, rectangles = rectangles, scale_img=scale_img)
@app.route('/<string:config_filename>',methods=["GET"])
def viewConfig_byselect(config_filename):
session['config_filename2'] = config_filename
config = []
rectnames = []
rectangles = []
scale_img = 0.0
with open('static/mau/' + config_filename + '.json') as f:
config = json.load(f)
scale_img = float(config['scale_img'])
for rect in config['rectangles']:
rectnames.append(rect['rectname'])
rectangles.append(rect['rectangle'])
return render_template('view2.html', filename = session['filename2'], list_config_files = session['list_config_files'], config_filename = session['config_filename2'], rectnames = rectnames, rectangles = rectangles, scale_img=scale_img)
@app.route('/selectconfigandalign_<string:config_filename>')
def selectConfigAndAlign(config_filename):
if 'filename2' not in session:
return index2()
session['config_filename2'] = config_filename
config = []
rectnames = []
rectangles = []
scale_img = 0.0
with open('static/mau/' + config_filename + '.json') as f:
config = json.load(f)
scale_img = float(config['scale_img'])
for rect in config['rectangles']:
rectnames.append(rect['rectname'])
rectangles.append(rect['rectangle'])
image_process_filename = session['filename2']
json_config_name = session['config_filename2']
image_confile_filename = 'static/mau/' + json_config_name.replace('.json','') + '.png'
_ = alignFile(image_confile_filename, image_process_filename)
#print(a)
return render_template('view2.html', filename = session['filename2'], list_config_files = session['list_config_files'], config_filename = session['config_filename2'], rectnames = rectnames, rectangles = rectangles, scale_img=scale_img)
@app.route('/post_align_<string:config_filename>',methods=["POST","GET"])
def postConfigAndAlign(config_filename):
if 'filename2' not in session:
return index2()
session['config_filename2'] = config_filename
config = []
rectnames = []
rectangles = []
scale_img = 0.0
with open('static/mau/' + config_filename + '.json') as f:
config = json.load(f)
scale_img = float(config['scale_img'])
for rect in config['rectangles']:
rectnames.append(rect['rectname'])
rectangles.append(rect['rectangle'])
image_process_filename = session['filename2']
json_config_name = session['config_filename2']
image_config_filename = 'static/mau/' + json_config_name.replace('.json','') + '.png'
a = alignFile(image_config_filename, image_process_filename)
session['filename2'] = a
#print(a)
resp = jsonify(success=True, rectnames=rectnames, rectangles=rectangles, scale_img=scale_img)
if request.method == "POST":
return resp
if request.method == "GET":
return render_template('view2.html', filename = session['filename2'], list_config_files = session['list_config_files'], config_filename = session['config_filename2'], rectnames = rectnames, rectangles = rectangles, scale_img=scale_img)
def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=1.0, threshold=0):
"""Return a sharpened version of the image, using an unsharp mask."""
blurred = cv2.GaussianBlur(image, kernel_size, sigma)
sharpened = float(amount + 1) * image - float(amount) * blurred
sharpened = np.maximum(sharpened, np.zeros(sharpened.shape))
sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape))
sharpened = sharpened.round().astype(np.uint8)
if threshold > 0:
low_contrast_mask = np.absolute(image - blurred) < threshold
np.copyto(sharpened, image, where=low_contrast_mask)
return sharpened
@app.route('/recognize_all_rectangles2',methods=['POST'])
def recognizeAllrectangle2():
rq = request.get_json()
rectangles = rq['rectangles']
scale_img = rq['scale_img']*0.6
filename = session['filename2']
img = cv2.imread(filename)
texts = []
for rectangle in rectangles:
x = rectangle['x']/scale_img
y = rectangle['y']/scale_img
w = rectangle['width']/scale_img
h = rectangle['height']/scale_img
#print(rectangle['x'],rectangle['y'],rectangle['width'],rectangle['height'])
crop_img = img[int(y):int(y+h), int(x):int(x+w)]
crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2RGB)
#crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
#plt.imshow(crop_img)
#plt.show()
#_, crop_img = cv2.threshold(crop_img, kmeans(input_img=img, k=8, i_val=2)[0], 255, cv2.THRESH_BINARY)
#plt.imshow(crop_img)
#plt.show()
#print(crop_img.shape)
#crop = cv2.cvtColor(crop_img, cv2.COLOR_RGB2GRAY)
#crop = cv2.GaussianBlur(crop, (1, 1), 0)
#_, crop_img = cv2.threshold(crop_img, 120 ,255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
# plt.imshow(crop)
# plt.show()
text = pytesseract.image_to_string(crop_img, config='-l vie --psm 13')
texts.append(text)
print(texts)
resp = jsonify(success=True,data=texts)
return resp
from preprocess_image import alignFile
@app.route('/preprocess_image', methods=["GET"])
def preprocessImage():
print(session['config_filename2'])
image_process_filename = session['filename2']
json_config_name = session['config_filename2']
image_confile_filename = 'static/mau/' + json_config_name.replace('.json','') + '.png'
_ = alignFile(image_confile_filename, image_process_filename)
rectnames = []
rectangles = []
scale_img = 0.0
with open('static/mau/' + json_config_name + '.json') as f:
config = json.load(f)
scale_img = float(config['scale_img'])
for rect in config['rectangles']:
rectnames.append(rect['rectname'])
rectangles.append(rect['rectangle'])
return render_template('view2.html', filename = session['filename2'], list_config_files = session['list_config_files'], config_filename = session['config_filename2'], rectnames = rectnames, rectangles = rectangles, scale_img=scale_img)
if __name__ == '__main__':
app.secret_key = 'dangvansam'
#app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(debug=True)
#app.run(debug=True, host="192.168.2.26", port=4040)