This repository was archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
123 lines (99 loc) · 4.05 KB
/
app.py
File metadata and controls
123 lines (99 loc) · 4.05 KB
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
# -*- coding:utf-8 -*-
import os,base64,time,json
import tornado.ioloop
import tornado.web
from settings import static_path,IP,PORT
ALLOW_FILETYPE = ['.png', '.PNG', '.jpg', '.JPG', '.jpeg', '.JPEG']
class Main1Handler(tornado.web.RequestHandler):
def get(self):
return self.render('web/index1.html')
class Main2Handler(tornado.web.RequestHandler):
def get(self):
return self.render('web/index2.html')
class UploadFile2Handler(tornado.web.RequestHandler):
def post(self):
from settings import upload_path
from ocr import OCR
now_time = time.strftime('%Y-%m-%dT%H-%M-%S',time.localtime(time.time()))
dir_prefix = now_time
try:
file_metas = self.request.files['file']
except:
return self.render('web/result.html', result='negative', result_header='失败', result_content='请选择需要打印的文件。')
for meta in file_metas:
filename = meta['filename']
# valid filetype
if not os.path.splitext(filename)[1] in ALLOW_FILETYPE:
return self.render('web/result.html', result='negative', result_header='上传失败',
result_content='文件格式不支持')
try:
os.makedirs(os.path.join(upload_path, dir_prefix))
except:
pass
# save file
filepath = os.path.join(upload_path, dir_prefix, "img"+os.path.splitext(filename)[1])
with open(filepath,'wb') as up:
up.write(meta['body'])
# orc
ocrinstance = OCR()
res = ocrinstance.getResult(filepath)
statusCode = res['code']
status = '成功' if (statusCode == 1) else '失败'
text = res['text']
# save res
respath = os.path.join(upload_path, dir_prefix, 'result.txt')
with open(respath, 'w',encoding="utf-8") as info:
info.write(status+'\n'+text)
# render
if(statusCode == 1):
return self.render('web/result.html', result='positive', result_header=status, result_content=text)
else:
return self.render('web/result.html', result='negative', result_header=status, result_content=text)
class UploadFile1Handler(tornado.web.RequestHandler):
def post(self):
from settings import upload_path
from ocr import OCR
now_time = time.strftime('%Y-%m-%dT%H-%M-%S', time.localtime(time.time()))
dir_prefix = now_time
try:
base64ImgData = self.request.arguments['data'][0].decode("utf-8")
# 去除base64图片前面的说明str
base64ImgData = base64ImgData[base64ImgData.find(',') + 1:]
imgData = base64.b64decode(base64ImgData)
except:
self.finish({
'code': 0,
'message': "error"
})
try:
os.makedirs(os.path.join(upload_path, dir_prefix))
except:
pass
# save file
filepath = os.path.join(upload_path, dir_prefix, "img.png" )
with open(filepath, 'wb') as up:
up.write(imgData)
# orc
ocrinstance = OCR()
res = ocrinstance.getResult(filepath)
statusCode = res['code']
status = '成功' if (statusCode == 1) else '失败'
text = res['text']
# save res
respath = os.path.join(upload_path, dir_prefix, 'result.txt')
with open(respath, 'w', encoding="utf-8") as info:
info.write(status + '\n' + text)
self.finish({
'code': statusCode,
'message': text
})
application = tornado.web.Application([
(r"/", tornado.web.RedirectHandler,{"url":"/1","permanent":False}),
(r"/1", Main1Handler),
(r"/2", Main2Handler),
(r"/1/upload", UploadFile1Handler),
(r"/2/upload", UploadFile2Handler)],
static_path=static_path)
if __name__ == "__main__":
application.listen(PORT, address=IP)
tornado.ioloop.IOLoop.instance().start()