Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Aniket025
GitHub Repository: Aniket025/Medical-Prescription-OCR
Path: blob/master/Model-4/word_classification.py
427 views
1
2
# coding: utf-8
3
4
# In[1]:
5
6
7
import numpy as np
8
import tensorflow as tf
9
from ocr.helpers import *
10
from ocr.tfhelpers import Graph
11
from ocr.normalization import imageNorm, letterNorm
12
from ocr.datahelpers import idx2char
13
import cv2
14
import math
15
16
17
18
# In[2]:
19
20
21
print("Loading Segmentation model:")
22
segCNNGraph = Graph('models/gap-clas/CNN-CG')
23
segRNNGraph = Graph('models/gap-clas/RNN/Bi-RNN-new', 'prediction')
24
MODEL_LOC = 'models/char-clas/en/CharClassifier'
25
charClass = Graph(MODEL_LOC)
26
27
28
# In[3]:
29
30
31
def classify(img, step=2, RNN=False, slider=(60, 60)):
32
""" Slice the image and return raw output of classifier """
33
length = (img.shape[1] - slider[1]) // 2 + 1
34
if RNN:
35
input_seq = np.zeros((1, length, slider[0]*slider[1]), dtype=np.float32)
36
input_seq[0][:] = [img[:, loc * step: loc * step + slider[1]].flatten()
37
for loc in range(length)]
38
pred = segRNNGraph.eval_feed({'inputs:0': input_seq,
39
'length:0': [length],
40
'keep_prob:0': 1})[0]
41
else:
42
input_seq = np.zeros((length, slider[0]*slider[1]), dtype=np.float32)
43
input_seq[:] = [img[:, loc * step: loc * step + slider[1]].flatten()
44
for loc in range(length)]
45
pred = segCNNGraph.run(input_seq)
46
47
return pred
48
49
50
# In[4]:
51
52
53
def segmentation(img, step=2, RNN=False, debug=False):
54
"""
55
Take preprocessed image of word
56
and return array of positions separating chars - gaps
57
"""
58
slider = (60, 30)
59
if RNN:
60
slider = (60, 60)
61
62
# Run the classifier
63
pred = classify(img, step=step, RNN=RNN, slider=slider)
64
65
# Finalize the gap positions from raw prediction
66
gaps = []
67
lastGap = 0
68
gapCount = 1
69
gapPositionSum = slider[1] / 2
70
firstGap = True
71
gapBlockFirst = 0
72
gapBlockLast = slider[1] / 2
73
74
for i, p in enumerate(pred):
75
if p == 1:
76
gapPositionSum += i * step + slider[1] / 2
77
gapBlockLast = i * step + slider[1] / 2
78
gapCount += 1
79
lastGap = 0
80
if gapBlockFirst == 0:
81
gapBlockFirst = i * step + slider[1] / 2
82
else:
83
if gapCount != 0 and lastGap >= 1:
84
if firstGap:
85
gaps.append(int(gapBlockLast))
86
firstGap = False
87
else:
88
gaps.append(int(gapPositionSum // gapCount))
89
gapPositionSum = 0
90
gapCount = 0
91
gapBlockFirst = 0
92
lastGap += 1
93
94
# Adding final gap position
95
if gapBlockFirst != 0:
96
gaps.append(int(gapBlockFirst))
97
else:
98
gapPositionSum += (len(pred) - 1) * 2 + slider[1]/2
99
gaps.append(int(gapPositionSum / (gapCount + 1)))
100
101
if debug:
102
# Drawing lines
103
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
104
for gap in gaps:
105
cv2.line(img,
106
((int)(gap), 0),
107
((int)(gap), slider[0]),
108
(0, 255, 0), 1)
109
#implt(img, t="Separated characters")
110
111
return gaps
112
113
114
# In[5]:
115
116
117
# img = cv2.imread("./output/words/normal/0.jpg")
118
# implt(img)
119
120
121
# # In[6]:
122
123
124
# img = cv2.imread("./output/words/normal/85.jpg")
125
# implt(img)
126
# img = imageNorm(img,60,border=False,tilt=True,hystNorm=True)
127
# implt(img)
128
# img = cv2.copyMakeBorder(img,0,0,30,30,cv2.BORDER_CONSTANT, value=[0,0,0])
129
# implt(img)
130
131
132
# # In[12]:
133
134
135
# gaps = segmentation(img, step=2, RNN=True, debug=False)
136
137
138
# # In[13]:
139
140
141
# print gaps
142
143
144
# # In[14]:
145
146
147
# for i in range(0,(len(gaps)-1)):
148
# implt(img[0:60,gaps[i]:gaps[i+1]])
149
150
151
# # In[15]:
152
153
154
# chars = []
155
# for i in range(0,(len(gaps)-1)):
156
# char = img[0:60,gaps[i]:gaps[i+1]]
157
# char, dim = letterNorm(char, is_thresh=True, dim=True)
158
# if dim[0]>4 and dim[1]>4:
159
# chars.append(char.flatten())
160
161
162
# # In[16]:
163
164
165
# chars = np.array(chars)
166
# word = ''
167
168
# if len(chars) != 0:
169
# pred = charClass.run(chars)
170
# for c in pred:
171
# word += idx2char(c)
172
173
# print word
174
175
176