yomitoku 0.4.0.post1.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- yomitoku/__init__.py +20 -0
- yomitoku/base.py +136 -0
- yomitoku/cli/__init__.py +0 -0
- yomitoku/cli/main.py +230 -0
- yomitoku/configs/__init__.py +13 -0
- yomitoku/configs/cfg_layout_parser_rtdtrv2.py +89 -0
- yomitoku/configs/cfg_table_structure_recognizer_rtdtrv2.py +80 -0
- yomitoku/configs/cfg_text_detector_dbnet.py +49 -0
- yomitoku/configs/cfg_text_recognizer_parseq.py +51 -0
- yomitoku/constants.py +32 -0
- yomitoku/data/__init__.py +3 -0
- yomitoku/data/dataset.py +40 -0
- yomitoku/data/functions.py +279 -0
- yomitoku/document_analyzer.py +315 -0
- yomitoku/export/__init__.py +6 -0
- yomitoku/export/export_csv.py +71 -0
- yomitoku/export/export_html.py +188 -0
- yomitoku/export/export_json.py +34 -0
- yomitoku/export/export_markdown.py +145 -0
- yomitoku/layout_analyzer.py +66 -0
- yomitoku/layout_parser.py +189 -0
- yomitoku/models/__init__.py +9 -0
- yomitoku/models/dbnet_plus.py +272 -0
- yomitoku/models/layers/__init__.py +0 -0
- yomitoku/models/layers/activate.py +38 -0
- yomitoku/models/layers/dbnet_feature_attention.py +160 -0
- yomitoku/models/layers/parseq_transformer.py +218 -0
- yomitoku/models/layers/rtdetr_backbone.py +333 -0
- yomitoku/models/layers/rtdetr_hybrid_encoder.py +433 -0
- yomitoku/models/layers/rtdetrv2_decoder.py +811 -0
- yomitoku/models/parseq.py +243 -0
- yomitoku/models/rtdetr.py +22 -0
- yomitoku/ocr.py +87 -0
- yomitoku/postprocessor/__init__.py +9 -0
- yomitoku/postprocessor/dbnet_postporcessor.py +137 -0
- yomitoku/postprocessor/parseq_tokenizer.py +128 -0
- yomitoku/postprocessor/rtdetr_postprocessor.py +107 -0
- yomitoku/reading_order.py +214 -0
- yomitoku/resource/MPLUS1p-Medium.ttf +0 -0
- yomitoku/resource/charset.txt +1 -0
- yomitoku/table_structure_recognizer.py +244 -0
- yomitoku/text_detector.py +103 -0
- yomitoku/text_recognizer.py +128 -0
- yomitoku/utils/__init__.py +0 -0
- yomitoku/utils/graph.py +20 -0
- yomitoku/utils/logger.py +15 -0
- yomitoku/utils/misc.py +102 -0
- yomitoku/utils/visualizer.py +179 -0
- yomitoku-0.4.0.post1.dev0.dist-info/METADATA +127 -0
- yomitoku-0.4.0.post1.dev0.dist-info/RECORD +52 -0
- yomitoku-0.4.0.post1.dev0.dist-info/WHEEL +4 -0
- yomitoku-0.4.0.post1.dev0.dist-info/entry_points.txt +2 -0
@@ -0,0 +1,103 @@
|
|
1
|
+
from typing import List
|
2
|
+
|
3
|
+
import numpy as np
|
4
|
+
import torch
|
5
|
+
from pydantic import conlist
|
6
|
+
|
7
|
+
from .base import BaseModelCatalog, BaseModule, BaseSchema
|
8
|
+
from .configs import TextDetectorDBNetConfig
|
9
|
+
from .data.functions import (
|
10
|
+
array_to_tensor,
|
11
|
+
resize_shortest_edge,
|
12
|
+
standardization_image,
|
13
|
+
)
|
14
|
+
from .models import DBNet
|
15
|
+
from .postprocessor import DBnetPostProcessor
|
16
|
+
from .utils.visualizer import det_visualizer
|
17
|
+
|
18
|
+
|
19
|
+
class TextDetectorModelCatalog(BaseModelCatalog):
|
20
|
+
def __init__(self):
|
21
|
+
super().__init__()
|
22
|
+
self.register("dbnet", TextDetectorDBNetConfig, DBNet)
|
23
|
+
|
24
|
+
|
25
|
+
class TextDetectorSchema(BaseSchema):
|
26
|
+
points: List[
|
27
|
+
conlist(
|
28
|
+
conlist(int, min_length=2, max_length=2),
|
29
|
+
min_length=4,
|
30
|
+
max_length=4,
|
31
|
+
)
|
32
|
+
]
|
33
|
+
scores: List[float]
|
34
|
+
|
35
|
+
|
36
|
+
class TextDetector(BaseModule):
|
37
|
+
model_catalog = TextDetectorModelCatalog()
|
38
|
+
|
39
|
+
def __init__(
|
40
|
+
self,
|
41
|
+
model_name="dbnet",
|
42
|
+
path_cfg=None,
|
43
|
+
device="cuda",
|
44
|
+
visualize=False,
|
45
|
+
from_pretrained=True,
|
46
|
+
):
|
47
|
+
super().__init__()
|
48
|
+
self.load_model(
|
49
|
+
model_name,
|
50
|
+
path_cfg,
|
51
|
+
from_pretrained=True,
|
52
|
+
)
|
53
|
+
|
54
|
+
self.device = device
|
55
|
+
self.visualize = visualize
|
56
|
+
|
57
|
+
self.model.eval()
|
58
|
+
self.model.to(self.device)
|
59
|
+
|
60
|
+
self.post_processor = DBnetPostProcessor(**self._cfg.post_process)
|
61
|
+
|
62
|
+
def preprocess(self, img):
|
63
|
+
img = img.copy()
|
64
|
+
img = img[:, :, ::-1].astype(np.float32)
|
65
|
+
resized = resize_shortest_edge(
|
66
|
+
img, self._cfg.data.shortest_size, self._cfg.data.limit_size
|
67
|
+
)
|
68
|
+
normalized = standardization_image(resized)
|
69
|
+
tensor = array_to_tensor(normalized)
|
70
|
+
return tensor
|
71
|
+
|
72
|
+
def postprocess(self, preds, image_size):
|
73
|
+
return self.post_processor(preds, image_size)
|
74
|
+
|
75
|
+
def __call__(self, img):
|
76
|
+
"""apply the detection model to the input image.
|
77
|
+
|
78
|
+
Args:
|
79
|
+
img (np.ndarray): target image(BGR)
|
80
|
+
"""
|
81
|
+
|
82
|
+
ori_h, ori_w = img.shape[:2]
|
83
|
+
tensor = self.preprocess(img)
|
84
|
+
tensor = tensor.to(self.device)
|
85
|
+
with torch.inference_mode():
|
86
|
+
preds = self.model(tensor)
|
87
|
+
|
88
|
+
quads, scores = self.postprocess(preds, (ori_h, ori_w))
|
89
|
+
outputs = {"points": quads, "scores": scores}
|
90
|
+
|
91
|
+
results = TextDetectorSchema(**outputs)
|
92
|
+
|
93
|
+
vis = None
|
94
|
+
if self.visualize:
|
95
|
+
vis = det_visualizer(
|
96
|
+
preds,
|
97
|
+
img,
|
98
|
+
quads,
|
99
|
+
vis_heatmap=self._cfg.visualize.heatmap,
|
100
|
+
line_color=tuple(self._cfg.visualize.color[::-1]),
|
101
|
+
)
|
102
|
+
|
103
|
+
return results, vis
|
@@ -0,0 +1,128 @@
|
|
1
|
+
from typing import List
|
2
|
+
|
3
|
+
import numpy as np
|
4
|
+
import torch
|
5
|
+
from pydantic import conlist
|
6
|
+
|
7
|
+
from .base import BaseModelCatalog, BaseModule, BaseSchema
|
8
|
+
from .configs import TextRecognizerPARSeqConfig
|
9
|
+
from .data.dataset import ParseqDataset
|
10
|
+
from .models import PARSeq
|
11
|
+
from .postprocessor import ParseqTokenizer as Tokenizer
|
12
|
+
from .utils.misc import load_charset
|
13
|
+
from .utils.visualizer import rec_visualizer
|
14
|
+
|
15
|
+
|
16
|
+
class TextRecognizerModelCatalog(BaseModelCatalog):
|
17
|
+
def __init__(self):
|
18
|
+
super().__init__()
|
19
|
+
self.register("parseq", TextRecognizerPARSeqConfig, PARSeq)
|
20
|
+
|
21
|
+
|
22
|
+
class TextRecognizerSchema(BaseSchema):
|
23
|
+
contents: List[str]
|
24
|
+
directions: List[str]
|
25
|
+
scores: List[float]
|
26
|
+
points: List[
|
27
|
+
conlist(
|
28
|
+
conlist(int, min_length=2, max_length=2),
|
29
|
+
min_length=4,
|
30
|
+
max_length=4,
|
31
|
+
)
|
32
|
+
]
|
33
|
+
|
34
|
+
|
35
|
+
class TextRecognizer(BaseModule):
|
36
|
+
model_catalog = TextRecognizerModelCatalog()
|
37
|
+
|
38
|
+
def __init__(
|
39
|
+
self,
|
40
|
+
model_name="parseq",
|
41
|
+
path_cfg=None,
|
42
|
+
device="cuda",
|
43
|
+
visualize=False,
|
44
|
+
from_pretrained=True,
|
45
|
+
):
|
46
|
+
super().__init__()
|
47
|
+
self.load_model(
|
48
|
+
model_name,
|
49
|
+
path_cfg,
|
50
|
+
from_pretrained=True,
|
51
|
+
)
|
52
|
+
self.charset = load_charset(self._cfg.charset)
|
53
|
+
self.tokenizer = Tokenizer(self.charset)
|
54
|
+
|
55
|
+
self.device = device
|
56
|
+
|
57
|
+
self.model.eval()
|
58
|
+
self.model.to(self.device)
|
59
|
+
|
60
|
+
self.visualize = visualize
|
61
|
+
|
62
|
+
def preprocess(self, img, polygons):
|
63
|
+
dataset = ParseqDataset(self._cfg, img, polygons)
|
64
|
+
dataloader = torch.utils.data.DataLoader(
|
65
|
+
dataset,
|
66
|
+
batch_size=self._cfg.data.batch_size,
|
67
|
+
shuffle=False,
|
68
|
+
num_workers=self._cfg.data.num_workers,
|
69
|
+
)
|
70
|
+
|
71
|
+
return dataloader
|
72
|
+
|
73
|
+
def postprocess(self, p, points):
|
74
|
+
pred, score = self.tokenizer.decode(p)
|
75
|
+
directions = []
|
76
|
+
for point in points:
|
77
|
+
point = np.array(point)
|
78
|
+
w = np.linalg.norm(point[0] - point[1])
|
79
|
+
h = np.linalg.norm(point[1] - point[2])
|
80
|
+
|
81
|
+
direction = "vertical" if h > w * 2 else "horizontal"
|
82
|
+
directions.append(direction)
|
83
|
+
|
84
|
+
return pred, score, directions
|
85
|
+
|
86
|
+
def __call__(self, img, points, vis=None):
|
87
|
+
"""
|
88
|
+
Apply the recognition model to the input image.
|
89
|
+
|
90
|
+
Args:
|
91
|
+
img (np.ndarray): target image(BGR)
|
92
|
+
points (list): list of quadrilaterals. Each quadrilateral is represented as a list of 4 points sorted clockwise.
|
93
|
+
vis (np.ndarray, optional): rendering image. Defaults to None.
|
94
|
+
"""
|
95
|
+
|
96
|
+
dataloader = self.preprocess(img, points)
|
97
|
+
preds = []
|
98
|
+
scores = []
|
99
|
+
directions = []
|
100
|
+
for data in dataloader:
|
101
|
+
data = data.to(self.device)
|
102
|
+
with torch.inference_mode():
|
103
|
+
p = self.model(self.tokenizer, data).softmax(-1)
|
104
|
+
pred, score, direction = self.postprocess(p, points)
|
105
|
+
preds.extend(pred)
|
106
|
+
scores.extend(score)
|
107
|
+
directions.extend(direction)
|
108
|
+
|
109
|
+
outputs = {
|
110
|
+
"contents": preds,
|
111
|
+
"scores": scores,
|
112
|
+
"points": points,
|
113
|
+
"directions": directions,
|
114
|
+
}
|
115
|
+
results = TextRecognizerSchema(**outputs)
|
116
|
+
|
117
|
+
if self.visualize:
|
118
|
+
if vis is None:
|
119
|
+
vis = img.copy()
|
120
|
+
vis = rec_visualizer(
|
121
|
+
vis,
|
122
|
+
results,
|
123
|
+
font_size=self._cfg.visualize.font_size,
|
124
|
+
font_color=tuple(self._cfg.visualize.color[::-1]),
|
125
|
+
font_path=self._cfg.visualize.font,
|
126
|
+
)
|
127
|
+
|
128
|
+
return results, vis
|
File without changes
|
yomitoku/utils/graph.py
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class Node:
|
2
|
+
def __init__(self, id, prop):
|
3
|
+
self.id = id
|
4
|
+
self.prop = prop
|
5
|
+
self.parents = []
|
6
|
+
self.children = []
|
7
|
+
|
8
|
+
self.is_locked = False
|
9
|
+
|
10
|
+
def add_link(self, node):
|
11
|
+
if node in self.children:
|
12
|
+
return
|
13
|
+
|
14
|
+
self.children.append(node)
|
15
|
+
node.parents.append(self)
|
16
|
+
|
17
|
+
def __repr__(self):
|
18
|
+
if "contents" in self.prop:
|
19
|
+
return self.prop["contents"]
|
20
|
+
return "table"
|
yomitoku/utils/logger.py
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
import warnings
|
2
|
+
from logging import Formatter, StreamHandler, getLogger
|
3
|
+
|
4
|
+
|
5
|
+
def set_logger(name, level="INFO"):
|
6
|
+
logger = getLogger(name)
|
7
|
+
logger.setLevel(level)
|
8
|
+
handler = StreamHandler()
|
9
|
+
handler.setLevel(level)
|
10
|
+
format = Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
11
|
+
handler.setFormatter(format)
|
12
|
+
logger.addHandler(handler)
|
13
|
+
|
14
|
+
warnings.filterwarnings("ignore")
|
15
|
+
return logger
|
yomitoku/utils/misc.py
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
def load_charset(charset_path):
|
2
|
+
with open(charset_path, "r") as f:
|
3
|
+
charset = f.read()
|
4
|
+
return charset
|
5
|
+
|
6
|
+
|
7
|
+
def filter_by_flag(elements, flags):
|
8
|
+
assert len(elements) == len(flags)
|
9
|
+
return [element for element, flag in zip(elements, flags) if flag]
|
10
|
+
|
11
|
+
|
12
|
+
def is_contained(rect_a, rect_b, threshold=0.8):
|
13
|
+
"""二つの矩形A, Bが与えられたとき、矩形Bが矩形Aに含まれるかどうかを判定する。
|
14
|
+
ずれを許容するため、重複率求め、thresholdを超える場合にTrueを返す。
|
15
|
+
|
16
|
+
|
17
|
+
Args:
|
18
|
+
rect_a (np.array): x1, y1, x2, y2
|
19
|
+
rect_b (np.array): x1, y1, x2, y2
|
20
|
+
threshold (float, optional): 判定の閾値. Defaults to 0.9.
|
21
|
+
|
22
|
+
Returns:
|
23
|
+
bool: 矩形Bが矩形Aに含まれる場合True
|
24
|
+
"""
|
25
|
+
|
26
|
+
intersection = calc_intersection(rect_a, rect_b)
|
27
|
+
if intersection is None:
|
28
|
+
return False
|
29
|
+
|
30
|
+
ix1, iy1, ix2, iy2 = intersection
|
31
|
+
|
32
|
+
overlap_width = ix2 - ix1
|
33
|
+
overlap_height = iy2 - iy1
|
34
|
+
bx1, by1, bx2, by2 = rect_b
|
35
|
+
|
36
|
+
b_area = (bx2 - bx1) * (by2 - by1)
|
37
|
+
overlap_area = overlap_width * overlap_height
|
38
|
+
|
39
|
+
if overlap_area / b_area > threshold:
|
40
|
+
return True
|
41
|
+
|
42
|
+
return False
|
43
|
+
|
44
|
+
|
45
|
+
def calc_intersection(rect_a, rect_b):
|
46
|
+
ax1, ay1, ax2, ay2 = map(int, rect_a)
|
47
|
+
bx1, by1, bx2, by2 = map(int, rect_b)
|
48
|
+
|
49
|
+
# 交差領域の左上と右下の座標
|
50
|
+
ix1 = max(ax1, bx1)
|
51
|
+
iy1 = max(ay1, by1)
|
52
|
+
ix2 = min(ax2, bx2)
|
53
|
+
iy2 = min(ay2, by2)
|
54
|
+
|
55
|
+
overlap_width = max(0, ix2 - ix1)
|
56
|
+
overlap_height = max(0, iy2 - iy1)
|
57
|
+
|
58
|
+
if overlap_width == 0 or overlap_height == 0:
|
59
|
+
return None
|
60
|
+
|
61
|
+
return [ix1, iy1, ix2, iy2]
|
62
|
+
|
63
|
+
|
64
|
+
def is_intersected_horizontal(rect_a, rect_b):
|
65
|
+
_, ay1, _, ay2 = map(int, rect_a)
|
66
|
+
_, by1, _, by2 = map(int, rect_b)
|
67
|
+
|
68
|
+
# 交差領域の左上と右下の座標
|
69
|
+
iy1 = max(ay1, by1)
|
70
|
+
iy2 = min(ay2, by2)
|
71
|
+
|
72
|
+
overlap_height = max(0, iy2 - iy1)
|
73
|
+
|
74
|
+
if overlap_height == 0:
|
75
|
+
return False
|
76
|
+
|
77
|
+
return True
|
78
|
+
|
79
|
+
|
80
|
+
def is_intersected_vertical(rect_a, rect_b):
|
81
|
+
ax1, _, ax2, _ = map(int, rect_a)
|
82
|
+
bx1, _, bx2, _ = map(int, rect_b)
|
83
|
+
|
84
|
+
# 交差領域の左上と右下の座標
|
85
|
+
ix1 = max(ax1, bx1)
|
86
|
+
ix2 = min(ax2, bx2)
|
87
|
+
|
88
|
+
overlap_width = max(0, ix2 - ix1)
|
89
|
+
|
90
|
+
if overlap_width == 0:
|
91
|
+
return False
|
92
|
+
|
93
|
+
return True
|
94
|
+
|
95
|
+
|
96
|
+
def quad_to_xyxy(quad):
|
97
|
+
x1 = min([x for x, _ in quad])
|
98
|
+
y1 = min([y for _, y in quad])
|
99
|
+
x2 = max([x for x, _ in quad])
|
100
|
+
y2 = max([y for _, y in quad])
|
101
|
+
|
102
|
+
return x1, y1, x2, y2
|
@@ -0,0 +1,179 @@
|
|
1
|
+
import cv2
|
2
|
+
import numpy as np
|
3
|
+
from PIL import Image, ImageDraw, ImageFont
|
4
|
+
|
5
|
+
from ..constants import PALETTE
|
6
|
+
|
7
|
+
|
8
|
+
def _reading_order_visualizer(img, elements, line_color, tip_size):
|
9
|
+
out = img.copy()
|
10
|
+
for i, element in enumerate(elements):
|
11
|
+
if i == 0:
|
12
|
+
continue
|
13
|
+
|
14
|
+
prev_element = elements[i - 1]
|
15
|
+
cur_x1, cur_y1, cur_x2, cur_y2 = element.box
|
16
|
+
prev_x1, prev_y1, prev_x2, prev_y2 = prev_element.box
|
17
|
+
|
18
|
+
cur_center = (
|
19
|
+
cur_x1 + (cur_x2 - cur_x1) / 2,
|
20
|
+
cur_y1 + (cur_y2 - cur_y1) / 2,
|
21
|
+
)
|
22
|
+
prev_center = (
|
23
|
+
prev_x1 + (prev_x2 - prev_x1) / 2,
|
24
|
+
prev_y1 + (prev_y2 - prev_y1) / 2,
|
25
|
+
)
|
26
|
+
|
27
|
+
arrow_length = np.linalg.norm(np.array(cur_center) - np.array(prev_center))
|
28
|
+
|
29
|
+
# tipLength を計算(矢印長さに対する固定サイズの割合)
|
30
|
+
if arrow_length > 0:
|
31
|
+
tip_length = tip_size / arrow_length
|
32
|
+
else:
|
33
|
+
tip_length = 0 # 長さが0なら矢じりもゼロ
|
34
|
+
|
35
|
+
cv2.arrowedLine(
|
36
|
+
out,
|
37
|
+
(int(prev_center[0]), int(prev_center[1])),
|
38
|
+
(int(cur_center[0]), int(cur_center[1])),
|
39
|
+
line_color,
|
40
|
+
2,
|
41
|
+
tipLength=tip_length,
|
42
|
+
)
|
43
|
+
return out
|
44
|
+
|
45
|
+
|
46
|
+
def reading_order_visualizer(
|
47
|
+
img,
|
48
|
+
results,
|
49
|
+
line_color=(0, 0, 255),
|
50
|
+
tip_size=10,
|
51
|
+
visualize_figure_letter=False,
|
52
|
+
):
|
53
|
+
elements = results.paragraphs + results.tables + results.figures
|
54
|
+
elements = sorted(elements, key=lambda x: x.order)
|
55
|
+
|
56
|
+
out = _reading_order_visualizer(img, elements, line_color, tip_size)
|
57
|
+
|
58
|
+
if visualize_figure_letter:
|
59
|
+
for figure in results.figures:
|
60
|
+
out = _reading_order_visualizer(
|
61
|
+
out, figure.paragraphs, line_color=(0, 255, 0), tip_size=5
|
62
|
+
)
|
63
|
+
|
64
|
+
return out
|
65
|
+
|
66
|
+
|
67
|
+
def det_visualizer(preds, img, quads, vis_heatmap=False, line_color=(0, 255, 0)):
|
68
|
+
preds = preds["binary"][0]
|
69
|
+
binary = preds.detach().cpu().numpy()
|
70
|
+
out = img.copy()
|
71
|
+
h, w = out.shape[:2]
|
72
|
+
binary = binary.squeeze(0)
|
73
|
+
binary = (binary * 255).astype(np.uint8)
|
74
|
+
if vis_heatmap:
|
75
|
+
binary = cv2.resize(binary, (w, h), interpolation=cv2.INTER_LINEAR)
|
76
|
+
heatmap = cv2.applyColorMap(binary, cv2.COLORMAP_JET)
|
77
|
+
out = cv2.addWeighted(out, 0.5, heatmap, 0.5, 0)
|
78
|
+
|
79
|
+
for quad in quads:
|
80
|
+
quad = np.array(quad).astype(np.int32)
|
81
|
+
out = cv2.polylines(out, [quad], True, line_color, 1)
|
82
|
+
return out
|
83
|
+
|
84
|
+
|
85
|
+
def layout_visualizer(results, img):
|
86
|
+
out = img.copy()
|
87
|
+
results_dict = results.dict()
|
88
|
+
for id, (category, preds) in enumerate(results_dict.items()):
|
89
|
+
for element in preds:
|
90
|
+
box = element["box"]
|
91
|
+
role = element["role"]
|
92
|
+
|
93
|
+
if role is None:
|
94
|
+
role = ""
|
95
|
+
else:
|
96
|
+
role = f"({role})"
|
97
|
+
|
98
|
+
color = PALETTE[id % len(PALETTE)]
|
99
|
+
x1, y1, x2, y2 = tuple(map(int, box))
|
100
|
+
out = cv2.rectangle(out, (x1, y1), (x2, y2), color, 2)
|
101
|
+
out = cv2.putText(
|
102
|
+
out,
|
103
|
+
category + role,
|
104
|
+
(x1, y1),
|
105
|
+
cv2.FONT_HERSHEY_SIMPLEX,
|
106
|
+
0.5,
|
107
|
+
color,
|
108
|
+
2,
|
109
|
+
)
|
110
|
+
|
111
|
+
return out
|
112
|
+
|
113
|
+
|
114
|
+
def table_visualizer(img, table):
|
115
|
+
out = img.copy()
|
116
|
+
cells = table.cells
|
117
|
+
for cell in cells:
|
118
|
+
box = cell.box
|
119
|
+
row = cell.row
|
120
|
+
col = cell.col
|
121
|
+
row_span = cell.row_span
|
122
|
+
col_span = cell.col_span
|
123
|
+
|
124
|
+
text = f"[{row}, {col}] ({row_span}x{col_span})"
|
125
|
+
|
126
|
+
x1, y1, x2, y2 = map(int, box)
|
127
|
+
out = cv2.rectangle(out, (x1, y1), (x2, y2), (255, 0, 255), 2)
|
128
|
+
out = cv2.putText(
|
129
|
+
out,
|
130
|
+
text,
|
131
|
+
(x1, y1),
|
132
|
+
cv2.FONT_HERSHEY_SIMPLEX,
|
133
|
+
0.5,
|
134
|
+
(255, 0, 0),
|
135
|
+
2,
|
136
|
+
)
|
137
|
+
|
138
|
+
return out
|
139
|
+
|
140
|
+
|
141
|
+
def rec_visualizer(
|
142
|
+
img,
|
143
|
+
outputs,
|
144
|
+
font_path,
|
145
|
+
font_size=12,
|
146
|
+
font_color=(255, 0, 0),
|
147
|
+
):
|
148
|
+
out = img.copy()
|
149
|
+
pillow_img = Image.fromarray(out)
|
150
|
+
draw = ImageDraw.Draw(pillow_img)
|
151
|
+
|
152
|
+
for pred, quad, direction in zip(
|
153
|
+
outputs.contents, outputs.points, outputs.directions
|
154
|
+
):
|
155
|
+
quad = np.array(quad).astype(np.int32)
|
156
|
+
font = ImageFont.truetype(font_path, font_size)
|
157
|
+
if direction == "horizontal":
|
158
|
+
x_offset = 0
|
159
|
+
y_offset = -font_size
|
160
|
+
|
161
|
+
pos_x = quad[0][0] + x_offset
|
162
|
+
pox_y = quad[0][1] + y_offset
|
163
|
+
draw.text((pos_x, pox_y), pred, font=font, fill=font_color)
|
164
|
+
else:
|
165
|
+
x_offset = -font_size
|
166
|
+
y_offset = 0
|
167
|
+
|
168
|
+
pos_x = quad[0][0] + x_offset
|
169
|
+
pox_y = quad[0][1] + y_offset
|
170
|
+
draw.text(
|
171
|
+
(pos_x, pox_y),
|
172
|
+
pred,
|
173
|
+
font=font,
|
174
|
+
fill=font_color,
|
175
|
+
direction="ttb",
|
176
|
+
)
|
177
|
+
|
178
|
+
out = np.array(pillow_img)
|
179
|
+
return out
|
@@ -0,0 +1,127 @@
|
|
1
|
+
Metadata-Version: 2.3
|
2
|
+
Name: yomitoku
|
3
|
+
Version: 0.4.0.post1.dev0
|
4
|
+
Summary: Yomitoku is a document image analysis package powered by AI technology for the Japanese language.
|
5
|
+
Author-email: Kotaro Kinoshita <kotaro.kinoshita@mlism.com>
|
6
|
+
License: CC BY-NC-SA 4.0
|
7
|
+
Keywords: Deep Learning,Japanese,OCR
|
8
|
+
Requires-Python: >=3.9
|
9
|
+
Requires-Dist: huggingface-hub>=0.26.1
|
10
|
+
Requires-Dist: lxml>=5.3.0
|
11
|
+
Requires-Dist: omegaconf>=2.3.0
|
12
|
+
Requires-Dist: opencv-python>=4.10.0.84
|
13
|
+
Requires-Dist: pdf2image>=1.17.0
|
14
|
+
Requires-Dist: pyclipper>=1.3.0.post6
|
15
|
+
Requires-Dist: pydantic>=2.9.2
|
16
|
+
Requires-Dist: shapely>=2.0.6
|
17
|
+
Requires-Dist: timm>=1.0.11
|
18
|
+
Requires-Dist: torch>=2.5.0
|
19
|
+
Requires-Dist: torchvision>=0.20.0
|
20
|
+
Description-Content-Type: text/markdown
|
21
|
+
|
22
|
+
# YomiToku
|
23
|
+
|
24
|
+

