-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocr.py
More file actions
289 lines (273 loc) · 11.5 KB
/
ocr.py
File metadata and controls
289 lines (273 loc) · 11.5 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# pip install opencv-contrib-python
# ======================================================================
#
# python script to run OCR functionality
# This can be used to iterate pic change when ocr doesnt output correct
# this action maybe for instance :-
# 1.Rotate 2.Invert 3.Resize (4. canny for edge detection)
#
# you can use the script as an iterative autoscaler with known example
# graphic files until the ocr output result is equal to the expectation
# then you can work out the correct ratio for that application for all
# new and unknown files.
#
# ======================================================================
#
import os
import cv2
import numpy as np
# ============== only if you want to do saliency =======================
# requires https://github.com/akisatok/pySaliencyMap
# for producing salency maps if you dont want uncomment this bit out
import pySaliencyMap
# ============== gaussian ==============================================
from scipy.ndimage import gaussian_filter
# ============== prewitt ===============================================
from scipy import ndimage
# read the designated image for use with the conversions
im = cv2.imread('/mnt/c/linuxmirror/testFile1.jpg')
# ======= instead use a webcam uncomment below =========================
# capture = cv2.VideoCapture(0)
# while(True):
# capture
# retval, im = capture.read()
#if not im:
# print('the image was not read correctly')
#else:
# ========= convert BGR to RGB =========================================
im_rgb = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
# here we write the image as a pnm file (we choose without modification)
# for testing with the ocrad ocr engine
oi = cv2.imwrite('/home/mark/pics/ocradFile.pnm', im_rgb)
# ========= crop the image if you want =================================
crop = im[ 0:200, 200:500 ]
oi = cv2.imwrite('/mnt/c/linuxmirror/crop.jpg', crop)
# ========= use this to re-size images if needed =======================
print('Original Dimensions : ',im.shape)
scale_percent = 60 # percent of original size <100 (smaller) >100 (bigger)
width = int(im.shape[1] * scale_percent / 100) # can set explicitly
height = int(im.shape[0] * scale_percent / 100) # can set explicitly
dim = (width, height)
# ======== resize image ================================================
resized = cv2.resize(im, dim, interpolation = cv2.INTER_AREA)
print('Resized Dimensions : ',resized.shape)
# ======== to show it ==================================================
# cv2.imshow("Resized image", resized)
# ======= if you want to write the re-sized image for use with ocr =====
rs = cv2.imwrite('/mnt/c/linuxmirror/resized1.jpg', resized)
# ========= invert image ===============================================
# im = (255-im)
ni = ~im
# im = cv2.bitwise_not(im)
# to write out
iv = cv2.imwrite('/mnt/c/linuxmirror/invert1.jpg', ni)
# ======== rotate image ================================================
# 5 degree rotate to left
# angleInDegrees = 5
# 5 degree rotate to right
angleInDegrees = -5
# scale if you want here we do no change at value 1.0 unity
scale = 1.0
(h, w) = im.shape[:2]
center=tuple(np.array([h,w])/2)
# Perform the rotation
M = cv2.getRotationMatrix2D(center, angleInDegrees, scale)
rotated = cv2.warpAffine(im, M, (w, h))
ro = cv2.imwrite('/mnt/c/linuxmirror/rotated.jpg', rotated)
#
# alternate rotate code
#
# rad = math.radians(angleInDegrees)
# sin = math.sin(rad)
# cos = math.cos(rad)
# b_w = int((h * abs(sin)) + (w * abs(cos)))
# b_h = int((h * abs(cos)) + (w * abs(sin)))
# rot[0, 2] += ((b_w / 2) - center[0])
# rot[1, 2] += ((b_h / 2) - center[1])
# outImg = cv2.warpAffine(im, rot, (b_w, b_h), flags=cv2.INTER_LINEAR)
# oi = cv2.imwrite('/mnt/c/linuxmirror/rotated2.jpg', outImg)
# ========= Edge detection (doesnt normally help ocr) ==================
#
# very good for looking at weeds in fields though
#
# ========= apply canny filter =========================================
edges = cv2.Canny(im,100,200)
# ======= if you want to write the canny image for use =================
ca = cv2.imwrite('/mnt/c/linuxmirror/canny1.jpg', edges)
# ======= dilate function use when edge not connected ==================
kern = np.ones((5,5),np.uint8)
imgDilation = cv2.dilate(edges, kern, iterations=1)
ci = cv2.imwrite('/mnt/c/linuxmirror/cannydilate.jpg', imgDilation)
# ======= erode when you want to thin image ============================
imgErode = cv2.erode(imgDilation, kern, iterations=1)
ce = cv2.imwrite('/mnt/c/linuxmirror/cannyerode.jpg', imgErode)
# ========= sobel edge detection =======================================
laplacian = cv2.Laplacian(im,cv2.CV_64F)
sobelx = cv2.Sobel(im,cv2.CV_64F,1,0,ksize=5)
sobely = cv2.Sobel(im,cv2.CV_64F,0,1,ksize=5)
# Grayscale processing image (used by sobel and prewitt)
grayImage = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
# Sobel operator
x = cv2.Sobel(grayImage, cv2.CV_16S, 1, 0)
y = cv2.Sobel(grayImage, cv2.CV_16S, 0, 1)
# Turn uint8, image fusion
absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
Sobel = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
# ======= if you want to write the sobel image for use =================
lp = cv2.imwrite('/mnt/c/linuxmirror/laplace1.jpg', laplacian)
sx = cv2.imwrite('/mnt/c/linuxmirror/sobelX.jpg', sobelx)
sy = cv2.imwrite('/mnt/c/linuxmirror/sobelY.jpg', sobely)
so = cv2.imwrite('/mnt/c/linuxmirror/sobel.jpg', Sobel)
# ======== prewitt edge detection ======================================
prewit = ndimage.prewitt(im)
pw = cv2.imwrite('/mnt/c/linuxmirror/prewitt.jpg', prewit)
# Prewitt operator
kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]],dtype=int)
kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]],dtype=int)
x = cv2.filter2D(grayImage, cv2.CV_16S, kernelx)
y = cv2.filter2D(grayImage, cv2.CV_16S, kernely)
# Turn uint8, image fusion
absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
Prewitt = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
pw = cv2.imwrite('/mnt/c/linuxmirror/prewitt2.jpg', Prewitt)
# ======== roberts edge detector =======================================
grayImage = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
# Roberts operator
kernelx = np.array([[-1, 0], [0, 1]], dtype=int)
kernely = np.array([[0, -1], [1, 0]], dtype=int)
x = cv2.filter2D(grayImage, cv2.CV_16S, kernelx)
y = cv2.filter2D(grayImage, cv2.CV_16S, kernely)
# Turn uint8, image fusion
absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
Roberts = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
ro = cv2.imwrite('/mnt/c/linuxmirror/roberts.jpg', Roberts)
# ========= apply guassian blur on src image ===========================
dst = cv2.GaussianBlur(im,(5,5),cv2.BORDER_DEFAULT)
# display input and output image
#cv2.imshow("Gaussian Smoothing",numpy.hstack((im, dst)))
#gblur = np.hstack((im, dst)
gb = cv2.imwrite('/mnt/c/linuxmirror/gauss2.jpg', dst)
# sharp
gbresult = gaussian_filter(im, sigma=-1)
# smooth
#gbresult = gaussian_filter(im, sigma=1)
gb = cv2.imwrite('/mnt/c/linuxmirror/gaussi.jpg', gbresult)
# ========= apply filter on src image ==================================
kernel = np.ones((5,5),np.float32)/25
filtr = cv2.filter2D(im,-1,kernel)
fi = cv2.imwrite('/mnt/c/linuxmirror/filtered.jpg', filtr)
# ========= compute gradient using scharr ==============================
gradx = cv2.Scharr(im, cv2.CV_32F, 1, 0, scale=1.0/32)
grady = cv2.Scharr(im, cv2.CV_32F, 0, 1, scale=1.0/32)
gx = cv2.imwrite('/mnt/c/linuxmirror/gradx.jpg', gradx)
gy = cv2.imwrite('/mnt/c/linuxmirror/grady.jpg', grady)
gradient = np.dstack([gradx, grady])
# ======== implementation for extracting a saliency map from image
# you need to download the following python code libraries
# ref :- https://github.com/akisatok/pySaliencyMap
# ref :- L. Itti, C. Koch, E. Niebur,
# A Model of Saliency-Based Visual Attention for Rapid Scene Analysis,
# IEEE Transactions on Pattern Analysis and Machine Intelligence,
# Vol. 20, No. 11, pp. 1254-1259, Nov 1998.
#
# some further interesting info in matlab can be found here
# http://people.vision.caltech.edu/~harel/share/gbvs.php
#
im2 = cv2.imread('/mnt/c/linuxmirror/pic1.jpg')
im = cv2.resize(im2,(300,300))
imgsize = im.shape
img_width = imgsize[1]
img_height = imgsize[0]
sm = pySaliencyMap.pySaliencyMap(img_width, img_height)
saliency_map = sm.SMGetSM(im)
binarized_map = sm.SMGetBinarizedSM(im)
salient_region = sm.SMGetSalientRegion(im)
sm = cv2.imwrite('/mnt/c/linuxmirror/saliency.jpg', saliency_map)
bm = cv2.imwrite('/mnt/c/linuxmirror/binarized.jpg', binarized_map)
sr_rgb = cv2.cvtColor(salient_region, cv2.COLOR_BGR2RGB)
sr = cv2.imwrite('/mnt/c/linuxmirror/salient_reg.jpg', sr_rgb)
#
# resize the milk bottle label to be read by tesseract
# rotate to make font more readable by the ocr
# make 2 different size pictures to read whole text and invert
#
im2 = cv2.imread('/mnt/c/linuxmirror/CW118.jpg')
im3 = cv2.resize(im2,(1800,1449))
#im3 = cv2.resize(im2,(1200,900))
angleInDegrees = 1.5
# scale if you want here we do no change at value 1.0 unity
scale = 1.0
(h, w) = im3.shape[:2]
center=tuple(np.array([h,w])/2)
# Perform the rotation
M = cv2.getRotationMatrix2D(center, angleInDegrees, scale)
rotated = cv2.warpAffine(im3, M, (w, h))
# filter
gbresult = gaussian_filter(rotated, sigma=-2)
# write it out inverted
mb = cv2.imwrite('/mnt/c/linuxmirror/CW118_r.jpg', ~gbresult)
# repeat for the image not resized (reads other text)
(h, w) = im2.shape[:2]
center=tuple(np.array([h,w])/2)
M = cv2.getRotationMatrix2D(center, angleInDegrees, scale)
rotated = cv2.warpAffine(im2, M, (w, h))
gbresult = gaussian_filter(rotated, sigma=-2)
mb = cv2.imwrite('/mnt/c/linuxmirror/CW118_r2.jpg', ~gbresult)
#
# this one shows how to read the curved label
# (need to make automated proceedure for flattening out label)
#
im2 = cv2.imread('/mnt/c/linuxmirror/CW119_t.jpg')
im3 = cv2.resize(im2,(1200,900))
mb = cv2.imwrite('/mnt/c/linuxmirror/CW119_r.jpg', ~im3)
# =========== try to automate reading the farm label ===================
im2 = cv2.imread('/mnt/c/linuxmirror/CW119.jpg')
(h, w) = im2.shape[:2]
# drag the shape out a bit
print(im2.shape)
im3 = cv2.resize(im2,(int(w*8),int(h*4)))
(h, w) = im3.shape[:2]
center=tuple(np.array([h,w])/2)
M = cv2.getRotationMatrix2D(center, angleInDegrees, scale)
rotated = cv2.warpAffine(im3, M, (w, h))
# filter
gbresult = gaussian_filter(rotated, sigma=-2)
mb = cv2.imwrite('/mnt/c/linuxmirror/CW119_r2.jpg', ~gbresult)
# ------- now stretch that more ---------
# drag the shape out a bit
(h, w) = im2.shape[:2]
#im3 = cv2.resize(im2,(int(w*10),int(h*6)))
im3 = cv2.resize(im2,(int(w*8),int(h*6)))
(h, w) = im3.shape[:2]
center=tuple(np.array([h,w])/2)
M = cv2.getRotationMatrix2D(center, angleInDegrees, scale)
rotated = cv2.warpAffine(im3, M, (w, h))
# filter
gbresult = gaussian_filter(rotated, sigma=-2)
mb = cv2.imwrite('/mnt/c/linuxmirror/CW119_r3.jpg', ~gbresult)
# if you are using a webcam then uncomment it here
# exit if the key "q" is pressed
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# if you use imshow to look at anything you need this one
# cv2.destroyAllWindows()
#if not oi:
# print('Image was not converted')
#else:
#
# here we are shelling to the operating system to run the ocr on the pnm
# it also runs the ocr tests on the labels we are testing
#
f = os.popen('/home/mark/pics/run_ocrad.sh /home/mark/pics/ocradFile.pnm')
strFound = f.read()
print ("The following text was read using ocrad : ", strFound)
#
# alternative OCR with GUI is lios installed using python as follows
# git clone https://gitlab.com/Nalin-x-Linux/lios-3.git
# cd lios-3
# sudo python3 setup.py install --install-data=/usr or in my case
# sudo python3 setup.py install --install-data=/home
#