Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-220
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2022-220
2022-220
Commits
cdfab0ca
Commit
cdfab0ca
authored
Nov 13, 2022
by
Pathirana K.P.G.I
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
132bf0f5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
0 deletions
+73
-0
ocr.py
ocr.py
+73
-0
No files found.
ocr.py
0 → 100644
View file @
cdfab0ca
import
cv2
import
numpy
as
np
import
pytesseract
# get grayscale image
def
get_grayscale
(
image
):
return
cv2
.
cvtColor
(
image
,
cv2
.
COLOR_BGR2GRAY
)
# noise removal
def
remove_noise
(
image
):
return
cv2
.
medianBlur
(
image
,
5
)
# thresholding
def
thresholding
(
image
):
return
cv2
.
threshold
(
image
,
0
,
255
,
cv2
.
THRESH_BINARY
+
cv2
.
THRESH_OTSU
)[
1
]
# dilation
def
dilate
(
image
):
kernel
=
np
.
ones
((
5
,
5
),
np
.
uint8
)
return
cv2
.
dilate
(
image
,
kernel
,
iterations
=
1
)
# erosion
def
erode
(
image
):
kernel
=
np
.
ones
((
5
,
5
),
np
.
uint8
)
return
cv2
.
erode
(
image
,
kernel
,
iterations
=
1
)
# opening - erosion followed by dilation
def
opening
(
image
):
kernel
=
np
.
ones
((
5
,
5
),
np
.
uint8
)
return
cv2
.
morphologyEx
(
image
,
cv2
.
MORPH_OPEN
,
kernel
)
# canny edge detection
def
canny
(
image
):
return
cv2
.
Canny
(
image
,
100
,
200
)
# skew correction
def
deskew
(
image
):
coords
=
np
.
column_stack
(
np
.
where
(
image
>
0
))
angle
=
cv2
.
minAreaRect
(
coords
)[
-
1
]
if
angle
<
-
45
:
angle
=
-
(
90
+
angle
)
else
:
angle
=
-
angle
(
h
,
w
)
=
image
.
shape
[:
2
]
center
=
(
w
//
2
,
h
//
2
)
M
=
cv2
.
getRotationMatrix2D
(
center
,
angle
,
1.0
)
rotated
=
cv2
.
warpAffine
(
image
,
M
,
(
w
,
h
),
flags
=
cv2
.
INTER_CUBIC
,
borderMode
=
cv2
.
BORDER_REPLICATE
)
return
rotated
# template matching
def
match_template
(
image
,
template
):
return
cv2
.
matchTemplate
(
image
,
template
,
cv2
.
TM_CCOEFF_NORMED
)
def
get_rome_number
(
img
):
custom_config
=
r'-c tessedit_char_whitelist=ivxlcdm --psm 6'
text
=
pytesseract
.
image_to_string
(
img
,
config
=
custom_config
)
return
text
# text = get_rome_number('test1.png')
# print(text)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment