forked from chineseocr/chineseocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocrjob.py
67 lines (58 loc) · 1.81 KB
/
ocrjob.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 22 02:02:29 2019
job
@author: chineseocr
"""
from apphelper.redisbase import redisDataBase
from config import *
from crnn.keys import alphabetChinese,alphabetEnglish
if ocrFlag=='keras':
if GPU:
os.environ["CUDA_VISIBLE_DEVICES"] = str(GPUID)
import tensorflow as tf
from keras import backend as K
config = tf.ConfigProto()
config.gpu_options.allocator_type = 'BFC'
config.gpu_options.per_process_gpu_memory_fraction = 0.1## GPU最大占用量
config.gpu_options.allow_growth = True##GPU是否可动态增加
K.set_session(tf.Session(config=config))
K.get_session().run(tf.global_variables_initializer())
else:
##CPU启动
os.environ["CUDA_VISIBLE_DEVICES"] = ''
if ocrFlag=='keras':
from crnn.network_keras import CRNN
if chineseModel:
alphabet = alphabetChinese
if LSTMFLAG:
ocrModel = ocrModelKerasLstm
else:
ocrModel = ocrModelKerasDense
else:
ocrModel = ocrModelKerasEng
alphabet = alphabetEnglish
LSTMFLAG = True
else:
from crnn.network_torch import CRNN
if chineseModel:
alphabet = alphabetChinese
if LSTMFLAG:
ocrModel = ocrModelTorchLstm
else:
ocrModel = ocrModelTorchDense
else:
ocrModel = ocrModelTorchEng
alphabet = alphabetEnglish
LSTMFLAG = True
nclass = len(alphabet)+1
ocr = CRNN( 32, 1, nclass, 256, leakyRelu=False,lstmFlag=LSTMFLAG,GPU=GPU,alphabet=alphabet)
if os.path.exists(ocrModel):
ocr.load_weights(ocrModel)
else:
print("download model or tranform model with tools!")
if __name__=='__main__':
redisJob = redisDataBase()
while True:
redisJob.get_job(ocr.predict)