-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOCRTest.py
92 lines (74 loc) · 3 KB
/
OCRTest.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
from matplotlib import pyplot as plt
import abc
import cv2
class OCRTest(metaclass=abc.ABCMeta):
def __init__(self):
pass
@abc.abstractmethod
def detect_text_ocr(self, prepro_img, original_img):
pass
def pre_process(self, img):
temp_img = img.copy()
# 黒っぽい色以外は白とする
# threshold = 200
# mask = (temp_img[:, :, 0] >= threshold) | (temp_img[:, :, 1] >= threshold) | (temp_img[:, :, 2] >= threshold)
# temp_img[mask] = [255, 255, 255]
# グレースケール化
if False:
temp_img = cv2.cvtColor(temp_img, cv2.COLOR_BGR2GRAY)
else:
# HSV色空間に変換し、Vチャネルでグレースケール化
temp_img = cv2.cvtColor(temp_img, cv2.COLOR_BGR2HSV)
temp_img = temp_img[:, :, 2]
# ノイズ除去
if True:
# ガウシアンフィルタでノイズ除去
temp_img = cv2.GaussianBlur(temp_img, (5, 5), 0)
else:
# バイラテラルフィルタでノイズ除去
temp_img = cv2.bilateralFilter(temp_img, 9, 75, 75)
# 大津法で二値化
# 本の表紙の場合、背景にも絵があるので二値化すると判読不可となる。
# 白地に文字を書いたようなドキュメントのOCRなら有効。
# _, temp_img = cv2.threshold(temp_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# 傾き補正
# coords = np.column_stack(np.where(temp_img > 0))
# angle = cv2.minAreaRect(coords)[-1]
# if angle < -45:
# angle = -(90 + angle)
# else:
# angle = -angle
# (h, w) = temp_img.shape[:2]
# center = (w // 2, h // 2)
# M = cv2.getRotationMatrix2D(center, angle, 1.0)
# temp_img = cv2.warpAffine(temp_img, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
# コントラスト調整
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
temp_img = clahe.apply(temp_img)
if False:
# for Debug
# 前処理後の画像確認
plt.imshow(temp_img, cmap='gray')
plt.axis('off')
plt.show()
return temp_img
def resize_image(self, image, target_size=1000):
# 短辺をtarget_sizeとする倍率を計算
height, width = image.shape[:2]
if height < width:
scale = target_size / height
else:
scale = target_size / width
# 画像のリサイズ
new_width = int(width * scale)
new_height = int(height * scale)
resized_image = cv2.resize(image, (new_width, new_height))
return resized_image
def run(self, image_path: str):
img = cv2.imread(image_path)
img = self.resize_image(img)
prepro_img = self.pre_process(img)
img = self.detect_text_ocr(prepro_img, img)
plt.imshow(img)
plt.axis('off')
plt.show()