|
25
|
+

|
26
|
+

|
27
|
+
[](https://kotaro-kinoshita.github.io/yomitoku-dev/)
|
28
|
+
|
29
|
+
<img src="static/logo/horizontal.png" width="800px">
|
30
|
+
|
31
|
+
## 🌟 概要
|
32
|
+
|
33
|
+
YomiToku は日本語に特化した AI 文章画像解析エンジン(Document AI)です。画像内の文字の全文 OCR およびレイアウト解析機能を有しており、画像内の文字情報や図表を認識、抽出、変換します。
|
34
|
+
|
35
|
+
- 🤖 日本語データセットで学習した 4 種類(文字位置の検知、文字列認識、レイアウト解析、表の構造認識)の AI モデルを搭載しています。4 種類のモデルはすべて独自に学習されたモデルで日本語文書に対して、高精度に推論可能です。
|
36
|
+
- 🇯🇵 各モデルは日本語の文書画像に特化して学習されており、7000 文字を超える日本語文字の認識をサーポート、縦書きなど日本語特有のレイアウト構造の文書画像の解析も可能です。(日本語以外にも英語の文書に対しても対応しています)。
|
37
|
+
- 📈 レイアウト解析、表の構造解析, 読み順推定機能により、文書画像のレイアウトの意味的構造を壊さずに情報を抽出することが可能です。
|
38
|
+
- 📄 多様な出力形式をサポートしています。html やマークダウン、json、csv のいずれかのフォーマットに変換可能です。また、文書内に含まれる図表、画像の抽出の出力も可能です。
|
39
|
+
- ⚡ GPU 環境で高速に動作し、効率的に文書の文字起こし解析が可能です。また、VRAM も 8GB 以内で動作し、ハイエンドな GPU を用意する必要はありません。
|
40
|
+
|
41
|
+
## 🖼️ デモ
|
42
|
+
|
43
|
+
[gallery.md](gallery.md)にも複数種類の画像の検証結果を掲載しています。
|
44
|
+
|
45
|
+
| 入力画像 | OCR の結果 |
|
46
|
+
| :--------------------------------------------------------: | :-----------------------------------------------------: |
|
47
|
+
| <img src="static/in/demo.jpg" width="400px"> | <img src="static/out/in_demo_p1_ocr.jpg" width="400px"> |
|
48
|
+
| レイアウト解析の結果 | エクスポート<br>(HTML で出力したものをスクショ) |
|
49
|
+
| <img src="static/out/in_demo_p1_layout.jpg" width="400px"> | <img src="static/out/demo_html.png" width="400px"> |
|
50
|
+
|
51
|
+
Markdown でエクスポートした結果は関してはリポジトリ内の[static/out/in_demo_p1.md](static/out/in_demo_p1.md)を参照
|
52
|
+
|
53
|
+
- `赤枠` : 図、画像等の位置
|
54
|
+
- `緑枠` : 表領域全体の位置
|
55
|
+
- `ピンク枠` : 表のセル構造(セル上の文字は [行番号, 列番号] (rowspan x colspan)を表します)
|
56
|
+
- `青枠` : 段落、テキストグループ領域
|
57
|
+
- `赤矢印` : 読み順推定の結果
|
58
|
+
|
59
|
+
画像の出典:[「令和 6 年版情報通信白書 3 章 2 節 AI の進化に伴い発展するテクノロジー」](https://www.soumu.go.jp/johotsusintokei/whitepaper/ja/r06/pdf/n1410000.pdf):(総務省) を加工して作成
|
60
|
+
|
61
|
+
## 📣 リリース情報
|
62
|
+
|
63
|
+
- 2024 年 12 月 XX YomiToku vX.X.X を公開
|
64
|
+
|
65
|
+
## 💡 インストールの方法
|
66
|
+
|
67
|
+
```
|
68
|
+
pip install git+https://github.com/kotaro-kinoshita/yomitoku-dev.git@main
|
69
|
+
```
|
70
|
+
|
71
|
+
- pytorch がご自身の GPU の環境にあったものをインストールしてください
|
72
|
+
|
73
|
+
### 依存ライブラリ
|
74
|
+
|
75
|
+
pdf ファイルの解析を行うためには、別途、[poppler](https://poppler.freedesktop.org/)のインストールが必要です。
|
76
|
+
|
77
|
+
**Mac**
|
78
|
+
|
79
|
+
```
|
80
|
+
brew install poppler
|
81
|
+
```
|
82
|
+
|
83
|
+
**Linux**
|
84
|
+
|
85
|
+
```
|
86
|
+
apt install poppler-utils -y
|
87
|
+
```
|
88
|
+
|
89
|
+
## 🚀 実行方法
|
90
|
+
|
91
|
+
```
|
92
|
+
yomitoku ${path_data} -f md -o results -v --figure
|
93
|
+
```
|
94
|
+
|
95
|
+
- `${path_data}` 解析対象の画像が含まれたディレクトリか画像ファイルのパスを直接して指定してください。ディレクトリを対象とした場合はディレクトリのサブディレクトリ内の画像も含めて処理を実行します。
|
96
|
+
- `-f`, `--format` 出力形式のファイルフォーマットを指定します。(json, csv, html, md をサポート)
|
97
|
+
- `-o`, `--outdir` 出力先のディレクトリ名を指定します。存在しない場合は新規で作成されます。
|
98
|
+
- `-v`, `--vis` を指定すると解析結果を可視化した画像を出力します。
|
99
|
+
- `-d`, `--device` モデルを実行するためのデバイスを指定します。gpu が利用できない場合は cpu で推論が実行されます。(デフォルト: cuda)
|
100
|
+
- `--ignore_line_break` 画像の改行位置を無視して、段落内の文章を連結して返します。(デフォルト:画像通りの改行位置位置で改行します。)
|
101
|
+
- `figure_letter` 検出した図表に含まれる文字も出力ファイルにエクスポートします。
|
102
|
+
- `figure` 検出した図、画像を出力ファイルにエクスポートします。(html と markdown のみ)
|
103
|
+
|
104
|
+
その他のオプションに関しては、ヘルプを参照
|
105
|
+
|
106
|
+
```
|
107
|
+
yomitoku --help
|
108
|
+
```
|
109
|
+
|
110
|
+
### Note
|
111
|
+
|
112
|
+
- CPU を用いての推論向けに最適化されておらず、処理時間が長くなりますので、GPU での実行を推奨します。
|
113
|
+
- 活字のみ識別をサポートしております。手書き文字に関しては、読み取れる場合もありますが、公式にはサポートしておりません。
|
114
|
+
- OCR は文書 OCR と情景 OCR(看板など紙以外にプリントされた文字)に大別されますが、Yomitoku は文書 OCR 向けに最適化されています。
|
115
|
+
- AI-OCR の識別精度を高めるために、入力画像の解像度が重要です。低解像度画像では識別精度が低下します。最低でも画像の短辺を 720px 以上の画像で推論することをお勧めします。
|
116
|
+
|
117
|
+
## 📝 ドキュメント
|
118
|
+
|
119
|
+
パッケージの詳細は[ドキュメント](https://kotaro-kinoshita.github.io/yomitoku-dev/)を確認してください。
|
120
|
+
|
121
|
+
## LICENSE
|
122
|
+
|
123
|
+
本リポジトリ内に格納されているリソースのライセンスは YomiToku は CC BY-NC-SA 4.0 に従います。
|
124
|
+
非商用での個人利用、研究目的での利用は自由に利用できます。
|
125
|
+
商用目的での利用に関しては、別途、商用ライセンスを提供しますので、開発者にお問い合わせください。
|
126
|
+
|
127
|
+
YomiToku © 2024 by MLism Inc. is licensed under CC BY-NC-SA 4.0. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/
|