Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleOCR
提交
1a0a75e3
P
PaddleOCR
项目概览
s920243400
/
PaddleOCR
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleOCR
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleOCR
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
1a0a75e3
编写于
8月 24, 2022
作者:
qq_25193841
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add pdf2word exe
Add pdf2word exe
上级
d18e8172
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
450 addition
and
8 deletion
+450
-8
ppstructure/pdf2word/icons/chinese.png
ppstructure/pdf2word/icons/chinese.png
+0
-0
ppstructure/pdf2word/icons/english.png
ppstructure/pdf2word/icons/english.png
+0
-0
ppstructure/pdf2word/icons/folder-open.png
ppstructure/pdf2word/icons/folder-open.png
+0
-0
ppstructure/pdf2word/icons/folder-plus.png
ppstructure/pdf2word/icons/folder-plus.png
+0
-0
ppstructure/pdf2word/pdf2word.md
ppstructure/pdf2word/pdf2word.md
+9
-8
ppstructure/pdf2word/pdf2word.py
ppstructure/pdf2word/pdf2word.py
+441
-0
未找到文件。
icons/chinese.png
→
ppstructure/pdf2word/
icons/chinese.png
浏览文件 @
1a0a75e3
文件已移动
icons/english.png
→
ppstructure/pdf2word/
icons/english.png
浏览文件 @
1a0a75e3
文件已移动
icons/folder-open.png
→
ppstructure/pdf2word/
icons/folder-open.png
浏览文件 @
1a0a75e3
文件已移动
icons/folder-plus.png
→
ppstructure/pdf2word/
icons/folder-plus.png
浏览文件 @
1a0a75e3
文件已移动
PDF2WORD
.md
→
ppstructure/pdf2word/pdf2word
.md
浏览文件 @
1a0a75e3
# PDF2WORD
# PDF2WORD
PDF2WORD是PaddleOCR社区开发者@whj 基于PP-Structure智能文档分析
系统实现的PDF转换w
ord应用程序,提供可直接安装的exe,方便windows用户运行
PDF2WORD是PaddleOCR社区开发者@whj 基于PP-Structure智能文档分析
模型实现的PDF转换W
ord应用程序,提供可直接安装的exe,方便windows用户运行
<div
align=
"center"
>
<div
align=
"center"
>
<img
src=
"./doc/imgs_results/PP-OCRv3/en/en_4.png"
width=
"
8
00"
>
<img
src=
"./doc/imgs_results/PP-OCRv3/en/en_4.png"
width=
"
2
00"
>
</div>
</div>
## 1.使用
## 1.使用
### 应用程序
### 应用程序
...
@@ -16,17 +17,15 @@ PDF2WORD是PaddleOCR社区开发者@whj 基于PP-Structure智能文档分析系
...
@@ -16,17 +17,15 @@ PDF2WORD是PaddleOCR社区开发者@whj 基于PP-Structure智能文档分析系
### 脚本运行
### 脚本运行
首次运行需要将
首次运行需要将
切换路径到
`/ppstructure/pdf2word`
,然后运行代码
```
```
python pdf2word.py
python pdf2word.py
```
```
## 2.自行打包
## 2.自行打包
PDF2WORD应用程序通过
[
QPT
](
https://github.com/QPT-Family/QPT
)
工具打包实现,若您修改了界面代码需要重新打包,请在
`
ppstructure
`
文件夹下运行下方指令
PDF2WORD应用程序通过
[
QPT
](
https://github.com/QPT-Family/QPT
)
工具打包实现,若您修改了界面代码需要重新打包,请在
`
PaddleOCR
`
文件夹下运行下方指令
```
```
cd ./
cd ./
...
@@ -34,9 +33,11 @@ mv ./ppstructure/pdf2word .. -r
...
@@ -34,9 +33,11 @@ mv ./ppstructure/pdf2word .. -r
python GenEXE.py
python GenEXE.py
```
```
## 3.软件下载
## 3.软件下载
如需获取已打包程序,可以扫描下方二维码,关注公众号填写问卷后,加入PaddleOCR官方交流群免费获取20G OCR学习大礼包,内含OCR场景应用集合(包含数码管、液晶屏、车牌、高精度SVTR模型等7个垂类模型)、《动手学OCR》电子书、课程回放视频、前沿论文等重磅资料
如需获取已打包程序,可以扫描下方二维码,关注公众号填写问卷后,加入PaddleOCR官方交流群免费获取20G OCR学习大礼包,内含OCR场景应用集合(包含数码管、液晶屏、车牌、高精度SVTR模型等7个垂类模型)、《动手学OCR》电子书、课程回放视频、前沿论文等重磅资料
<div
align=
"center"
>
<img
src=
"https://user-images.githubusercontent.com/50011306/186369636-35f2008b-df5a-4784-b1f5-cebebcb2b7a5.jpg"
width =
"150"
height =
"150"
/>
</div>
image2doc
.py
→
ppstructure/pdf2word/pdf2word
.py
浏览文件 @
1a0a75e3
...
@@ -2,22 +2,30 @@ import sys
...
@@ -2,22 +2,30 @@ import sys
import
tarfile
import
tarfile
import
os
import
os
import
time
import
time
import
datetime
import
functools
import
functools
import
cv2
import
cv2
import
platform
import
platform
import
numpy
as
np
import
numpy
as
np
from
qtpy
import
QtWidgets
from
qtpy.QtWidgets
import
QApplication
,
QWidget
,
QPushButton
,
QProgressBar
,
\
QGridLayout
,
QMessageBox
,
QLabel
,
QFileDialog
from
qtpy.QtCore
import
Signal
,
QThread
,
QObject
from
qtpy.QtGui
import
QImage
,
QPixmap
,
QIcon
from
qtpy.QtGui
import
QImage
,
QPixmap
,
QIcon
file
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
root
=
os
.
path
.
abspath
(
os
.
path
.
join
(
file
,
'../../'
))
sys
.
path
.
append
(
file
)
sys
.
path
.
insert
(
0
,
root
)
from
ppstructure.predict_system
import
StructureSystem
,
save_structure_res
from
ppstructure.predict_system
import
StructureSystem
,
save_structure_res
from
ppstructure.utility
import
parse_args
,
draw_structure_result
from
ppstructure.utility
import
parse_args
,
draw_structure_result
from
ppocr.utils.network
import
download_with_progressbar
from
ppocr.utils.network
import
download_with_progressbar
from
ppstructure.recovery.recovery_to_doc
import
sorted_layout_boxes
,
convert_info_docx
from
ppstructure.recovery.recovery_to_doc
import
sorted_layout_boxes
,
convert_info_docx
from
ScreenShotWidget
import
ScreenShotWidget
from
ScreenShotWidget
import
ScreenShotWidget
__APPNAME__
=
"
Image2Doc
"
__APPNAME__
=
"
pdf2word
"
__VERSION__
=
"0.
0.2
"
__VERSION__
=
"0.
1.1
"
here
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
URLs_EN
=
{
URLs_EN
=
{
# 下载超英文轻量级PP-OCRv3模型的检测模型并解压
# 下载超英文轻量级PP-OCRv3模型的检测模型并解压
"en_PP-OCRv3_det_infer"
:
"https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar"
,
"en_PP-OCRv3_det_infer"
:
"https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar"
,
...
@@ -49,7 +57,8 @@ DICT_CN = {
...
@@ -49,7 +57,8 @@ DICT_CN = {
}
}
def
QImageToCvMat
(
incomingImage
):
def
QImageToCvMat
(
incomingImage
)
->
np
.
array
:
'''
'''
Converts a QImage into an opencv MAT format
Converts a QImage into an opencv MAT format
'''
'''
...
@@ -65,20 +74,125 @@ def QImageToCvMat(incomingImage):
...
@@ -65,20 +74,125 @@ def QImageToCvMat(incomingImage):
return
arr
return
arr
class
APP_Image2Doc
(
QtWidgets
.
QWidget
):
def
readImage
(
image_file
)
->
list
:
if
os
.
path
.
basename
(
image_file
)[
-
3
:]
in
[
'pdf'
]:
import
fitz
from
PIL
import
Image
imgs
=
[]
with
fitz
.
open
(
image_file
)
as
pdf
:
for
pg
in
range
(
0
,
pdf
.
pageCount
):
page
=
pdf
[
pg
]
mat
=
fitz
.
Matrix
(
2
,
2
)
pm
=
page
.
getPixmap
(
matrix
=
mat
,
alpha
=
False
)
# if width or height > 2000 pixels, don't enlarge the image
if
pm
.
width
>
2000
or
pm
.
height
>
2000
:
pm
=
page
.
getPixmap
(
matrix
=
fitz
.
Matrix
(
1
,
1
),
alpha
=
False
)
img
=
Image
.
frombytes
(
"RGB"
,
[
pm
.
width
,
pm
.
height
],
pm
.
samples
)
img
=
cv2
.
cvtColor
(
np
.
array
(
img
),
cv2
.
COLOR_RGB2BGR
)
imgs
.
append
(
img
)
else
:
img
=
cv2
.
imread
(
image_file
,
cv2
.
IMREAD_COLOR
)
if
img
is
not
None
:
imgs
=
[
img
]
return
imgs
class
Worker
(
QThread
):
progressBarValue
=
Signal
(
int
)
endsignal
=
Signal
()
loopFlag
=
True
def
__init__
(
self
,
predictors
,
save_pdf
,
vis_font_path
):
super
(
Worker
,
self
).
__init__
()
self
.
predictors
=
predictors
self
.
save_pdf
=
save_pdf
self
.
vis_font_path
=
vis_font_path
self
.
lang
=
'EN'
self
.
imagePaths
=
[]
self
.
outputDir
=
None
self
.
setStackSize
(
1024
*
1024
)
def
setImagePath
(
self
,
imagePaths
):
self
.
imagePaths
=
imagePaths
def
setLang
(
self
,
lang
):
self
.
lang
=
lang
def
setOutputDir
(
self
,
outputDir
):
self
.
outputDir
=
outputDir
def
predictAndSave
(
self
,
imgs
,
img_name
):
all_res
=
[]
for
index
,
img
in
enumerate
(
imgs
):
res
,
time_dict
=
self
.
predictors
[
self
.
lang
](
img
)
# save output
save_structure_res
(
res
,
self
.
outputDir
,
img_name
)
draw_img
=
draw_structure_result
(
img
,
res
,
self
.
vis_font_path
)
img_save_path
=
os
.
path
.
join
(
self
.
outputDir
,
img_name
,
'show_{}.jpg'
.
format
(
index
))
if
res
!=
[]:
cv2
.
imwrite
(
img_save_path
,
draw_img
)
# recovery
h
,
w
,
_
=
img
.
shape
res
=
sorted_layout_boxes
(
res
,
w
)
all_res
+=
res
try
:
convert_info_docx
(
img
,
all_res
,
self
.
outputDir
,
img_name
,
self
.
save_pdf
)
except
Exception
as
ex
:
print
(
self
,
"error in layout recovery image:{}, err msg: {}"
.
format
(
img_name
,
ex
))
print
(
'result save to {}'
.
format
(
self
.
outputDir
))
def
run
(
self
):
try
:
findex
=
0
os
.
makedirs
(
self
.
outputDir
,
exist_ok
=
True
)
for
i
,
image_file
in
enumerate
(
self
.
imagePaths
):
if
self
.
loopFlag
==
True
:
imgs
=
readImage
(
image_file
)
if
len
(
imgs
)
==
0
:
continue
img_name
=
os
.
path
.
basename
(
image_file
).
split
(
'.'
)[
0
]
os
.
makedirs
(
os
.
path
.
join
(
self
.
outputDir
,
img_name
),
exist_ok
=
True
)
self
.
predictAndSave
(
imgs
,
img_name
)
findex
+=
1
self
.
progressBarValue
.
emit
(
findex
)
else
:
break
self
.
endsignal
.
emit
()
self
.
exec
()
except
Exception
as
e
:
print
(
e
)
raise
class
APP_Image2Doc
(
QWidget
):
def
__init__
(
self
):
def
__init__
(
self
):
super
(
QtWidgets
.
QWidget
,
self
).
__init__
()
super
().
__init__
()
self
.
pb
=
None
# 进度条
self
.
setFixedHeight
(
90
)
self
.
pb_text
=
"已载入: {} / 已转换: {}"
self
.
setFixedWidth
(
400
)
# settings
self
.
imagePaths
=
[]
self
.
imagePaths
=
[]
# self.resultPath = os.path.join(here, "output")
self
.
screenShotWg
=
ScreenShotWidget
()
self
.
screenShotWg
=
ScreenShotWidget
()
self
.
screenShot
=
None
self
.
screenShot
=
None
self
.
save_pdf
=
False
self
.
save_pdf
=
False
self
.
output_dir
=
None
self
.
vis_font_path
=
os
.
path
.
join
(
here
,
self
.
vis_font_path
=
os
.
path
.
join
(
root
,
"doc"
,
"fonts"
,
"simfang.ttf"
)
"doc"
,
"fonts"
,
"simfang.ttf"
)
# ProgressBar
self
.
pb
=
QProgressBar
()
self
.
pb
.
setRange
(
0
,
100
)
self
.
pb
.
setValue
(
0
)
# 初始化界面
# 初始化界面
self
.
setupUi
()
self
.
setupUi
()
...
@@ -86,45 +200,58 @@ class APP_Image2Doc(QtWidgets.QWidget):
...
@@ -86,45 +200,58 @@ class APP_Image2Doc(QtWidgets.QWidget):
self
.
downloadModels
(
URLs_EN
)
self
.
downloadModels
(
URLs_EN
)
self
.
downloadModels
(
URLs_CN
)
self
.
downloadModels
(
URLs_CN
)
self
.
structure_sys_en
=
self
.
initPredictor
(
'EN'
)
# 初始化模型
self
.
structure_sys_cn
=
self
.
initPredictor
(
'CN'
)
predictors
=
{
'EN'
:
self
.
initPredictor
(
'EN'
),
'CN'
:
self
.
initPredictor
(
'CN'
),
}
# 设置工作进程
self
.
_thread
=
Worker
(
predictors
,
self
.
save_pdf
,
self
.
vis_font_path
)
self
.
_thread
.
progressBarValue
.
connect
(
self
.
handleProgressBarSingal
)
self
.
_thread
.
endsignal
.
connect
(
self
.
handleEndsignalSignal
)
self
.
_thread
.
finished
.
connect
(
QObject
.
deleteLater
)
self
.
time_start
=
0
# save start time
def
setupUi
(
self
):
def
setupUi
(
self
):
self
.
setObjectName
(
"MainWindow"
)
self
.
setObjectName
(
"MainWindow"
)
self
.
setWindowTitle
(
__APPNAME__
+
" "
+
__VERSION__
)
self
.
setWindowTitle
(
__APPNAME__
+
" "
+
__VERSION__
)
layout
=
Q
tWidgets
.
Q
GridLayout
()
layout
=
QGridLayout
()
openFileButton
=
QtWidgets
.
QPushButton
(
"打开文件"
)
self
.
openFileButton
=
QPushButton
(
"打开文件"
)
openFileButton
.
setIcon
(
QIcon
(
QPixmap
(
"./icons/folder-plus.png"
)))
self
.
openFileButton
.
setIcon
(
QIcon
(
QPixmap
(
"./icons/folder-plus.png"
)))
layout
.
addWidget
(
openFileButton
,
0
,
0
,
1
,
1
)
layout
.
addWidget
(
self
.
openFileButton
,
0
,
0
,
1
,
1
)
openFileButton
.
clicked
.
connect
(
self
.
openFileSlot
)
self
.
openFileButton
.
clicked
.
connect
(
self
.
handleOpenFileSignal
)
# screenShotButton = Q
tWidgets.Q
PushButton("截图识别")
# screenShotButton = QPushButton("截图识别")
# layout.addWidget(screenShotButton, 0, 1, 1, 1)
# layout.addWidget(screenShotButton, 0, 1, 1, 1)
# screenShotButton.clicked.connect(self.screenShotSlot)
# screenShotButton.clicked.connect(self.screenShotSlot)
# screenShotButton.setEnabled(False) # temporarily disenble
# screenShotButton.setEnabled(False) # temporarily disenble
startCNShotButton
=
QtWidgets
.
QPushButton
(
"中文转换"
)
self
.
startCNButton
=
QPushButton
(
"中文转换"
)
startCNShotButton
.
setIcon
(
QIcon
(
QPixmap
(
"./icons/chinese.png"
)))
self
.
startCNButton
.
setIcon
(
QIcon
(
QPixmap
(
"./icons/chinese.png"
)))
layout
.
addWidget
(
startCNShotButton
,
0
,
1
,
1
,
1
)
layout
.
addWidget
(
self
.
startCNButton
,
0
,
1
,
1
,
1
)
startCNShotButton
.
clicked
.
connect
(
self
.
startCNButton
.
clicked
.
connect
(
functools
.
partial
(
self
.
startSlot
,
'CN'
))
functools
.
partial
(
self
.
handleStartSignal
,
'CN'
))
startENButton
=
QtWidgets
.
QPushButton
(
"英文转换"
)
self
.
startENButton
=
QPushButton
(
"英文转换"
)
startENButton
.
setIcon
(
QIcon
(
QPixmap
(
"./icons/english.png"
)))
self
.
startENButton
.
setIcon
(
QIcon
(
QPixmap
(
"./icons/english.png"
)))
layout
.
addWidget
(
startENButton
,
0
,
2
,
1
,
1
)
layout
.
addWidget
(
self
.
startENButton
,
0
,
2
,
1
,
1
)
startENButton
.
clicked
.
connect
(
self
.
startENButton
.
clicked
.
connect
(
functools
.
partial
(
self
.
startSlot
,
'EN'
))
functools
.
partial
(
self
.
handleStartSignal
,
'EN'
))
showResultButton
=
QtWidgets
.
QPushButton
(
"显示结果"
)
self
.
showResultButton
=
QPushButton
(
"显示结果"
)
showResultButton
.
setIcon
(
QIcon
(
QPixmap
(
"./icons/folder-open.png"
)))
self
.
showResultButton
.
setIcon
(
QIcon
(
QPixmap
(
"./icons/folder-open.png"
)))
layout
.
addWidget
(
showResultButton
,
0
,
3
,
1
,
1
)
layout
.
addWidget
(
self
.
showResultButton
,
0
,
3
,
1
,
1
)
showResultButton
.
clicked
.
connect
(
self
.
showResultSlot
)
self
.
showResultButton
.
clicked
.
connect
(
self
.
handleShowResultSignal
)
self
.
pb
=
QtWidgets
.
QLabel
(
# ProgressBar
self
.
pb_text
.
format
(
0
,
0
))
layout
.
addWidget
(
self
.
pb
,
2
,
0
,
1
,
4
)
layout
.
addWidget
(
self
.
pb
,
1
,
0
,
1
,
4
)
# time estimate label
self
.
timeEstLabel
=
QLabel
(
(
"Time Left: --"
))
layout
.
addWidget
(
self
.
timeEstLabel
,
3
,
0
,
1
,
4
)
self
.
setLayout
(
layout
)
self
.
setLayout
(
layout
)
...
@@ -138,7 +265,7 @@ class APP_Image2Doc(QtWidgets.QWidget):
...
@@ -138,7 +265,7 @@ class APP_Image2Doc(QtWidgets.QWidget):
'model.pdiparams.info'
,
'model.pdiparams.info'
,
'model.pdmodel'
'model.pdmodel'
]
]
model_path
=
os
.
path
.
join
(
here
,
'inference'
)
model_path
=
os
.
path
.
join
(
root
,
'inference'
)
os
.
makedirs
(
model_path
,
exist_ok
=
True
)
os
.
makedirs
(
model_path
,
exist_ok
=
True
)
# download and unzip models
# download and unzip models
...
@@ -184,51 +311,52 @@ class APP_Image2Doc(QtWidgets.QWidget):
...
@@ -184,51 +311,52 @@ class APP_Image2Doc(QtWidgets.QWidget):
args
.
ocr
=
True
args
.
ocr
=
True
args
.
recovery
=
True
args
.
recovery
=
True
args
.
save_pdf
=
self
.
save_pdf
args
.
save_pdf
=
self
.
save_pdf
args
.
table_char_dict_path
=
os
.
path
.
join
(
here
,
args
.
table_char_dict_path
=
os
.
path
.
join
(
root
,
"ppocr"
,
"utils"
,
"dict"
,
"table_structure_dict.txt"
)
"ppocr"
,
"utils"
,
"dict"
,
"table_structure_dict.txt"
)
if
lang
==
'EN'
:
if
lang
==
'EN'
:
args
.
det_model_dir
=
os
.
path
.
join
(
here
,
# 此处从这里找到模型存放位置
args
.
det_model_dir
=
os
.
path
.
join
(
root
,
# 此处从这里找到模型存放位置
"inference"
,
"en_PP-OCRv3_det_infer"
)
"inference"
,
"en_PP-OCRv3_det_infer"
)
args
.
rec_model_dir
=
os
.
path
.
join
(
here
,
args
.
rec_model_dir
=
os
.
path
.
join
(
root
,
"inference"
,
"en_PP-OCRv3_rec_infer"
)
"inference"
,
"en_PP-OCRv3_rec_infer"
)
args
.
table_model_dir
=
os
.
path
.
join
(
here
,
args
.
table_model_dir
=
os
.
path
.
join
(
root
,
"inference"
,
"en_ppstructure_mobile_v2.0_SLANet_infer"
)
"inference"
,
"en_ppstructure_mobile_v2.0_SLANet_infer"
)
args
.
output
=
os
.
path
.
join
(
here
,
"output"
)
# 结果保存路径
args
.
output
=
os
.
path
.
join
(
root
,
"output"
)
# 结果保存路径
args
.
layout_model_dir
=
os
.
path
.
join
(
here
,
args
.
layout_model_dir
=
os
.
path
.
join
(
root
,
"inference"
,
"picodet_lcnet_x1_0_fgd_layout_infer"
)
"inference"
,
"picodet_lcnet_x1_0_fgd_layout_infer"
)
lang_dict
=
DICT_EN
lang_dict
=
DICT_EN
elif
lang
==
'CN'
:
elif
lang
==
'CN'
:
args
.
det_model_dir
=
os
.
path
.
join
(
here
,
# 此处从这里找到模型存放位置
args
.
det_model_dir
=
os
.
path
.
join
(
root
,
# 此处从这里找到模型存放位置
"inference"
,
"cn_PP-OCRv3_det_infer"
)
"inference"
,
"cn_PP-OCRv3_det_infer"
)
args
.
rec_model_dir
=
os
.
path
.
join
(
here
,
args
.
rec_model_dir
=
os
.
path
.
join
(
root
,
"inference"
,
"cn_PP-OCRv3_rec_infer"
)
"inference"
,
"cn_PP-OCRv3_rec_infer"
)
args
.
table_model_dir
=
os
.
path
.
join
(
here
,
args
.
table_model_dir
=
os
.
path
.
join
(
root
,
"inference"
,
"cn_ppstructure_mobile_v2.0_SLANet_infer"
)
"inference"
,
"cn_ppstructure_mobile_v2.0_SLANet_infer"
)
args
.
output
=
os
.
path
.
join
(
here
,
"output"
)
# 结果保存路径
args
.
output
=
os
.
path
.
join
(
root
,
"output"
)
# 结果保存路径
args
.
layout_model_dir
=
os
.
path
.
join
(
here
,
args
.
layout_model_dir
=
os
.
path
.
join
(
root
,
"inference"
,
"picodet_lcnet_x1_0_fgd_layout_cdla_infer"
)
"inference"
,
"picodet_lcnet_x1_0_fgd_layout_cdla_infer"
)
lang_dict
=
DICT_CN
lang_dict
=
DICT_CN
else
:
else
:
raise
ValueError
(
"Unsupported language"
)
raise
ValueError
(
"Unsupported language"
)
args
.
rec_char_dict_path
=
os
.
path
.
join
(
here
,
args
.
rec_char_dict_path
=
os
.
path
.
join
(
root
,
"ppocr"
,
"utils"
,
"ppocr"
,
"utils"
,
lang_dict
[
'rec_char_dict_path'
])
lang_dict
[
'rec_char_dict_path'
])
args
.
layout_dict_path
=
os
.
path
.
join
(
here
,
args
.
layout_dict_path
=
os
.
path
.
join
(
root
,
"ppocr"
,
"utils"
,
"dict"
,
"layout_dict"
,
"ppocr"
,
"utils"
,
"dict"
,
"layout_dict"
,
lang_dict
[
'layout_dict_path'
])
lang_dict
[
'layout_dict_path'
])
# init predictor
# init predictor
return
StructureSystem
(
args
)
return
StructureSystem
(
args
)
def
openFileSlot
(
self
):
def
handleOpenFileSignal
(
self
):
'''
'''
可以多选图像文件
可以多选图像文件
'''
'''
selectedFiles
=
Q
tWidgets
.
Q
FileDialog
.
getOpenFileNames
(
self
,
selectedFiles
=
QFileDialog
.
getOpenFileNames
(
self
,
"多文件选择"
,
"/"
,
"图片文件 (*.png *.jpeg *.jpg *.bmp *.pdf)"
)[
0
]
"多文件选择"
,
"/"
,
"图片文件 (*.png *.jpeg *.jpg *.bmp *.pdf)"
)[
0
]
if
len
(
selectedFiles
)
>
0
:
if
len
(
selectedFiles
)
>
0
:
self
.
imagePaths
=
selectedFiles
self
.
imagePaths
=
selectedFiles
self
.
screenShot
=
None
# discard screenshot temp image
self
.
screenShot
=
None
# discard screenshot temp image
self
.
updateProgressBar
(
len
(
selectedFiles
),
0
)
self
.
pb
.
setRange
(
0
,
len
(
self
.
imagePaths
))
self
.
pb
.
setValue
(
0
)
def
screenShotSlot
(
self
):
def
screenShotSlot
(
self
):
'''
'''
...
@@ -239,109 +367,73 @@ class APP_Image2Doc(QtWidgets.QWidget):
...
@@ -239,109 +367,73 @@ class APP_Image2Doc(QtWidgets.QWidget):
if
self
.
screenShotWg
.
captureImage
:
if
self
.
screenShotWg
.
captureImage
:
self
.
screenShot
=
self
.
screenShotWg
.
captureImage
self
.
screenShot
=
self
.
screenShotWg
.
captureImage
self
.
imagePaths
.
clear
()
# discard openfile temp list
self
.
imagePaths
.
clear
()
# discard openfile temp list
self
.
updateProgressBar
(
1
,
0
)
self
.
pb
.
setRange
(
0
,
1
)
self
.
pb
.
setValue
(
0
)
def
startSlot
(
self
,
lang
):
def
handleStartSignal
(
self
,
lang
):
if
self
.
screenShot
:
# for screenShot
if
self
.
screenShot
:
# for screenShot
img_name
=
'screenshot_'
+
time
.
strftime
(
"%Y%m%d%H%M%S"
,
time
.
localtime
())
img_name
=
'screenshot_'
+
time
.
strftime
(
"%Y%m%d%H%M%S"
,
time
.
localtime
())
image
=
QImageToCvMat
(
self
.
screenShot
)
image
=
QImageToCvMat
(
self
.
screenShot
)
self
.
predictAndSave
(
image
,
img_name
,
lang
)
self
.
predictAndSave
(
image
,
img_name
,
lang
)
# update Progress Bar
# update Progress Bar
self
.
updateProgressBar
(
1
,
1
)
self
.
pb
.
setValue
(
1
)
Q
tWidgets
.
Q
MessageBox
.
information
(
self
,
QMessageBox
.
information
(
self
,
u
'Information'
,
"文档提取完成"
)
u
'Information'
,
"文档提取完成"
)
elif
len
(
self
.
imagePaths
)
>
0
:
# for image file selection
elif
len
(
self
.
imagePaths
)
>
0
:
# for image file selection
# Must set image path list and language before start
self
.
output_dir
=
os
.
path
.
join
(
self
.
output_dir
=
os
.
path
.
join
(
os
.
path
.
dirname
(
self
.
imagePaths
[
0
]),
"output"
)
# output_dir shold be same as imagepath
os
.
path
.
dirname
(
self
.
imagePaths
[
0
]),
"output"
)
# output_dir shold be same as imagepath
os
.
makedirs
(
self
.
output_dir
,
exist_ok
=
True
)
self
.
_thread
.
setOutputDir
(
self
.
output_dir
)
for
i
,
image_file
in
enumerate
(
self
.
imagePaths
):
self
.
_thread
.
setImagePath
(
self
.
imagePaths
)
if
os
.
path
.
basename
(
image_file
)[
-
3
:]
in
[
'pdf'
]:
self
.
_thread
.
setLang
(
lang
)
import
fitz
# disenble buttons
from
PIL
import
Image
self
.
openFileButton
.
setEnabled
(
False
)
imgs
=
[]
self
.
startCNButton
.
setEnabled
(
False
)
with
fitz
.
open
(
image_file
)
as
pdf
:
self
.
startENButton
.
setEnabled
(
False
)
for
pg
in
range
(
0
,
pdf
.
pageCount
):
# 启动工作进程
page
=
pdf
[
pg
]
self
.
_thread
.
start
()
mat
=
fitz
.
Matrix
(
2
,
2
)
self
.
time_start
=
time
.
time
()
# log start time
pm
=
page
.
getPixmap
(
matrix
=
mat
,
alpha
=
False
)
QMessageBox
.
information
(
self
,
u
'Information'
,
"开始转换"
)
# if width or height > 2000 pixels, don't enlarge the image
if
pm
.
width
>
2000
or
pm
.
height
>
2000
:
pm
=
page
.
getPixmap
(
matrix
=
fitz
.
Matrix
(
1
,
1
),
alpha
=
False
)
img
=
Image
.
frombytes
(
"RGB"
,
[
pm
.
width
,
pm
.
height
],
pm
.
samples
)
img
=
cv2
.
cvtColor
(
np
.
array
(
img
),
cv2
.
COLOR_RGB2BGR
)
imgs
.
append
(
img
)
else
:
img
=
cv2
.
imread
(
image_file
)
if
img
is
None
:
print
(
"error in loading image:{}"
.
format
(
image_file
))
continue
imgs
=
[
img
]
img_name
=
os
.
path
.
basename
(
image_file
).
split
(
'.'
)[
0
]
os
.
makedirs
(
os
.
path
.
join
(
self
.
output_dir
,
img_name
),
exist_ok
=
True
)
self
.
predictAndSave
(
imgs
,
img_name
,
lang
)
# update Progress Bar
self
.
updateProgressBar
(
len
(
self
.
imagePaths
),
i
+
1
)
QtWidgets
.
QMessageBox
.
information
(
self
,
u
'Information'
,
"文档提取完成"
)
else
:
else
:
print
(
'empty input'
)
QMessageBox
.
warning
(
self
,
u
'Information'
,
"请选择要识别的文件或截图"
)
def
predictAndSave
(
self
,
imgs
,
img_name
,
lang
):
all_res
=
[]
for
index
,
img
in
enumerate
(
imgs
):
if
lang
==
'EN'
:
res
,
time_dict
=
self
.
structure_sys_en
(
img
)
elif
lang
==
'CN'
:
res
,
time_dict
=
self
.
structure_sys_cn
(
img
)
# save output
save_structure_res
(
res
,
self
.
output_dir
,
img_name
)
draw_img
=
draw_structure_result
(
img
,
res
,
self
.
vis_font_path
)
img_save_path
=
os
.
path
.
join
(
self
.
output_dir
,
img_name
,
'show_{}.jpg'
.
format
(
index
))
if
res
!=
[]:
cv2
.
imwrite
(
img_save_path
,
draw_img
)
# recovery
h
,
w
,
_
=
img
.
shape
res
=
sorted_layout_boxes
(
res
,
w
)
all_res
+=
res
try
:
convert_info_docx
(
img
,
all_res
,
self
.
output_dir
,
img_name
,
self
.
save_pdf
)
except
Exception
as
ex
:
QtWidgets
.
QMessageBox
.
information
(
self
,
u
'Information'
,
"error in layout recovery image:{}, err msg: {}"
.
format
(
img_name
,
ex
))
print
(
'result save to {}'
.
format
(
self
.
output_dir
))
def
handleShowResultSignal
(
self
):
if
self
.
output_dir
is
None
:
def
showResultSlot
(
self
):
return
if
os
.
path
.
exists
(
self
.
output_dir
):
if
os
.
path
.
exists
(
self
.
output_dir
):
if
platform
.
system
()
==
'Windows'
:
if
platform
.
system
()
==
'Windows'
:
os
.
startfile
(
self
.
output_dir
)
os
.
startfile
(
self
.
output_dir
)
else
:
else
:
os
.
system
(
'open '
+
os
.
path
.
normpath
(
self
.
lastOpenD
ir
))
os
.
system
(
'open '
+
os
.
path
.
normpath
(
self
.
output_d
ir
))
else
:
else
:
Q
tWidgets
.
Q
MessageBox
.
information
(
self
,
QMessageBox
.
information
(
self
,
u
'Information'
,
"输出文件不存在"
)
u
'Information'
,
"输出文件不存在"
)
def
updateProgressBar
(
self
,
loaded
,
finished
):
def
handleProgressBarSingal
(
self
,
i
):
self
.
pb
.
setText
(
self
.
pb
.
setValue
(
i
)
self
.
pb_text
.
format
(
loaded
,
finished
))
# calculate time left of recognition
lenbar
=
self
.
pb
.
maximum
()
avg_time
=
(
time
.
time
()
-
self
.
time_start
)
/
i
# Use average time to prevent time fluctuations
time_left
=
str
(
datetime
.
timedelta
(
seconds
=
avg_time
*
(
lenbar
-
i
))).
split
(
"."
)[
0
]
# Remove microseconds
self
.
timeEstLabel
.
setText
(
f
"Time Left:
{
time_left
}
"
)
# show time left
def
handleEndsignalSignal
(
self
):
# enble buttons
self
.
openFileButton
.
setEnabled
(
True
)
self
.
startCNButton
.
setEnabled
(
True
)
self
.
startENButton
.
setEnabled
(
True
)
QMessageBox
.
information
(
self
,
u
'Information'
,
"转换结束"
)
def
main
():
def
main
():
app
=
Q
tWidgets
.
Q
Application
(
sys
.
argv
)
app
=
QApplication
(
sys
.
argv
)
window
=
APP_Image2Doc
()
# 创建对象
window
=
APP_Image2Doc
()
# 创建对象
window
.
show
()
# 全屏显示窗口
window
.
show
()
# 全屏显示窗口
Q
tWidgets
.
Q
Application
.
processEvents
()
QApplication
.
processEvents
()
sys
.
exit
(
app
.
exec
())
sys
.
exit
(
app
.
exec
())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录