Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Aniket025
GitHub Repository: Aniket025/Medical-Prescription-OCR
Path: blob/master/Model-1/WordClassDM.py
427 views
1
"""
2
Last: 5069
3
4
Script with simple UI for creating gaplines data
5
Run: python WordClassDM.py --index 0
6
Controls:
7
setting gaplines - click and drag
8
saving gaplines - 's' key
9
reseting gaplines - 'r' key
10
skip to next img - 'n' key
11
delete last line - 'd' key
12
"""
13
14
import cv2
15
import os
16
import numpy as np
17
import glob
18
import argparse
19
import simplejson
20
from ocr.normalization import imageNorm
21
from ocr.viz import printProgressBar
22
23
24
def loadImages(dataloc, idx=0, num=None):
25
""" Load images and labels """
26
print("Loading words...")
27
28
dataloc += '/' if dataloc[-1] != '/' else ''
29
# Load images and short them from the oldest to the newest
30
imglist = glob.glob(os.path.join(dataloc, u'*.jpg'))
31
imglist.sort(key=lambda x: float(x.split("_")[-1][:-4]))
32
tmpLabels = [name[len(dataloc):] for name in imglist]
33
34
labels = np.array(tmpLabels)
35
images = np.empty(len(imglist), dtype=object)
36
37
if num is None:
38
upper = len(imglist)
39
else:
40
upper = min(idx + num, len(imglist))
41
num += idx
42
43
for i, img in enumerate(imglist):
44
# TODO Speed up loading - Normalization
45
if i >= idx and i < upper:
46
images[i] = imageNorm(
47
cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2RGB),
48
height=60,
49
border=False,
50
tilt=True,
51
hystNorm=True)
52
printProgressBar(i-idx, upper-idx-1)
53
print()
54
return (images[idx:num], labels[idx:num])
55
56
57
class Cycler:
58
drawing = False
59
scaleF = 4
60
61
def __init__(self, idx, loc='data/words_raw'):
62
""" Load images and starts from given index """
63
# self.images, self.labels = loadImages(loc, idx)
64
self.loc = loc
65
self.idx = 0
66
self.org_idx = idx
67
68
self.blockLoad()
69
self.image_act = self.images[self.idx]
70
71
cv2.namedWindow('image')
72
cv2.setMouseCallback('image', self.mouseHandler)
73
self.nextImage()
74
75
self.run()
76
77
def run(self):
78
while(1):
79
self.imageShow()
80
k = cv2.waitKey(1) & 0xFF
81
if k == ord('d'):
82
# Delete last line
83
self.deleteLastLine()
84
elif k == ord('r'):
85
# Clear current gaplines
86
self.nextImage()
87
elif k == ord('s'):
88
# Save gaplines with image
89
if self.saveData():
90
self.idx += 1
91
if self.idx >= len(self.images):
92
if not self.blockLoad():
93
break
94
self.nextImage()
95
elif k == ord('n'):
96
# Skip to next image
97
self.idx += 1
98
if self.idx >= len(self.images):
99
if not self.blockLoad():
100
break
101
self.nextImage()
102
elif k == 27:
103
cv2.destroyAllWindows()
104
break
105
106
print("End of labeling at INDEX: " + str(self.org_idx + self.idx))
107
108
def blockLoad(self):
109
self.images, self.labels = loadImages(
110
self.loc, self.org_idx + self.idx, 100)
111
self.org_idx += self.idx
112
self.idx = 0
113
return len(self.images) is not 0
114
115
def imageShow(self):
116
cv2.imshow(
117
'image',
118
cv2.resize(
119
self.image_act,
120
(0,0),
121
fx=self.scaleF,
122
fy=self.scaleF,
123
interpolation=cv2.INTERSECT_NONE))
124
125
def nextImage(self):
126
self.image_act = cv2.cvtColor(self.images[self.idx], cv2.COLOR_GRAY2RGB)
127
self.label_act = self.labels[self.idx][:-4]
128
self.gaplines = [0, self.image_act.shape[1]]
129
self.redrawLines()
130
131
print(self.org_idx + self.idx, ":", self.label_act.split("_")[0])
132
self.imageShow();
133
134
def saveData(self):
135
self.gaplines.sort()
136
print("Saving image with gaplines: ", self.gaplines)
137
138
try:
139
assert len(self.gaplines) - 1 == len(self.label_act.split("_")[0])
140
141
cv2.imwrite(
142
"data/words2/%s.jpg" % (self.label_act),
143
self.images[self.idx])
144
with open('data/words2/%s.txt' % (self.label_act), 'w') as fp:
145
simplejson.dump(self.gaplines, fp)
146
return True
147
except:
148
print("Wront number of gaplines")
149
return False
150
151
print()
152
self.nextImage()
153
154
def deleteLastLine(self):
155
if len(self.gaplines) > 0:
156
del self.gaplines[-1]
157
self.redrawLines()
158
159
def redrawLines(self):
160
self.image_act = cv2.cvtColor(self.images[self.idx], cv2.COLOR_GRAY2RGB)
161
for x in self.gaplines:
162
self.drawLine(x)
163
164
def drawLine(self, x):
165
cv2.line(
166
self.image_act, (x, 0), (x, self.image_act.shape[0]), (0,255,0), 1)
167
168
def mouseHandler(self, event, x, y, flags, param):
169
# Clip x into image width range
170
x = max(min(self.image_act.shape[1], x // self.scaleF), 0)
171
172
if event == cv2.EVENT_LBUTTONDOWN:
173
self.drawing = True
174
self.tmp = self.image_act.copy()
175
self.drawLine(x)
176
elif event == cv2.EVENT_MOUSEMOVE:
177
if self.drawing == True:
178
self.image_act = self.tmp.copy()
179
self.drawLine(x)
180
elif event == cv2.EVENT_LBUTTONUP:
181
self.drawing = False
182
if x not in self.gaplines:
183
self.gaplines.append(x)
184
self.image_act = self.tmp.copy()
185
self.drawLine(x)
186
187
188
if __name__ == '__main__':
189
parser = argparse.ArgumentParser(
190
"Script creating UI for gaplines classification")
191
parser.add_argument(
192
"--index",
193
type=int,
194
default=0,
195
help="Index of starting image")
196
197
args = parser.parse_args()
198
Cycler(args.index)
199
200