Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pdf-miner
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Qin Kaijie
pdf-miner
Commits
ef267e09
Commit
ef267e09
authored
Mar 19, 2024
by
赵小蒙
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
qa需求定制输出
parent
eb79c884
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
9 deletions
+59
-9
ocr_demo.py
demo/ocr_demo.py
+18
-8
ocr_mkcontent.py
magic_pdf/dict2md/ocr_mkcontent.py
+13
-0
pipeline.py
magic_pdf/pipeline.py
+28
-1
No files found.
demo/ocr_demo.py
View file @
ef267e09
...
@@ -4,6 +4,7 @@ import os
...
@@ -4,6 +4,7 @@ import os
from
loguru
import
logger
from
loguru
import
logger
from
pathlib
import
Path
from
pathlib
import
Path
from
demo.demo_test
import
get_json_from_local_or_s3
from
magic_pdf.dict2md.ocr_mkcontent
import
ocr_mk_mm_markdown_with_para
,
ocr_mk_nlp_markdown
,
ocr_mk_mm_markdown
,
ocr_mk_mm_standard_format
from
magic_pdf.dict2md.ocr_mkcontent
import
ocr_mk_mm_markdown_with_para
,
ocr_mk_nlp_markdown
,
ocr_mk_mm_markdown
,
ocr_mk_mm_standard_format
from
magic_pdf.libs.commons
import
join_path
from
magic_pdf.libs.commons
import
join_path
from
magic_pdf.pdf_parse_by_ocr
import
parse_pdf_by_ocr
from
magic_pdf.pdf_parse_by_ocr
import
parse_pdf_by_ocr
...
@@ -29,14 +30,7 @@ def read_json_file(file_path):
...
@@ -29,14 +30,7 @@ def read_json_file(file_path):
return
data
return
data
if
__name__
==
'__main__'
:
def
ocr_local_parse
(
ocr_pdf_path
,
ocr_json_file_path
):
#ocr_pdf_path = r"D:\project\20231108code-clean\ocr\new\双栏\s0043-1354(02)00581-x.pdf"
#ocr_json_file_path = r"D:\project\20231108code-clean\ocr\new\双栏\s0043-1354(02)00581-x.json"
# ocr_pdf_path = r"D:\project\20231108code-clean\ocr\new\双栏\j.1540-627x.2006.00176.x.pdf"
# ocr_json_file_path = r"D:\project\20231108code-clean\ocr\new\双栏\j.1540-627x.2006.00176.x.json"
ocr_pdf_path
=
r"/home/cxu/workspace/Magic-PDF/ocr_demo/ocr_1.pdf"
ocr_json_file_path
=
r"/home/cxu/workspace/Magic-PDF/ocr_demo/ocr_1.json"
try
:
try
:
ocr_pdf_model_info
=
read_json_file
(
ocr_json_file_path
)
ocr_pdf_model_info
=
read_json_file
(
ocr_json_file_path
)
pth
=
Path
(
ocr_json_file_path
)
pth
=
Path
(
ocr_json_file_path
)
...
@@ -72,3 +66,19 @@ if __name__ == '__main__':
...
@@ -72,3 +66,19 @@ if __name__ == '__main__':
# save_markdown(markdown_text, ocr_json_file_path)
# save_markdown(markdown_text, ocr_json_file_path)
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
exception
(
e
)
logger
.
exception
(
e
)
def
ocr_online_parse
(
book_name
,
start_page_id
=
0
,
debug_mode
=
True
):
json_object
=
get_json_from_local_or_s3
(
book_name
)
logger
.
info
(
json_object
)
if
__name__
==
'__main__'
:
#ocr_pdf_path = r"D:\project\20231108code-clean\ocr\new\双栏\s0043-1354(02)00581-x.pdf"
#ocr_json_file_path = r"D:\project\20231108code-clean\ocr\new\双栏\s0043-1354(02)00581-x.json"
# ocr_pdf_path = r"D:\project\20231108code-clean\ocr\new\双栏\j.1540-627x.2006.00176.x.pdf"
# ocr_json_file_path = r"D:\project\20231108code-clean\ocr\new\双栏\j.1540-627x.2006.00176.x.json"
ocr_pdf_path
=
r"/home/cxu/workspace/Magic-PDF/ocr_demo/ocr_1.pdf"
ocr_json_file_path
=
r"/home/cxu/workspace/Magic-PDF/ocr_demo/ocr_1.json"
ocr_online_parse
(
book_name
=
"数学新星网/edu_00001236"
)
ocr_local_parse
(
ocr_pdf_path
,
ocr_json_file_path
)
pass
magic_pdf/dict2md/ocr_mkcontent.py
View file @
ef267e09
...
@@ -79,6 +79,19 @@ def ocr_mk_mm_markdown_with_para(pdf_info_dict: dict):
...
@@ -79,6 +79,19 @@ def ocr_mk_mm_markdown_with_para(pdf_info_dict: dict):
return
'
\n\n
'
.
join
(
markdown
)
return
'
\n\n
'
.
join
(
markdown
)
def
make_standard_format_with_para
(
pdf_info_dict
:
dict
):
content_list
=
[]
for
_
,
page_info
in
pdf_info_dict
.
items
():
paras
=
page_info
.
get
(
"para_blocks"
)
if
not
paras
:
continue
for
para
in
paras
:
for
line
in
para
:
content
=
line_to_standard_format
(
line
)
content_list
.
append
(
content
)
return
content_list
def
line_to_standard_format
(
line
):
def
line_to_standard_format
(
line
):
line_text
=
""
line_text
=
""
inline_equation_num
=
0
inline_equation_num
=
0
...
...
magic_pdf/pipeline.py
View file @
ef267e09
...
@@ -3,7 +3,8 @@ import sys
...
@@ -3,7 +3,8 @@ import sys
import
time
import
time
from
urllib.parse
import
quote
from
urllib.parse
import
quote
from
magic_pdf.dict2md.ocr_mkcontent
import
ocr_mk_nlp_markdown
,
ocr_mk_mm_markdown
,
ocr_mk_mm_standard_format
from
magic_pdf.dict2md.ocr_mkcontent
import
ocr_mk_nlp_markdown
,
ocr_mk_mm_markdown
,
ocr_mk_mm_standard_format
,
\
ocr_mk_mm_markdown_with_para
from
magic_pdf.libs.commons
import
read_file
,
join_path
,
parse_bucket_key
,
formatted_time
,
s3_image_save_path
from
magic_pdf.libs.commons
import
read_file
,
join_path
,
parse_bucket_key
,
formatted_time
,
s3_image_save_path
from
magic_pdf.libs.drop_reason
import
DropReason
from
magic_pdf.libs.drop_reason
import
DropReason
from
magic_pdf.libs.json_compressor
import
JsonCompressor
from
magic_pdf.libs.json_compressor
import
JsonCompressor
...
@@ -407,6 +408,32 @@ def ocr_pdf_intermediate_dict_to_markdown(jso: dict, debug_mode=False) -> dict:
...
@@ -407,6 +408,32 @@ def ocr_pdf_intermediate_dict_to_markdown(jso: dict, debug_mode=False) -> dict:
return
jso
return
jso
def
ocr_pdf_intermediate_dict_to_markdown_with_para_for_qa
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
'need_drop'
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
'file_id'
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
'pdf_intermediate_dict'
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
markdown_content
=
ocr_mk_mm_markdown_with_para
(
pdf_intermediate_dict
)
jso
[
"content_ocr"
]
=
markdown_content
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},markdown content length is {len(markdown_content)}"
,
file
=
sys
.
stderr
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_standard_format
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
def
ocr_pdf_intermediate_dict_to_standard_format
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
if
debug_mode
:
...
...
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