-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
100 lines (77 loc) · 2.61 KB
/
util.py
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
from PIL import Image
import pytesseract
import cv2.cv2 as cv2
import os
import imutils
import numpy as np
import pillowfight
import io
import base64
def get_image(path):
# Reading image
image = cv2.imread(path)
image, gray = format_image(image)
return image, gray
def format_image(image):
# Resizing image
image = imutils.resize(image, height=800, inter=cv2.INTER_CUBIC)
# Converting image to gray scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return image, gray
def swt(img):
img = Image.fromarray(img)
img = pillowfight.swt(img, output_type=pillowfight.SWT_OUTPUT_ORIGINAL_BOXES)
img = np.array(img)
return img
def blur(img, type='g'):
if type == 'g':
img = cv2.GaussianBlur(img, (3, 3), 0)
else:
img = cv2.medianBlur(img, 3)
return img
def adaptive_histogram(img, adaptive=True):
if adaptive is True:
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
img = clahe.apply(img)
else:
img = cv2.equalizeHist(img) # regular hist
return img
def sharpen1(gray): # best for now
kernel_sharpen = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
gray = cv2.filter2D(gray, -1, kernel_sharpen)
return gray
def sharpen2(gray):
kernel_sharpen = np.array([[-1, -1, -1, -1, -1],
[-1, 2, 2, 2, -1],
[-1, 2, 8, 2, -1],
[-1, 2, 2, 2, -1],
[-1, -1, -1, -1, -1]]) / 8.0
gray = cv2.filter2D(gray, -1, kernel_sharpen)
return gray
def image_preprocessor(gray):
gray = sharpen1(gray)
gray = adaptive_histogram(gray)
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 1))
gray = cv2.dilate(gray, kernel, iterations=2)
return gray
# -oem 0 textord_heavy_nr 1
# enable_new_segsearch 1
# load_system_dawg F
# load_freq_dawg F
# 'textord_min_linesize 3.25' \
# 'textord_heavy_nr 1' \
def my_ocr(gray):
CONFIG = '--psm 1 ' \
'--oem 2 ' \
'tessedit_char_whitelist 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz:' \
'textord_space_size_is_variable 1'
# create image for tesseract lib
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename), lang='eng',
config='--psm 1',
)
os.remove(filename) # remove created image
# print(repr(text))
return text