网站建设支付安全,wd wordpress,棠下手机网站开发,专业的佛山网站建设一、前言
对很多人来说#xff0c;将PDF转换为可编辑的文本是个刚需#xff0c;却苦于没有简单的方法。发现 pdf 幻灯片#xff0c;效果还不错。
传统的讲座通常伴随有很多pdf幻灯片。一般来说#xff0c;想要对自己的讲座做笔记#xff0c;需要从pdf复制、补充大量内容…一、前言
对很多人来说将PDF转换为可编辑的文本是个刚需却苦于没有简单的方法。发现 pdf 幻灯片效果还不错。
传统的讲座通常伴随有很多pdf幻灯片。一般来说想要对自己的讲座做笔记需要从pdf复制、补充大量内容。
最近来自 K1 Digital 的高级机器工程师 Lucas Soares 一直在尝试通过使用 CR光学字符识别自动 pdf 幻灯片以便直接在 Markdown 文件中操作它们的内容从而避免手动复制和粘贴 pdf 内容实现这个过程的自动化。 图为项目作者卢卡斯·苏亚雷斯。
1.1、为什么不使用传统的pdf 转文本工具呢
Lucas Soares 发现传统工具往往会带来更多的问题需要花时间解决。他曾尝试使用传统的 Python 软件包但遇到了很多问题例如必须使用复杂的正则表达式模式解析最终输出等因此决定尝试使用目标检测和 OCR 来解决。
二、实现过程
基本过程可分为以下几个步骤 将 pdf 转换为图片 检测和识别图像中的文本 展示示例输出。
2.1、基于深度学习的 OCR 将 pdf 为文本
2.11、将 pdf 转换为图像
Soares 使用的 pdf 幻灯片来自于 David Silver 的增长学习参见以下 pdf 幻灯片地址。使用「pdf2image」包将每张幻灯片转换为 png 图像格式。 pdf 幻灯片示例。
地址https://www.davidsilver.uk/wp-content/uploads/2020/03/intro_RL.pdf
代码如下
from pdf2image import convert_from_path
from pdf2image.exceptions import (PDFInfoNotInstalledError,PDFPageCountError,PDFSyntaxError
)
pdf_path path/to/file/intro_RL_Lecture1.pdf
images convert_from_path(pdf_path)
for i, image in enumerate(images):fname image str(i) .pngimage.save(fname, PNG)
经过处理后所有的pdf幻灯片都转换成png格式的图片 2.12、检测和识别图像中的文本
为了检测和识别png图像中的文本Soares使用ocr.pytorch库中的文本检测器。按照说明下载模型保存模型保存在检查点文件夹中。
ocr.pytorch 库地址https://github.com/courao/ocr.pytorch
代码如下
# adapted from this source: https://github.com/courao/ocr.pytorch
%load_ext autoreload
%autoreload 2
import os
from ocr import ocr
import time
import shutil
import numpy as np
import pathlib
from PIL import Image
from glob import glob
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import pytesseractdef single_pic_proc(image_file):image np.array(Image.open(image_file).convert(RGB))result, image_framed ocr(image)return result,image_framedimage_files glob(./input_images/*.*)
result_dir ./output_images_with_boxes/# If the output folder exists we will remove it and redo it.
if os.path.exists(result_dir):shutil.rmtree(result_dir)
os.mkdir(result_dir)for image_file in sorted(image_files):result, image_framed single_pic_proc(image_file) # detecting and recognizing the textfilename pathlib.Path(image_file).nameoutput_file os.path.join(result_dir, image_file.split(/)[-1])txt_file os.path.join(result_dir, image_file.split(/)[-1].split(.)[0].txt)txt_f open(txt_file, w)Image.fromarray(image_framed).save(output_file)for key in result:txt_f.write(result[key][1]\n)txt_f.close() 设置输入和输出文件夹接着遍历所有输入图像转换后的pdf幻灯片然后通过single_pic_proc()函数运行OCR模块中的检测和识别模型最后将输出保存到输出文件夹。
从检测继承inherit了Pytorch CTPN识别了Pytorch CRNN模型都存在于OCR模块中。
2.13、示例输出
代码如下
import cv2 as cvoutput_dir pathlib.Path(./output_images_with_boxes)
# image cv.imread(str(np.random.choice(list(output_dir.iterdir()),1)[0]))
image cv.imread(f{output_dir}/image7.png)
size_reshaped (int(image.shape[1]),int(image.shape[0]))
image cv.resize(image, size_reshaped)
cv.imshow(image, image)
cv.waitKey(0)
cv.destroyAllWindows() 下图左为原始pdf 幻灯片图右为脑后的输出文本准确率非常高。 文本识别输出如下
filename f{output_dir}/image7.txt
with open(filename, r) as text:for line in text.readlines():print(line.strip(\n))
通过上述方法最终可以得到一个非常强大的工具来讨论文档从检测和识别手写笔记到检测和识别照片中的随机。
拥有文本的 OCR 工具来处理一些文本内容这比依赖外部软件来说明文档要好得多。