您现在的位置是:热点 >>正文
告别「复制+粘贴」,基于深度学习的OCR,实现PDF转文本
热点5061845人已围观
简介...
传统的讲座通常伴随着一组 pdf 幻灯片。一般来说,想要对此类讲座做笔记,需要从 pdf 复制、粘贴很多内容。
最近,来自 K1 Digital 的高级机器学习工程师 Lucas Soares 一直在尝试通过使用 OCR(光学字符识别)自动转录 pdf 幻灯片,以便直接在 markdown 文件中操作它们的内容,从而避免手动复制和粘贴 pdf 内容,实现这一过程的自动化。
左为项目作者 Lucas Soares。
项目地址:https://github.com/EnkrateiaLucca/ocr_for_transcribing_pdf_slides
为什么不使用传统的 pdf 转文本工具呢?
Lucas Soares 发现传统工具往往会带来更多的问题,需要告别花时间解决。他曾经尝试使用传统的 Python 软件包,但是遇到了很多问题(例如必须使用复杂的正则表达式模式解析最终输出等),因此决定尝试使用目标检测和 OCR 来解决。
基本过程可分为以下步骤:
将 pdf 转换为图片; 检测和识别图像中的文本; 展示示例输出。
基于深度学习的 OCR 将 pdf 转录为文本
将 pdf 转换为图像
Soares 使用的 pdf 幻灯片来自于 David Silver 的增强学习(参见以下 pdf 幻灯片地址)。使用「pdf2image」包将每张幻灯片转换为 png 图像格式。
pdf 幻灯片示例。
地址:https://www.davidsilver.uk/wp-content/uploads/2020/03/intro_R「复L.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) + ".png" image.save(fname, "PNG")
经过处理后,所有的 pdf 幻灯片都转换成 png 格式的图像:
检测和识别图像中的文本
为了检测和识别 png 图像中的文本粘贴,Soares 使用 ocr.pytorch 库中的文本检测器。按照说明下载模型并将模型保存在 checkpoints 文件夹中。
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&nb」,sp;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&nbs基于p;sns.set() import pytesseract def single_pic_proc(image_file): image = np.array(Image.open(image_file).convert('RGB')) result, image_framed = ocr(image) return resul深度t,image_framed image_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):&学习nbsp; 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 text的O filename = pathlib.Path(image_file).name output_file = os.path.join(result_dir, image_file.split('/')[-1]) txt_file = os.path.join(result_dir, image_file.split('/')[-1].split('.')[CR0]+'.txt') txt_f = open(txt_file, 'w') Image.fromarray(image_framed).save(output_file) for key in result: txt_f.write(result[key][1]+' ')&,实nbsp; txt_f.close()
设置输入和输出文件夹,接着遍历所有输入图像(转换后的 pdf 幻灯片),然后通过 single_pic_proc() 函数运行 OCR 模块中的检测和识别模型,最后将输出保存到输出文件夹。
其中检测继承(inherit)了 Pytorch CTPN 模型,识别继承了 Pytorch CRNN 模型,两者都存在于 OCR 模块中。
示例输出
代码如下:
import cv2 as cv output_dir = pathlib.Path("./output_image现Ps_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_resDFhaped) 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&nb转文sp;text.readlines(): print(line.strip(" "))
通过上述方法,最终你可以得到一个非常强大的工具来转录各种文档,从检测和识别手写笔记到检测和识别照片中的随机文本。拥有自己的 OCR 工具来处理一些文本内容,这比依赖外部软件来转录文档要好的多。
Tags:
相关文章
吃夏威夷果有哪些好处
热点夏威夷果的功效与作用夏威夷果可以健脑益智,调节血脂血糖,缓解便秘。人类的脑细胞由60%的不饱和脂肪酸以及35%的蛋白质构成,夏威夷果含有大量的不饱和脂肪酸,并含有15%-。夏威夷果的功效到底有哪些?夏...
【热点】
阅读更多标签: Supercell Royal Match Monopoly Go 移动游戏
热点云梦级2月11日云梦级消息,近日,Supercell CEO Ilkka Paananen发布2025年度公开信,其表示,公司研究发现,全球2020年以来发布的移动游戏有约5.3万款,其中只有22款(...
【热点】
阅读更多可灵AI推出最新视频生成模型可灵2.5Turbo 并宣布较之前版本降价近30%
热点近日可灵 AI 正式推出了其最新的视频生成模型 —— 可灵2.5Turbo,并宣布较之前版本降价近30%。此次降价策略成为可灵 AI 的一大卖点,显现出厂商在视频大模型订阅价格上的分化。 作为当前算力...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.military-history.org/?s=谷歌快排系统官网【tg:seogoogle987】,Oum
- https://www.springfieldlibrary.org/library/?s=谷歌快排系统官网【tg:seogoogle987】,f7w
- https://www.mdbg.net/chinese/dictionary?page=worddict&wdrst=0&wdqb=谷歌快排系统官网【tg:seogoogle987】,lSL
- https://www.teamlab.art/zh-hans/?submit=谷歌快排系统官网【tg:seogoogle987】,zPs
- https://m.shoppinghow.kakao.com/m/search/q/谷歌快排系统官网【tg:seogoogle987】iRi
- https://www.seforall.org/search?keywords=谷歌快排系统官网【tg:seogoogle987】,6aC
- https://www.lneg.pt/?cat=140&s=谷歌快排系统官网【tg:seogoogle987】,NZ5
- https://www.livehindustan.com/tags/谷歌快排系统官网【tg:seogoogle987】,G3A
- https://www.synonyms.com/antonyms/谷歌快排系统官网【tg:seogoogle987】,grj
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=谷歌快排系统官网【tg:seogoogle987】,p4l
- https://www.reallywildbirdfood.co.uk/store/search?q=谷歌快排系统官网【tg:seogoogle987】,piq
- https://www.sebson.de/en/search?query=谷歌快排系统官网【tg:seogoogle987】,MD2
- https://www.theaerogroup.net/?cat=28&s=谷歌快排系统官网【tg:seogoogle987】,cVO
- https://www.abbreviations.com/谷歌快排系统官网【tg:seogoogle987】yqH
- https://xn--lgernevedlystskoven-lxb.dk/search/谷歌快排系统官网【tg:seogoogle987】,BSn
- https://www.sweat.com/collections/vendors?q=谷歌快排系统官网【tg:seogoogle987】,Exy
- https://www.lee.com/search?q=谷歌快排系统官网【tg:seogoogle987】,UZ6
- https://www.skicheck.at/en/?action=CHECK&s=谷歌快排系统官网【tg:seogoogle987】,Gyl
- https://www.merchbar.com/search?q=谷歌快排系统官网【tg:seogoogle987】,w6o
- https://www.teacherspayteachers.com/Browse/Price-Range/Free/Search:谷歌快排系统官网【tg:seogoogle987】,686
- https://www.roberthalf.com/us/en/jobs/all/谷歌快排系统官网【tg:seogoogle987】sx9
- https://www.smics.com/tc/site/search?token=谷歌快排系统官网【tg:seogoogle987】,gD1
- https://www.wellbees.com/index.php/catalogsearch/result/index/?q=谷歌快排系统官网【tg:seogoogle987】,FcM
- https://www.nctsn.org/search?cat=191&query=谷歌快排系统官网【tg:seogoogle987】,IVR
- https://www.londontheatredirect.com/search/谷歌快排系统官网【tg:seogoogle987】,NtU
- https://www.kawainter.com/catalogsearch/result/?q=谷歌快排系统官网【tg:seogoogle987】,nsB
- https://www.revere.org/search?action=SEARCH&q=谷歌快排系统官网【tg:seogoogle987】,FVQ
- https://www.vivaraise.fr/catalogsearch/result/?q=谷歌快排系统官网【tg:seogoogle987】,qAe
- https://www.tombow.com/en/search/谷歌快排系统官网【tg:seogoogle987】,tQU
- https://www.wilsonart.com/catalogsearch/result/?action=CHECK&q=谷歌快排系统官网【tg:seogoogle987】,CRQ
- https://yscc1986.net/search/谷歌快排系统官网【tg:seogoogle987】x6S
- https://www.trockenmittel.ch/catalogsearch/result/?iC=ZM3&q=谷歌快排系统官网【tg:seogoogle987】,o59
- https://www.polliwogs.com/search/谷歌快排系统官网【tg:seogoogle987】,iLS
- https://yscc1986.net/search/谷歌快排系统官网【tg:seogoogle987】wGb
- https://www.unclefossil.com/search/谷歌快排系统官网【tg:seogoogle987】,mjk
- https://hsmoa.com/search?query=谷歌快排系统官网【tg:seogoogle987】W1K
- https://www.kubb-tool.bfs.admin.ch/en/search?action=CHECK&q=谷歌快排系统官网【tg:seogoogle987】,4yF
- https://wydden.com/?ID=r8w9WMSKWkOcmiaFWaUv0dzMdqZWfKtF&s=谷歌快排系统官网【tg:seogoogle987】,Ntj
- https://www.ussalabama.com/?action=CHECK&s=谷歌快排系统官网【tg:seogoogle987】,EvQ
- https://www.riohondo.edu/?action=QUERY&s=谷歌快排系统官网【tg:seogoogle987】,hxo
- https://www.target.com/s?searchTerm=谷歌快排系统官网【tg:seogoogle987】,82Z
- https://www.whirlpool.com.hk/chinese/catalogsearch/result?cat=30&q=谷歌快排系统官网【tg:seogoogle987】,pdI
- https://www.vfw.org/search-results?q=谷歌快排系统官网【tg:seogoogle987】,RPV
- https://www.so.com/s?ie=utf-8&src=360sou_home&_re=0&q=谷歌快排系统官网【tg:seogoogle987】,iq0
- https://www.qidulp.com/search?query=谷歌快排系统官网【tg:seogoogle987】,f0K
- https://www.rockngem.com/?action=CHECK&s=谷歌快排系统官网【tg:seogoogle987】,1JD
- https://www.teamlab.art/es/?submit=谷歌快排系统官网【tg:seogoogle987】,BFk
- https://www.nslcleaders.org/?s=谷歌快排系统官网【tg:seogoogle987】,lR2
- https://www.stormberg.com/no/search?q=谷歌快排系统官网【tg:seogoogle987】,Bay
- https://www.uctronics.com/catalogsearch/result/?q=谷歌快排系统官网【tg:seogoogle987】,ZXi