mirror of
https://github.com/PaddlePaddle/PaddleOCR.git
synced 2025-12-27 23:18:23 +00:00
cherry-pick for lazy import pymupdf and pre-commit (#11692)
Co-authored-by: jzhang533 <jzhang533@gmail.com>
This commit is contained in:
parent
efc01375c9
commit
b583b4773f
@ -1,10 +1,6 @@
|
||||
- repo: https://github.com/PaddlePaddle/mirrors-yapf.git
|
||||
sha: 0d79c0c469bab64f7229c9aca2b1186ef47f0e37
|
||||
hooks:
|
||||
- id: yapf
|
||||
files: \.py$
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
sha: a11d9314b22d8f8c7556443875b731ef05965464
|
||||
rev: a11d9314b22d8f8c7556443875b731ef05965464
|
||||
hooks:
|
||||
- id: check-merge-conflict
|
||||
- id: check-symlinks
|
||||
@ -15,7 +11,7 @@
|
||||
- id: trailing-whitespace
|
||||
files: \.md$
|
||||
- repo: https://github.com/Lucas-C/pre-commit-hooks
|
||||
sha: v1.0.1
|
||||
rev: v1.0.1
|
||||
hooks:
|
||||
- id: forbid-crlf
|
||||
files: \.md$
|
||||
@ -32,4 +28,10 @@
|
||||
description: Format files with ClangFormat
|
||||
entry: bash .clang_format.hook -i
|
||||
language: system
|
||||
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|cuh|proto)$
|
||||
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|cuh|proto)$
|
||||
# For Python files
|
||||
- repo: https://github.com/psf/black.git
|
||||
rev: 23.3.0
|
||||
hooks:
|
||||
- id: black
|
||||
files: (.*\.(py|pyi|bzl)|BUILD|.*\.BUILD|WORKSPACE)$
|
||||
|
||||
@ -41,18 +41,26 @@ def print_dict(d, logger, delimiter=0):
|
||||
|
||||
|
||||
def get_check_global_params(mode):
|
||||
check_params = ['use_gpu', 'max_text_length', 'image_shape', \
|
||||
'image_shape', 'character_type', 'loss_type']
|
||||
check_params = [
|
||||
"use_gpu",
|
||||
"max_text_length",
|
||||
"image_shape",
|
||||
"image_shape",
|
||||
"character_type",
|
||||
"loss_type",
|
||||
]
|
||||
if mode == "train_eval":
|
||||
check_params = check_params + [ \
|
||||
'train_batch_size_per_card', 'test_batch_size_per_card']
|
||||
check_params = check_params + [
|
||||
"train_batch_size_per_card",
|
||||
"test_batch_size_per_card",
|
||||
]
|
||||
elif mode == "test":
|
||||
check_params = check_params + ['test_batch_size_per_card']
|
||||
check_params = check_params + ["test_batch_size_per_card"]
|
||||
return check_params
|
||||
|
||||
|
||||
def _check_image_file(path):
|
||||
img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'pdf'}
|
||||
img_end = {"jpg", "bmp", "png", "jpeg", "rgb", "tif", "tiff", "gif", "pdf"}
|
||||
return any([path.lower().endswith(e) for e in img_end])
|
||||
|
||||
|
||||
@ -74,14 +82,16 @@ def get_image_file_list(img_file, infer_list=None):
|
||||
imgs_lists = sorted(imgs_lists)
|
||||
return imgs_lists
|
||||
|
||||
|
||||
def binarize_img(img):
|
||||
if len(img.shape) == 3 and img.shape[2] == 3:
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # conversion to grayscale image
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # conversion to grayscale image
|
||||
# use cv2 threshold binarization
|
||||
_, gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
||||
img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
|
||||
return img
|
||||
|
||||
|
||||
def alpha_to_color(img, alpha_color=(255, 255, 255)):
|
||||
if len(img.shape) == 3 and img.shape[2] == 4:
|
||||
B, G, R, A = cv2.split(img)
|
||||
@ -94,21 +104,25 @@ def alpha_to_color(img, alpha_color=(255, 255, 255)):
|
||||
img = cv2.merge((B, G, R))
|
||||
return img
|
||||
|
||||
|
||||
def check_and_read(img_path):
|
||||
if os.path.basename(img_path)[-3:].lower() == 'gif':
|
||||
if os.path.basename(img_path)[-3:].lower() == "gif":
|
||||
gif = cv2.VideoCapture(img_path)
|
||||
ret, frame = gif.read()
|
||||
if not ret:
|
||||
logger = logging.getLogger('ppocr')
|
||||
logger = logging.getLogger("ppocr")
|
||||
logger.info("Cannot read {}. This gif image maybe corrupted.")
|
||||
return None, False
|
||||
if len(frame.shape) == 2 or frame.shape[-1] == 1:
|
||||
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
|
||||
imgvalue = frame[:, :, ::-1]
|
||||
return imgvalue, True, False
|
||||
elif os.path.basename(img_path)[-3:].lower() == 'pdf':
|
||||
import fitz
|
||||
elif os.path.basename(img_path)[-3:].lower() == "pdf":
|
||||
from paddle.utils import try_import
|
||||
|
||||
try_import("fitz")
|
||||
from PIL import Image
|
||||
|
||||
imgs = []
|
||||
with fitz.open(img_path) as pdf:
|
||||
for pg in range(0, pdf.page_count):
|
||||
@ -128,7 +142,7 @@ def check_and_read(img_path):
|
||||
|
||||
|
||||
def load_vqa_bio_label_maps(label_map_path):
|
||||
with open(label_map_path, "r", encoding='utf-8') as fin:
|
||||
with open(label_map_path, "r", encoding="utf-8") as fin:
|
||||
lines = fin.readlines()
|
||||
old_lines = [line.strip() for line in lines]
|
||||
lines = ["O"]
|
||||
@ -155,19 +169,19 @@ def set_seed(seed=1024):
|
||||
def check_install(module_name, install_name):
|
||||
spec = importlib.util.find_spec(module_name)
|
||||
if spec is None:
|
||||
print(f'Warnning! The {module_name} module is NOT installed')
|
||||
print(f"Warnning! The {module_name} module is NOT installed")
|
||||
print(
|
||||
f'Try install {module_name} module automatically. You can also try to install manually by pip install {install_name}.'
|
||||
f"Try install {module_name} module automatically. You can also try to install manually by pip install {install_name}."
|
||||
)
|
||||
python = sys.executable
|
||||
try:
|
||||
subprocess.check_call(
|
||||
[python, '-m', 'pip', 'install', install_name],
|
||||
stdout=subprocess.DEVNULL)
|
||||
print(f'The {module_name} module is now installed')
|
||||
[python, "-m", "pip", "install", install_name],
|
||||
stdout=subprocess.DEVNULL,
|
||||
)
|
||||
print(f"The {module_name} module is now installed")
|
||||
except subprocess.CalledProcessError as exc:
|
||||
raise Exception(
|
||||
f"Install {module_name} failed, please install manually")
|
||||
raise Exception(f"Install {module_name} failed, please install manually")
|
||||
else:
|
||||
print(f"{module_name} has been installed.")
|
||||
|
||||
|
||||
@ -22,15 +22,27 @@ import cv2
|
||||
import platform
|
||||
import numpy as np
|
||||
import fitz
|
||||
from paddle.utils import try_import
|
||||
|
||||
try_import("fitz")
|
||||
from PIL import Image
|
||||
from pdf2docx.converter import Converter
|
||||
from qtpy.QtWidgets import QApplication, QWidget, QPushButton, QProgressBar, \
|
||||
QGridLayout, QMessageBox, QLabel, QFileDialog, QCheckBox
|
||||
from qtpy.QtWidgets import (
|
||||
QApplication,
|
||||
QWidget,
|
||||
QPushButton,
|
||||
QProgressBar,
|
||||
QGridLayout,
|
||||
QMessageBox,
|
||||
QLabel,
|
||||
QFileDialog,
|
||||
QCheckBox,
|
||||
)
|
||||
from qtpy.QtCore import Signal, QThread, QObject
|
||||
from qtpy.QtGui import QImage, QPixmap, QIcon
|
||||
|
||||
file = os.path.dirname(os.path.abspath(__file__))
|
||||
root = os.path.abspath(os.path.join(file, '../../'))
|
||||
root = os.path.abspath(os.path.join(file, "../../"))
|
||||
sys.path.append(file)
|
||||
sys.path.insert(0, root)
|
||||
|
||||
@ -38,6 +50,7 @@ from ppstructure.predict_system import StructureSystem, save_structure_res
|
||||
from ppstructure.utility import parse_args, draw_structure_result
|
||||
from ppocr.utils.network import download_with_progressbar
|
||||
from ppstructure.recovery.recovery_to_doc import sorted_layout_boxes, convert_info_docx
|
||||
|
||||
# from ScreenShotWidget import ScreenShotWidget
|
||||
|
||||
__APPNAME__ = "pdf2word"
|
||||
@ -45,17 +58,13 @@ __VERSION__ = "0.2.2"
|
||||
|
||||
URLs_EN = {
|
||||
# 下载超英文轻量级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",
|
||||
# 下载英文轻量级PP-OCRv3模型的识别模型并解压
|
||||
"en_PP-OCRv3_rec_infer":
|
||||
"https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar",
|
||||
"en_PP-OCRv3_rec_infer": "https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar",
|
||||
# 下载超轻量级英文表格英文模型并解压
|
||||
"en_ppstructure_mobile_v2.0_SLANet_infer":
|
||||
"https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/en_ppstructure_mobile_v2.0_SLANet_infer.tar",
|
||||
"en_ppstructure_mobile_v2.0_SLANet_infer": "https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/en_ppstructure_mobile_v2.0_SLANet_infer.tar",
|
||||
# 英文版面分析模型
|
||||
"picodet_lcnet_x1_0_fgd_layout_infer":
|
||||
"https://paddleocr.bj.bcebos.com/ppstructure/models/layout/picodet_lcnet_x1_0_fgd_layout_infer.tar",
|
||||
"picodet_lcnet_x1_0_fgd_layout_infer": "https://paddleocr.bj.bcebos.com/ppstructure/models/layout/picodet_lcnet_x1_0_fgd_layout_infer.tar",
|
||||
}
|
||||
DICT_EN = {
|
||||
"rec_char_dict_path": "en_dict.txt",
|
||||
@ -64,17 +73,13 @@ DICT_EN = {
|
||||
|
||||
URLs_CN = {
|
||||
# 下载超中文轻量级PP-OCRv3模型的检测模型并解压
|
||||
"cn_PP-OCRv3_det_infer":
|
||||
"https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar",
|
||||
"cn_PP-OCRv3_det_infer": "https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar",
|
||||
# 下载中文轻量级PP-OCRv3模型的识别模型并解压
|
||||
"cn_PP-OCRv3_rec_infer":
|
||||
"https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar",
|
||||
"cn_PP-OCRv3_rec_infer": "https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar",
|
||||
# 下载超轻量级英文表格英文模型并解压
|
||||
"cn_ppstructure_mobile_v2.0_SLANet_infer":
|
||||
"https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/en_ppstructure_mobile_v2.0_SLANet_infer.tar",
|
||||
"cn_ppstructure_mobile_v2.0_SLANet_infer": "https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/en_ppstructure_mobile_v2.0_SLANet_infer.tar",
|
||||
# 中文版面分析模型
|
||||
"picodet_lcnet_x1_0_fgd_layout_cdla_infer":
|
||||
"https://paddleocr.bj.bcebos.com/ppstructure/models/layout/picodet_lcnet_x1_0_fgd_layout_cdla_infer.tar",
|
||||
"picodet_lcnet_x1_0_fgd_layout_cdla_infer": "https://paddleocr.bj.bcebos.com/ppstructure/models/layout/picodet_lcnet_x1_0_fgd_layout_cdla_infer.tar",
|
||||
}
|
||||
DICT_CN = {
|
||||
"rec_char_dict_path": "ppocr_keys_v1.txt",
|
||||
@ -83,9 +88,9 @@ DICT_CN = {
|
||||
|
||||
|
||||
def QImageToCvMat(incomingImage) -> np.array:
|
||||
'''
|
||||
Converts a QImage into an opencv MAT format
|
||||
'''
|
||||
"""
|
||||
Converts a QImage into an opencv MAT format
|
||||
"""
|
||||
|
||||
incomingImage = incomingImage.convertToFormat(QImage.Format.Format_RGBA8888)
|
||||
|
||||
@ -99,7 +104,7 @@ def QImageToCvMat(incomingImage) -> np.array:
|
||||
|
||||
|
||||
def readImage(image_file) -> list:
|
||||
if os.path.basename(image_file)[-3:] == 'pdf':
|
||||
if os.path.basename(image_file)[-3:] == "pdf":
|
||||
imgs = []
|
||||
with fitz.open(image_file) as pdf:
|
||||
for pg in range(0, pdf.pageCount):
|
||||
@ -126,7 +131,7 @@ class Worker(QThread):
|
||||
progressBarValue = Signal(int)
|
||||
progressBarRange = Signal(int)
|
||||
endsignal = Signal()
|
||||
exceptedsignal = Signal(str) #发送一个异常信号
|
||||
exceptedsignal = Signal(str) # 发送一个异常信号
|
||||
loopFlag = True
|
||||
|
||||
def __init__(self, predictors, save_pdf, vis_font_path, use_pdf2docx_api):
|
||||
@ -134,7 +139,7 @@ class Worker(QThread):
|
||||
self.predictors = predictors
|
||||
self.save_pdf = save_pdf
|
||||
self.vis_font_path = vis_font_path
|
||||
self.lang = 'EN'
|
||||
self.lang = "EN"
|
||||
self.imagePaths = []
|
||||
self.use_pdf2docx_api = use_pdf2docx_api
|
||||
self.outputDir = None
|
||||
@ -187,10 +192,13 @@ class Worker(QThread):
|
||||
try:
|
||||
convert_info_docx(imgs, all_res, self.outputDir, img_name)
|
||||
except Exception as ex:
|
||||
print("error in layout recovery image:{}, err msg: {}".format(
|
||||
img_name, ex))
|
||||
print("Predict time : {:.3f}s".format(time_dict['all']))
|
||||
print('result save to {}'.format(self.outputDir))
|
||||
print(
|
||||
"error in layout recovery image:{}, err msg: {}".format(
|
||||
img_name, ex
|
||||
)
|
||||
)
|
||||
print("Predict time : {:.3f}s".format(time_dict["all"]))
|
||||
print("result save to {}".format(self.outputDir))
|
||||
|
||||
def run(self):
|
||||
self.resetPageCnt()
|
||||
@ -201,19 +209,16 @@ class Worker(QThread):
|
||||
if not self.loopFlag:
|
||||
break
|
||||
# using use_pdf2docx_api for PDF parsing
|
||||
if self.use_pdf2docx_api \
|
||||
and os.path.basename(image_file)[-3:] == 'pdf':
|
||||
if self.use_pdf2docx_api and os.path.basename(image_file)[-3:] == "pdf":
|
||||
self.totalPageCnt += 1
|
||||
self.progressBarRange.emit(self.totalPageCnt)
|
||||
print(
|
||||
'===============using use_pdf2docx_api===============')
|
||||
img_name = os.path.basename(image_file).split('.')[0]
|
||||
docx_file = os.path.join(self.outputDir,
|
||||
'{}.docx'.format(img_name))
|
||||
print("===============using use_pdf2docx_api===============")
|
||||
img_name = os.path.basename(image_file).split(".")[0]
|
||||
docx_file = os.path.join(self.outputDir, "{}.docx".format(img_name))
|
||||
cv = Converter(image_file)
|
||||
cv.convert(docx_file)
|
||||
cv.close()
|
||||
print('docx save to {}'.format(docx_file))
|
||||
print("docx save to {}".format(docx_file))
|
||||
self.pageCnt += 1
|
||||
self.progressBarValue.emit(self.pageCnt)
|
||||
else:
|
||||
@ -221,9 +226,8 @@ class Worker(QThread):
|
||||
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)
|
||||
img_name = os.path.basename(image_file).split(".")[0]
|
||||
os.makedirs(os.path.join(self.outputDir, img_name), exist_ok=True)
|
||||
self.ppocrPrecitor(imgs, img_name)
|
||||
# file processed
|
||||
self.endsignal.emit()
|
||||
@ -261,15 +265,15 @@ class APP_Image2Doc(QWidget):
|
||||
|
||||
# 初始化模型
|
||||
predictors = {
|
||||
'EN': self.initPredictor('EN'),
|
||||
'CN': self.initPredictor('CN'),
|
||||
"EN": self.initPredictor("EN"),
|
||||
"CN": self.initPredictor("CN"),
|
||||
}
|
||||
|
||||
# 设置工作进程
|
||||
self._thread = Worker(predictors, self.save_pdf, self.vis_font_path,
|
||||
self.use_pdf2docx_api)
|
||||
self._thread.progressBarValue.connect(
|
||||
self.handleProgressBarUpdateSingal)
|
||||
self._thread = Worker(
|
||||
predictors, self.save_pdf, self.vis_font_path, self.use_pdf2docx_api
|
||||
)
|
||||
self._thread.progressBarValue.connect(self.handleProgressBarUpdateSingal)
|
||||
self._thread.endsignal.connect(self.handleEndsignalSignal)
|
||||
# self._thread.finished.connect(QObject.deleteLater)
|
||||
self._thread.progressBarRange.connect(self.handleProgressBarRangeSingal)
|
||||
@ -296,18 +300,21 @@ class APP_Image2Doc(QWidget):
|
||||
self.startCNButton.setIcon(QIcon(QPixmap("./icons/chinese.png")))
|
||||
layout.addWidget(self.startCNButton, 0, 1, 1, 1)
|
||||
self.startCNButton.clicked.connect(
|
||||
functools.partial(self.handleStartSignal, 'CN', False))
|
||||
functools.partial(self.handleStartSignal, "CN", False)
|
||||
)
|
||||
|
||||
self.startENButton = QPushButton("英文转换")
|
||||
self.startENButton.setIcon(QIcon(QPixmap("./icons/english.png")))
|
||||
layout.addWidget(self.startENButton, 0, 2, 1, 1)
|
||||
self.startENButton.clicked.connect(
|
||||
functools.partial(self.handleStartSignal, 'EN', False))
|
||||
functools.partial(self.handleStartSignal, "EN", False)
|
||||
)
|
||||
|
||||
self.PDFParserButton = QPushButton('PDF解析', self)
|
||||
self.PDFParserButton = QPushButton("PDF解析", self)
|
||||
layout.addWidget(self.PDFParserButton, 0, 3, 1, 1)
|
||||
self.PDFParserButton.clicked.connect(
|
||||
functools.partial(self.handleStartSignal, 'CN', True))
|
||||
functools.partial(self.handleStartSignal, "CN", True)
|
||||
)
|
||||
|
||||
self.showResultButton = QPushButton("显示结果")
|
||||
self.showResultButton.setIcon(QIcon(QPixmap("./icons/folder-open.png")))
|
||||
@ -325,18 +332,21 @@ class APP_Image2Doc(QWidget):
|
||||
def downloadModels(self, URLs):
|
||||
# using custom model
|
||||
tar_file_name_list = [
|
||||
'inference.pdiparams', 'inference.pdiparams.info',
|
||||
'inference.pdmodel', 'model.pdiparams', 'model.pdiparams.info',
|
||||
'model.pdmodel'
|
||||
"inference.pdiparams",
|
||||
"inference.pdiparams.info",
|
||||
"inference.pdmodel",
|
||||
"model.pdiparams",
|
||||
"model.pdiparams.info",
|
||||
"model.pdmodel",
|
||||
]
|
||||
model_path = os.path.join(root, 'inference')
|
||||
model_path = os.path.join(root, "inference")
|
||||
os.makedirs(model_path, exist_ok=True)
|
||||
|
||||
# download and unzip models
|
||||
for name in URLs.keys():
|
||||
url = URLs[name]
|
||||
print("Try downloading file: {}".format(url))
|
||||
tarname = url.split('/')[-1]
|
||||
tarname = url.split("/")[-1]
|
||||
tarpath = os.path.join(model_path, tarname)
|
||||
if os.path.exists(tarpath):
|
||||
print("File have already exist. skip")
|
||||
@ -344,13 +354,12 @@ class APP_Image2Doc(QWidget):
|
||||
try:
|
||||
download_with_progressbar(url, tarpath)
|
||||
except Exception as e:
|
||||
print(
|
||||
"Error occurred when downloading file, error message:")
|
||||
print("Error occurred when downloading file, error message:")
|
||||
print(e)
|
||||
|
||||
# unzip model tar
|
||||
try:
|
||||
with tarfile.open(tarpath, 'r') as tarObj:
|
||||
with tarfile.open(tarpath, "r") as tarObj:
|
||||
storage_dir = os.path.join(model_path, name)
|
||||
os.makedirs(storage_dir, exist_ok=True)
|
||||
for member in tarObj.getmembers():
|
||||
@ -361,64 +370,70 @@ class APP_Image2Doc(QWidget):
|
||||
if filename is None:
|
||||
continue
|
||||
file = tarObj.extractfile(member)
|
||||
with open(os.path.join(storage_dir, filename),
|
||||
'wb') as f:
|
||||
with open(os.path.join(storage_dir, filename), "wb") as f:
|
||||
f.write(file.read())
|
||||
except Exception as e:
|
||||
print("Error occurred when unziping file, error message:")
|
||||
print(e)
|
||||
|
||||
def initPredictor(self, lang='EN'):
|
||||
def initPredictor(self, lang="EN"):
|
||||
# init predictor args
|
||||
args = parse_args()
|
||||
args.table_max_len = 488
|
||||
args.ocr = True
|
||||
args.recovery = True
|
||||
args.save_pdf = self.save_pdf
|
||||
args.table_char_dict_path = os.path.join(root, "ppocr", "utils", "dict",
|
||||
"table_structure_dict.txt")
|
||||
if lang == 'EN':
|
||||
args.table_char_dict_path = os.path.join(
|
||||
root, "ppocr", "utils", "dict", "table_structure_dict.txt"
|
||||
)
|
||||
if lang == "EN":
|
||||
args.det_model_dir = os.path.join(
|
||||
root, # 此处从这里找到模型存放位置
|
||||
"inference",
|
||||
"en_PP-OCRv3_det_infer")
|
||||
args.rec_model_dir = os.path.join(root, "inference",
|
||||
"en_PP-OCRv3_rec_infer")
|
||||
root, "inference", "en_PP-OCRv3_det_infer" # 此处从这里找到模型存放位置
|
||||
)
|
||||
args.rec_model_dir = os.path.join(
|
||||
root, "inference", "en_PP-OCRv3_rec_infer"
|
||||
)
|
||||
args.table_model_dir = os.path.join(
|
||||
root, "inference", "en_ppstructure_mobile_v2.0_SLANet_infer")
|
||||
root, "inference", "en_ppstructure_mobile_v2.0_SLANet_infer"
|
||||
)
|
||||
args.output = os.path.join(root, "output") # 结果保存路径
|
||||
args.layout_model_dir = os.path.join(
|
||||
root, "inference", "picodet_lcnet_x1_0_fgd_layout_infer")
|
||||
root, "inference", "picodet_lcnet_x1_0_fgd_layout_infer"
|
||||
)
|
||||
lang_dict = DICT_EN
|
||||
elif lang == 'CN':
|
||||
elif lang == "CN":
|
||||
args.det_model_dir = os.path.join(
|
||||
root, # 此处从这里找到模型存放位置
|
||||
"inference",
|
||||
"cn_PP-OCRv3_det_infer")
|
||||
args.rec_model_dir = os.path.join(root, "inference",
|
||||
"cn_PP-OCRv3_rec_infer")
|
||||
root, "inference", "cn_PP-OCRv3_det_infer" # 此处从这里找到模型存放位置
|
||||
)
|
||||
args.rec_model_dir = os.path.join(
|
||||
root, "inference", "cn_PP-OCRv3_rec_infer"
|
||||
)
|
||||
args.table_model_dir = os.path.join(
|
||||
root, "inference", "cn_ppstructure_mobile_v2.0_SLANet_infer")
|
||||
root, "inference", "cn_ppstructure_mobile_v2.0_SLANet_infer"
|
||||
)
|
||||
args.output = os.path.join(root, "output") # 结果保存路径
|
||||
args.layout_model_dir = os.path.join(
|
||||
root, "inference", "picodet_lcnet_x1_0_fgd_layout_cdla_infer")
|
||||
root, "inference", "picodet_lcnet_x1_0_fgd_layout_cdla_infer"
|
||||
)
|
||||
lang_dict = DICT_CN
|
||||
else:
|
||||
raise ValueError("Unsupported language")
|
||||
args.rec_char_dict_path = os.path.join(root, "ppocr", "utils",
|
||||
lang_dict['rec_char_dict_path'])
|
||||
args.layout_dict_path = os.path.join(root, "ppocr", "utils", "dict",
|
||||
"layout_dict",
|
||||
lang_dict['layout_dict_path'])
|
||||
args.rec_char_dict_path = os.path.join(
|
||||
root, "ppocr", "utils", lang_dict["rec_char_dict_path"]
|
||||
)
|
||||
args.layout_dict_path = os.path.join(
|
||||
root, "ppocr", "utils", "dict", "layout_dict", lang_dict["layout_dict_path"]
|
||||
)
|
||||
# init predictor
|
||||
return StructureSystem(args)
|
||||
|
||||
def handleOpenFileSignal(self):
|
||||
'''
|
||||
"""
|
||||
可以多选图像文件
|
||||
'''
|
||||
"""
|
||||
selectedFiles = QFileDialog.getOpenFileNames(
|
||||
self, "多文件选择", "/", "图片文件 (*.png *.jpeg *.jpg *.bmp *.pdf)")[0]
|
||||
self, "多文件选择", "/", "图片文件 (*.png *.jpeg *.jpg *.bmp *.pdf)"
|
||||
)[0]
|
||||
if len(selectedFiles) > 0:
|
||||
self.imagePaths = selectedFiles
|
||||
self.screenShot = None # discard screenshot temp image
|
||||
@ -436,20 +451,19 @@ class APP_Image2Doc(QWidget):
|
||||
# self.pb.setRange(0, 1)
|
||||
# self.pb.setValue(0)
|
||||
|
||||
def handleStartSignal(self, lang='EN', pdfParser=False):
|
||||
def handleStartSignal(self, lang="EN", pdfParser=False):
|
||||
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)
|
||||
self.predictAndSave(image, img_name, lang)
|
||||
# update Progress Bar
|
||||
self.pb.setValue(1)
|
||||
QMessageBox.information(self, u'Information', "文档提取完成")
|
||||
QMessageBox.information(self, "Information", "文档提取完成")
|
||||
elif len(self.imagePaths) > 0: # for image file selection
|
||||
# Must set image path list and language before start
|
||||
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
|
||||
self._thread.setOutputDir(self.output_dir)
|
||||
self._thread.setImagePath(self.imagePaths)
|
||||
self._thread.setLang(lang)
|
||||
@ -462,29 +476,31 @@ class APP_Image2Doc(QWidget):
|
||||
# 启动工作进程
|
||||
self._thread.start()
|
||||
self.time_start = time.time() # log start time
|
||||
QMessageBox.information(self, u'Information', "开始转换")
|
||||
QMessageBox.information(self, "Information", "开始转换")
|
||||
else:
|
||||
QMessageBox.warning(self, u'Information', "请选择要识别的文件或截图")
|
||||
QMessageBox.warning(self, "Information", "请选择要识别的文件或截图")
|
||||
|
||||
def handleShowResultSignal(self):
|
||||
if self.output_dir is None:
|
||||
return
|
||||
if os.path.exists(self.output_dir):
|
||||
if platform.system() == 'Windows':
|
||||
if platform.system() == "Windows":
|
||||
os.startfile(self.output_dir)
|
||||
else:
|
||||
os.system('open ' + os.path.normpath(self.output_dir))
|
||||
os.system("open " + os.path.normpath(self.output_dir))
|
||||
else:
|
||||
QMessageBox.information(self, u'Information', "输出文件不存在")
|
||||
QMessageBox.information(self, "Information", "输出文件不存在")
|
||||
|
||||
def handleProgressBarUpdateSingal(self, i):
|
||||
self.pb.setValue(i)
|
||||
# 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
|
||||
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 handleProgressBarRangeSingal(self, max):
|
||||
@ -496,14 +512,14 @@ class APP_Image2Doc(QWidget):
|
||||
self.startCNButton.setEnabled(True)
|
||||
self.startENButton.setEnabled(True)
|
||||
self.PDFParserButton.setEnabled(True)
|
||||
QMessageBox.information(self, u'Information', "转换结束")
|
||||
QMessageBox.information(self, "Information", "转换结束")
|
||||
|
||||
def handleCBChangeSignal(self):
|
||||
self._thread.setPDFParser(self.checkBox.isChecked())
|
||||
|
||||
def handleThreadException(self, message):
|
||||
self._thread.quit()
|
||||
QMessageBox.information(self, 'Error', message)
|
||||
QMessageBox.information(self, "Error", message)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
@ -14,5 +14,5 @@ lxml
|
||||
premailer
|
||||
openpyxl
|
||||
attrdict
|
||||
PyMuPDF<1.21.0
|
||||
Pillow
|
||||
Pillow>=10.0.0
|
||||
pyyaml
|
||||
Loading…
x
Reference in New Issue
Block a user