Spaces:
Sleeping
Sleeping
vk
commited on
Commit
·
6c55012
1
Parent(s):
9d5ed96
first commit
Browse files- .gitignore +3 -0
- .idea/svtr-ocr-gradio.iml +8 -0
- app.py +21 -0
- dict.txt +185 -0
- models/ocr_fp16.bin +3 -0
- models/ocr_fp16.xml +0 -0
- ocr_inference.py +168 -0
- requirements.txt +3 -0
- utils.py +115 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.ipynb_checkpoints
|
| 2 |
+
ocr-test/
|
| 3 |
+
# *.py
|
.idea/svtr-ocr-gradio.iml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$" />
|
| 5 |
+
<orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" />
|
| 6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
+
</component>
|
| 8 |
+
</module>
|
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ocr_inference import OCR
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
def get_response(input_img):
|
| 5 |
+
|
| 6 |
+
return ocr.predict([input_img])
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
if __name__ == "__main__":
|
| 10 |
+
ocr=OCR('models/ocr_fp16.xml')
|
| 11 |
+
iface = gr.Interface(
|
| 12 |
+
fn=get_response,
|
| 13 |
+
inputs=gr.Image(type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|
| 63 |
+
`
|
| 64 |
+
a
|
| 65 |
+
b
|
| 66 |
+
c
|
| 67 |
+
d
|
| 68 |
+
e
|
| 69 |
+
f
|
| 70 |
+
g
|
| 71 |
+
h
|
| 72 |
+
i
|
| 73 |
+
j
|
| 74 |
+
k
|
| 75 |
+
l
|
| 76 |
+
m
|
| 77 |
+
n
|
| 78 |
+
o
|
| 79 |
+
p
|
| 80 |
+
q
|
| 81 |
+
r
|
| 82 |
+
s
|
| 83 |
+
t
|
| 84 |
+
u
|
| 85 |
+
v
|
| 86 |
+
w
|
| 87 |
+
x
|
| 88 |
+
y
|
| 89 |
+
z
|
| 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 |
+
™
|
models/ocr_fp16.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4ace9c8fd8dba1a0f71a8bef0bd1f2db42af5640728ab6dfcdd4e1e4ae8cbaad
|
| 3 |
+
size 4430022
|
models/ocr_fp16.xml
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
ocr_inference.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''
|
| 2 |
+
Copyright 2023 Vignesh(VK)Kotteeswaran <[email protected]>
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 7 |
+
Unless required by applicable law or agreed to in writing, software
|
| 8 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 9 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 10 |
+
See the License for the specific language governing permissions and
|
| 11 |
+
limitations under the License.
|
| 12 |
+
'''
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
import numpy as np
|
| 16 |
+
from openvino.runtime import Core
|
| 17 |
+
import math
|
| 18 |
+
import cv2
|
| 19 |
+
from utils import CTCLabelDecode
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class OCR():
|
| 28 |
+
|
| 29 |
+
def __init__(self,model_path):
|
| 30 |
+
|
| 31 |
+
ie = Core()
|
| 32 |
+
|
| 33 |
+
print('\n',model_path)
|
| 34 |
+
|
| 35 |
+
model = ie.read_model(model=model_path)
|
| 36 |
+
self.compiled_model = ie.compile_model(model=model, device_name="CPU")
|
| 37 |
+
|
| 38 |
+
self.input_layer = self.compiled_model.input(0)
|
| 39 |
+
self.output_layer = self.compiled_model.output(0)
|
| 40 |
+
self.decoder=CTCLabelDecode('dict.txt',True)
|
| 41 |
+
self.show_frame=None
|
| 42 |
+
self.image_shape=None
|
| 43 |
+
self.dynamic_width=False
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def img_decode(self,img):
|
| 50 |
+
|
| 51 |
+
img = np.frombuffer(img, dtype='uint8')
|
| 52 |
+
img=cv2.imdecode(img, 1)
|
| 53 |
+
#print(img.shape)
|
| 54 |
+
|
| 55 |
+
return img
|
| 56 |
+
|
| 57 |
+
def preprocess_img(self,img):
|
| 58 |
+
|
| 59 |
+
grayscale_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
|
| 60 |
+
|
| 61 |
+
# Create an empty array of shape (height, width, 3) for the stacked image
|
| 62 |
+
stacked_image = np.zeros((grayscale_image.shape[0], grayscale_image.shape[1], 3), dtype=np.uint8)
|
| 63 |
+
|
| 64 |
+
# Assign the grayscale image to each channel of the stacked image
|
| 65 |
+
stacked_image[:, :, 0] = grayscale_image
|
| 66 |
+
stacked_image[:, :, 1] = grayscale_image
|
| 67 |
+
stacked_image[:, :, 2] = grayscale_image
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
return self.resize_norm_img(stacked_image)
|
| 71 |
+
|
| 72 |
+
def resize_norm_img(self,img,
|
| 73 |
+
padding=True,
|
| 74 |
+
interpolation=cv2.INTER_LINEAR):
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
self.image_shape=[3,48,int(img.shape[1]*2)]
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
imgC,imgH,imgW=self.image_shape
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# todo: change to 0 and modified image shape
|
| 84 |
+
max_wh_ratio = imgW * 1.0 / imgH
|
| 85 |
+
h, w = img.shape[0], img.shape[1]
|
| 86 |
+
ratio = w * 1.0 / h
|
| 87 |
+
max_wh_ratio = min(max(max_wh_ratio, ratio), max_wh_ratio)
|
| 88 |
+
imgW = int(imgH * max_wh_ratio)
|
| 89 |
+
|
| 90 |
+
if math.ceil(imgH * ratio) > imgW:
|
| 91 |
+
resized_w = imgW
|
| 92 |
+
else:
|
| 93 |
+
resized_w = int(math.ceil(imgH * ratio))
|
| 94 |
+
resized_image = cv2.resize(img, (resized_w, imgH))
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
self.show_frame=resized_image
|
| 98 |
+
resized_image = resized_image.astype('float32')
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
if self.image_shape[0] == 1:
|
| 103 |
+
resized_image = resized_image / 255
|
| 104 |
+
|
| 105 |
+
resized_image = resized_image[np.newaxis, :]
|
| 106 |
+
else:
|
| 107 |
+
|
| 108 |
+
resized_image = resized_image.transpose((2, 0, 1)) / 255
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
resized_image -= 0.5
|
| 113 |
+
resized_image /= 0.5
|
| 114 |
+
padding_im = np.zeros((imgC, imgH, imgW), dtype=np.float32)
|
| 115 |
+
padding_im[:, :, 0:resized_w] = resized_image
|
| 116 |
+
|
| 117 |
+
return padding_im
|
| 118 |
+
|
| 119 |
+
def predict(self,src):
|
| 120 |
+
|
| 121 |
+
imgs=[]
|
| 122 |
+
show_frames=[]
|
| 123 |
+
|
| 124 |
+
for item in src:
|
| 125 |
+
|
| 126 |
+
if hasattr(item,'shape'):
|
| 127 |
+
|
| 128 |
+
imgs.append(np.expand_dims(self.preprocess_img(item),axis=0))
|
| 129 |
+
|
| 130 |
+
elif isinstance(item,'str'):
|
| 131 |
+
|
| 132 |
+
with open(item, 'rb') as f:
|
| 133 |
+
content=f.read()
|
| 134 |
+
imgs.append(np.expand_dims(self.preprocess_img(self.img_decode(content)),axis=0))
|
| 135 |
+
|
| 136 |
+
else:
|
| 137 |
+
return "Error: Invalid Input"
|
| 138 |
+
|
| 139 |
+
show_frames.append(self.show_frame)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
blob=np.concatenate(imgs,axis=0).astype(np.float32)
|
| 143 |
+
|
| 144 |
+
outputs = self.compiled_model([blob])[self.output_layer]
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
texts=[]
|
| 148 |
+
|
| 149 |
+
for output in outputs:
|
| 150 |
+
|
| 151 |
+
output=np.expand_dims(output,axis=0)
|
| 152 |
+
|
| 153 |
+
curr_text=self.decoder(output)[0][0]
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
texts.append(curr_text)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
return texts[0]
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python==4.5.3.56
|
| 2 |
+
gradio
|
| 3 |
+
openvino
|
utils.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class BaseRecLabelDecode(object):
|
| 6 |
+
""" Convert between text-label and text-index """
|
| 7 |
+
|
| 8 |
+
def __init__(self, character_dict_path=None, use_space_char=False):
|
| 9 |
+
self.beg_str = "sos"
|
| 10 |
+
self.end_str = "eos"
|
| 11 |
+
self.reverse = False
|
| 12 |
+
self.character_str = []
|
| 13 |
+
|
| 14 |
+
if character_dict_path is None:
|
| 15 |
+
self.character_str = "0123456789abcdefghijklmnopqrstuvwxyz"
|
| 16 |
+
dict_character = list(self.character_str)
|
| 17 |
+
else:
|
| 18 |
+
with open(character_dict_path, "rb") as fin:
|
| 19 |
+
lines = fin.readlines()
|
| 20 |
+
for line in lines:
|
| 21 |
+
line = line.decode('utf-8').strip("\n").strip("\r\n")
|
| 22 |
+
self.character_str.append(line)
|
| 23 |
+
if use_space_char:
|
| 24 |
+
self.character_str.append(" ")
|
| 25 |
+
dict_character = list(self.character_str)
|
| 26 |
+
if 'arabic' in character_dict_path:
|
| 27 |
+
self.reverse = True
|
| 28 |
+
|
| 29 |
+
dict_character = self.add_special_char(dict_character)
|
| 30 |
+
self.dict = {}
|
| 31 |
+
for i, char in enumerate(dict_character):
|
| 32 |
+
self.dict[char] = i
|
| 33 |
+
self.character = dict_character
|
| 34 |
+
|
| 35 |
+
def pred_reverse(self, pred):
|
| 36 |
+
pred_re = []
|
| 37 |
+
c_current = ''
|
| 38 |
+
for c in pred:
|
| 39 |
+
if not bool(re.search('[a-zA-Z0-9 :*./%+-]', c)):
|
| 40 |
+
if c_current != '':
|
| 41 |
+
pred_re.append(c_current)
|
| 42 |
+
pred_re.append(c)
|
| 43 |
+
c_current = ''
|
| 44 |
+
else:
|
| 45 |
+
c_current += c
|
| 46 |
+
if c_current != '':
|
| 47 |
+
pred_re.append(c_current)
|
| 48 |
+
|
| 49 |
+
return ''.join(pred_re[::-1])
|
| 50 |
+
|
| 51 |
+
def add_special_char(self, dict_character):
|
| 52 |
+
return dict_character
|
| 53 |
+
|
| 54 |
+
def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
|
| 55 |
+
""" convert text-index into text-label. """
|
| 56 |
+
result_list = []
|
| 57 |
+
ignored_tokens = self.get_ignored_tokens()
|
| 58 |
+
batch_size = len(text_index)
|
| 59 |
+
for batch_idx in range(batch_size):
|
| 60 |
+
selection = np.ones(len(text_index[batch_idx]), dtype=bool)
|
| 61 |
+
if is_remove_duplicate:
|
| 62 |
+
selection[1:] = text_index[batch_idx][1:] != text_index[
|
| 63 |
+
batch_idx][:-1]
|
| 64 |
+
for ignored_token in ignored_tokens:
|
| 65 |
+
selection &= text_index[batch_idx] != ignored_token
|
| 66 |
+
|
| 67 |
+
char_list = [
|
| 68 |
+
self.character[text_id]
|
| 69 |
+
for text_id in text_index[batch_idx][selection]
|
| 70 |
+
]
|
| 71 |
+
if text_prob is not None:
|
| 72 |
+
conf_list = text_prob[batch_idx][selection]
|
| 73 |
+
else:
|
| 74 |
+
conf_list = [1] * len(selection)
|
| 75 |
+
if len(conf_list) == 0:
|
| 76 |
+
conf_list = [0]
|
| 77 |
+
# print('\n char_list:',char_list)
|
| 78 |
+
text = ''.join(char_list)
|
| 79 |
+
|
| 80 |
+
if self.reverse: # for arabic rec
|
| 81 |
+
text = self.pred_reverse(text)
|
| 82 |
+
|
| 83 |
+
result_list.append((text, np.mean(conf_list).tolist()))
|
| 84 |
+
return result_list
|
| 85 |
+
|
| 86 |
+
def get_ignored_tokens(self):
|
| 87 |
+
return [0] # for ctc blank
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
class CTCLabelDecode(BaseRecLabelDecode):
|
| 91 |
+
""" Convert between text-label and text-index """
|
| 92 |
+
|
| 93 |
+
def __init__(self, character_dict_path=None, use_space_char=False,
|
| 94 |
+
**kwargs):
|
| 95 |
+
|
| 96 |
+
super(CTCLabelDecode, self).__init__(character_dict_path,
|
| 97 |
+
use_space_char)
|
| 98 |
+
|
| 99 |
+
print('\n decoder:', character_dict_path, use_space_char)
|
| 100 |
+
|
| 101 |
+
def __call__(self, preds, label=None, *args, **kwargs):
|
| 102 |
+
if isinstance(preds, tuple) or isinstance(preds, list):
|
| 103 |
+
preds = preds[-1]
|
| 104 |
+
|
| 105 |
+
preds_idx = preds.argmax(axis=2)
|
| 106 |
+
preds_prob = preds.max(axis=2)
|
| 107 |
+
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=True)
|
| 108 |
+
if label is None:
|
| 109 |
+
return text
|
| 110 |
+
label = self.decode(label)
|
| 111 |
+
return text, label
|
| 112 |
+
|
| 113 |
+
def add_special_char(self, dict_character):
|
| 114 |
+
dict_character = ['blank'] + dict_character
|
| 115 |
+
return dict_character
|