deepdoc-lib 0.2.0__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.
- deepdoc/README.md +122 -0
- deepdoc/README_zh.md +116 -0
- deepdoc/__init__.py +43 -0
- deepdoc/_version.py +34 -0
- deepdoc/common/__init__.py +52 -0
- deepdoc/common/config_utils.py +63 -0
- deepdoc/common/connection_utils.py +73 -0
- deepdoc/common/file_utils.py +19 -0
- deepdoc/common/misc_utils.py +44 -0
- deepdoc/common/model_store.py +369 -0
- deepdoc/common/settings.py +42 -0
- deepdoc/common/tiktoken_cache.py +84 -0
- deepdoc/common/token_utils.py +96 -0
- deepdoc/config.py +149 -0
- deepdoc/depend/find_codec.py +42 -0
- deepdoc/depend/nltk_manager.py +114 -0
- deepdoc/depend/prompts/vision_llm_describe_prompt.md +23 -0
- deepdoc/depend/prompts/vision_llm_figure_describe_prompt.md +24 -0
- deepdoc/depend/prompts.py +35 -0
- deepdoc/depend/rag_tokenizer.py +578 -0
- deepdoc/depend/simple_cv_model.py +469 -0
- deepdoc/depend/surname.py +91 -0
- deepdoc/depend/timeout.py +73 -0
- deepdoc/depend/vision_llm_chunk.py +35 -0
- deepdoc/dict/README.md +19 -0
- deepdoc/dict/huqie.txt +555629 -0
- deepdoc/download_models.py +169 -0
- deepdoc/llm_adapter/__init__.py +15 -0
- deepdoc/llm_adapter/adapter.py +223 -0
- deepdoc/llm_adapter/utils.py +104 -0
- deepdoc/llm_adapter/vision.py +163 -0
- deepdoc/parser/__init__.py +42 -0
- deepdoc/parser/docling_parser.py +889 -0
- deepdoc/parser/docx_parser.py +150 -0
- deepdoc/parser/excel_parser.py +270 -0
- deepdoc/parser/figure_parser.py +182 -0
- deepdoc/parser/html_parser.py +221 -0
- deepdoc/parser/json_parser.py +179 -0
- deepdoc/parser/markdown_parser.py +321 -0
- deepdoc/parser/mineru_parser.py +646 -0
- deepdoc/parser/pdf_parser.py +1591 -0
- deepdoc/parser/ppt_parser.py +96 -0
- deepdoc/parser/resume/__init__.py +109 -0
- deepdoc/parser/resume/entities/__init__.py +15 -0
- deepdoc/parser/resume/entities/corporations.py +128 -0
- deepdoc/parser/resume/entities/degrees.py +44 -0
- deepdoc/parser/resume/entities/industries.py +712 -0
- deepdoc/parser/resume/entities/regions.py +789 -0
- deepdoc/parser/resume/entities/res/corp.tks.freq.json +65 -0
- deepdoc/parser/resume/entities/res/corp_baike_len.csv +31480 -0
- deepdoc/parser/resume/entities/res/corp_tag.json +14939 -0
- deepdoc/parser/resume/entities/res/good_corp.json +911 -0
- deepdoc/parser/resume/entities/res/good_sch.json +595 -0
- deepdoc/parser/resume/entities/res/school.rank.csv +1627 -0
- deepdoc/parser/resume/entities/res/schools.csv +5713 -0
- deepdoc/parser/resume/entities/schools.py +91 -0
- deepdoc/parser/resume/step_one.py +189 -0
- deepdoc/parser/resume/step_two.py +692 -0
- deepdoc/parser/tcadp_parser.py +538 -0
- deepdoc/parser/txt_parser.py +64 -0
- deepdoc/parser/utils.py +33 -0
- deepdoc/vision/__init__.py +90 -0
- deepdoc/vision/layout_recognizer.py +481 -0
- deepdoc/vision/ocr.py +757 -0
- deepdoc/vision/operators.py +733 -0
- deepdoc/vision/postprocess.py +370 -0
- deepdoc/vision/recognizer.py +451 -0
- deepdoc/vision/seeit.py +87 -0
- deepdoc/vision/t_ocr.py +101 -0
- deepdoc/vision/t_recognizer.py +186 -0
- deepdoc/vision/table_structure_recognizer.py +617 -0
- deepdoc_lib-0.2.0.dist-info/METADATA +246 -0
- deepdoc_lib-0.2.0.dist-info/RECORD +78 -0
- deepdoc_lib-0.2.0.dist-info/WHEEL +5 -0
- deepdoc_lib-0.2.0.dist-info/entry_points.txt +2 -0
- deepdoc_lib-0.2.0.dist-info/licenses/LICENSE +201 -0
- deepdoc_lib-0.2.0.dist-info/top_level.txt +2 -0
- scripts/download_models.py +10 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
#
|
|
16
|
+
|
|
17
|
+
import logging
|
|
18
|
+
import os
|
|
19
|
+
import sys
|
|
20
|
+
|
|
21
|
+
sys.path.insert(
|
|
22
|
+
0,
|
|
23
|
+
os.path.abspath(
|
|
24
|
+
os.path.join(
|
|
25
|
+
os.path.dirname(
|
|
26
|
+
os.path.abspath(__file__)),
|
|
27
|
+
'../../')))
|
|
28
|
+
|
|
29
|
+
from deepdoc.vision.seeit import draw_box
|
|
30
|
+
from deepdoc.vision import LayoutRecognizer, TableStructureRecognizer, OCR, init_in_out
|
|
31
|
+
import argparse
|
|
32
|
+
import re
|
|
33
|
+
import numpy as np
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def main(args):
|
|
37
|
+
images, outputs = init_in_out(args)
|
|
38
|
+
if args.mode.lower() == "layout":
|
|
39
|
+
detr = LayoutRecognizer("layout")
|
|
40
|
+
layouts = detr.forward(images, thr=float(args.threshold))
|
|
41
|
+
if args.mode.lower() == "tsr":
|
|
42
|
+
detr = TableStructureRecognizer()
|
|
43
|
+
ocr = OCR()
|
|
44
|
+
layouts = detr(images, thr=float(args.threshold))
|
|
45
|
+
for i, lyt in enumerate(layouts):
|
|
46
|
+
if args.mode.lower() == "tsr":
|
|
47
|
+
#lyt = [t for t in lyt if t["type"] == "table column"]
|
|
48
|
+
html = get_table_html(images[i], lyt, ocr)
|
|
49
|
+
with open(outputs[i] + ".html", "w+", encoding='utf-8') as f:
|
|
50
|
+
f.write(html)
|
|
51
|
+
lyt = [{
|
|
52
|
+
"type": t["label"],
|
|
53
|
+
"bbox": [t["x0"], t["top"], t["x1"], t["bottom"]],
|
|
54
|
+
"score": t["score"]
|
|
55
|
+
} for t in lyt]
|
|
56
|
+
img = draw_box(images[i], lyt, detr.labels, float(args.threshold))
|
|
57
|
+
img.save(outputs[i], quality=95)
|
|
58
|
+
logging.info("save result to: " + outputs[i])
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def get_table_html(img, tb_cpns, ocr):
|
|
62
|
+
boxes = ocr(np.array(img))
|
|
63
|
+
boxes = LayoutRecognizer.sort_Y_firstly(
|
|
64
|
+
[{"x0": b[0][0], "x1": b[1][0],
|
|
65
|
+
"top": b[0][1], "text": t[0],
|
|
66
|
+
"bottom": b[-1][1],
|
|
67
|
+
"layout_type": "table",
|
|
68
|
+
"page_number": 0} for b, t in boxes if b[0][0] <= b[1][0] and b[0][1] <= b[-1][1]],
|
|
69
|
+
np.mean([b[-1][1] - b[0][1] for b, _ in boxes]) / 3
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
def gather(kwd, fzy=10, ption=0.6):
|
|
73
|
+
nonlocal boxes
|
|
74
|
+
eles = LayoutRecognizer.sort_Y_firstly(
|
|
75
|
+
[r for r in tb_cpns if re.match(kwd, r["label"])], fzy)
|
|
76
|
+
eles = LayoutRecognizer.layouts_cleanup(boxes, eles, 5, ption)
|
|
77
|
+
return LayoutRecognizer.sort_Y_firstly(eles, 0)
|
|
78
|
+
|
|
79
|
+
headers = gather(r".*header$")
|
|
80
|
+
rows = gather(r".* (row|header)")
|
|
81
|
+
spans = gather(r".*spanning")
|
|
82
|
+
clmns = sorted([r for r in tb_cpns if re.match(
|
|
83
|
+
r"table column$", r["label"])], key=lambda x: x["x0"])
|
|
84
|
+
clmns = LayoutRecognizer.layouts_cleanup(boxes, clmns, 5, 0.5)
|
|
85
|
+
|
|
86
|
+
for b in boxes:
|
|
87
|
+
ii = LayoutRecognizer.find_overlapped_with_threshold(b, rows, thr=0.3)
|
|
88
|
+
if ii is not None:
|
|
89
|
+
b["R"] = ii
|
|
90
|
+
b["R_top"] = rows[ii]["top"]
|
|
91
|
+
b["R_bott"] = rows[ii]["bottom"]
|
|
92
|
+
|
|
93
|
+
ii = LayoutRecognizer.find_overlapped_with_threshold(b, headers, thr=0.3)
|
|
94
|
+
if ii is not None:
|
|
95
|
+
b["H_top"] = headers[ii]["top"]
|
|
96
|
+
b["H_bott"] = headers[ii]["bottom"]
|
|
97
|
+
b["H_left"] = headers[ii]["x0"]
|
|
98
|
+
b["H_right"] = headers[ii]["x1"]
|
|
99
|
+
b["H"] = ii
|
|
100
|
+
|
|
101
|
+
ii = LayoutRecognizer.find_horizontally_tightest_fit(b, clmns)
|
|
102
|
+
if ii is not None:
|
|
103
|
+
b["C"] = ii
|
|
104
|
+
b["C_left"] = clmns[ii]["x0"]
|
|
105
|
+
b["C_right"] = clmns[ii]["x1"]
|
|
106
|
+
|
|
107
|
+
ii = LayoutRecognizer.find_overlapped_with_threshold(b, spans, thr=0.3)
|
|
108
|
+
if ii is not None:
|
|
109
|
+
b["H_top"] = spans[ii]["top"]
|
|
110
|
+
b["H_bott"] = spans[ii]["bottom"]
|
|
111
|
+
b["H_left"] = spans[ii]["x0"]
|
|
112
|
+
b["H_right"] = spans[ii]["x1"]
|
|
113
|
+
b["SP"] = ii
|
|
114
|
+
|
|
115
|
+
html = """
|
|
116
|
+
<html>
|
|
117
|
+
<head>
|
|
118
|
+
<style>
|
|
119
|
+
._table_1nkzy_11 {
|
|
120
|
+
margin: auto;
|
|
121
|
+
width: 70%%;
|
|
122
|
+
padding: 10px;
|
|
123
|
+
}
|
|
124
|
+
._table_1nkzy_11 p {
|
|
125
|
+
margin-bottom: 50px;
|
|
126
|
+
border: 1px solid #e1e1e1;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
caption {
|
|
130
|
+
color: #6ac1ca;
|
|
131
|
+
font-size: 20px;
|
|
132
|
+
height: 50px;
|
|
133
|
+
line-height: 50px;
|
|
134
|
+
font-weight: 600;
|
|
135
|
+
margin-bottom: 10px;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
._table_1nkzy_11 table {
|
|
139
|
+
width: 100%%;
|
|
140
|
+
border-collapse: collapse;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
th {
|
|
144
|
+
color: #fff;
|
|
145
|
+
background-color: #6ac1ca;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
td:hover {
|
|
149
|
+
background: #c1e8e8;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
tr:nth-child(even) {
|
|
153
|
+
background-color: #f2f2f2;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
._table_1nkzy_11 th,
|
|
157
|
+
._table_1nkzy_11 td {
|
|
158
|
+
text-align: center;
|
|
159
|
+
border: 1px solid #ddd;
|
|
160
|
+
padding: 8px;
|
|
161
|
+
}
|
|
162
|
+
</style>
|
|
163
|
+
</head>
|
|
164
|
+
<body>
|
|
165
|
+
%s
|
|
166
|
+
</body>
|
|
167
|
+
</html>
|
|
168
|
+
""" % TableStructureRecognizer.construct_table(boxes, html=True)
|
|
169
|
+
return html
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
if __name__ == "__main__":
|
|
173
|
+
parser = argparse.ArgumentParser()
|
|
174
|
+
parser.add_argument('--inputs',
|
|
175
|
+
help="Directory where to store images or PDFs, or a file path to a single image or PDF",
|
|
176
|
+
required=True)
|
|
177
|
+
parser.add_argument('--output_dir', help="Directory where to store the output images. Default: './layouts_outputs'",
|
|
178
|
+
default="./layouts_outputs")
|
|
179
|
+
parser.add_argument(
|
|
180
|
+
'--threshold',
|
|
181
|
+
help="A threshold to filter out detections. Default: 0.5",
|
|
182
|
+
default=0.5)
|
|
183
|
+
parser.add_argument('--mode', help="Task mode: layout recognition or table structure recognition", choices=["layout", "tsr"],
|
|
184
|
+
default="layout")
|
|
185
|
+
args = parser.parse_args()
|
|
186
|
+
main(args)
|