-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathapp.py
More file actions
114 lines (94 loc) · 2.66 KB
/
Copy pathapp.py
File metadata and controls
114 lines (94 loc) · 2.66 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
# -*- coding: utf-8 -*-
"""
@author: wenlihaoyu
"""
import web
import time
import sys
import os
from PIL import Image
import json
from glob import glob
import traceback
from read_img import read_image_label,correct_image,set_image_status
from config import IMAGEPATH,correctpath,Nmax,timeOut,ocrAuto
if ocrAuto:
##是否自动调用OCR引擎进行纠偏
from crnn.crnn_torch import crnnOcr
else:
crnnOcr=None
imagefiles =glob(IMAGEPATH)##需要纠偏的图像
imageNum = len(imagefiles)
statusDict={}##每张图片的状态
timeDict={}##每张图片的完成时间,如果超过最大时间仍然未完成纠偏,那么重新放入纠偏池
def read_batch():
data = []
index = 0
num = 0
while True:
p =imagefiles[index]
status = statusDict.get(p,0)
if status==1:
##纠偏中
if time.time()-timeDict.get(p)>timeOut:
timeDict[p] = time.time()
line = read_image_label(p)
set_image_status(p,statusDict,status=1)
line['index'] =num
data.append(line)
num+=1
elif status==0:
timeDict[p] = time.time()
line = read_image_label(p)
set_image_status(p,statusDict,status=1)
line['index'] =num
data.append(line)
num+=1
index+=1
if num == Nmax-1 or index>imageNum-1:
break
return data
class OCR:
"""
调用OCR引擎
"""
def POST(self):
data = web.data()
data = json.loads(data)
path = data['path']
im = Image.open(path).convert('L')
res = ''
if crnnOcr is not None:
res = crnnOcr(im)
return res
class Label:
def GET(self):
data =read_batch()
if data !=[]:
post = {'data':data,'labelUrl':'label','ocrUrl':'ocr'}
return render.label(post)
else:
post = {'labelUrl':'label','ocrUrl':'ocr'}
return render.label(post)
def POST(self):
"""
纠正图像中文字
"""
data = web.data()
try:
data = json.loads(data)
labelImageS = data.get('data')
correct_image(labelImageS,correctpath,statusDict,status=2)
except:
pass
return ''
#指定模板目录,并设定公共模板
urls = (
'/label', 'Label',
'/ocr','OCR'
)
app = web.application(urls, globals())
render = web.template.render('templates', base='base')
import json
if __name__ == '__main__':
app.run()