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