您现在的位置是:热点 >>正文
告别「复制+粘贴」,基于深度学习的OCR,实现PDF转文本
热点27329488人已围观
简介...
传统的讲座通常伴随着一组 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「复_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&nb制+sp;= 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 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&nb基于sp;sns 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&深度nbsp;result,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): 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 的O;text 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('.'CR)[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]+' ')&,实nbsp; txt_f.close()
设置输入和输出文件夹,接着遍历所有输入图像(转换后的 pdf 幻灯片),然后通过 single_pic_proc() 函数运行 OCR 模块中的检测和识别模型,最后将输出保存到输出文件夹。
其中检测继承(inherit)了 Pytorch CTPN 模型,识别继承了 Pytorch CRNN 模型,两者都存在于 OCR 模块中。
示例输出
代码如下:
import cv2 as cv output_dir = pathlib.Path("./output_images_wi现Pth_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_reshapeDFd) 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:
相关文章
标签: 特斯拉 Optimus Yilun Chen 苹果
热点云梦级12月1日云梦级消息,11 月 30 日,前苹果机器人研究员 Yilun Chen宣布离开公司,加入特斯拉 Optimus AI 团队。Chen 在苹果一路从工程师做到技术负责人,称特斯拉在 O...
【热点】
阅读更多标签: 小米SU7 苹果MacBook Pro 比亚迪 M5芯片
热点Hello,大家早上好,又是元气满满的一天,先来浏览新鲜的早报吧~ 【云梦级消息称第一代小米 SU7 已停产】 小米汽车公布 2026 年 1 月交付量超 3.9 万辆,知情人士透露,第一代 SU7...
【热点】
阅读更多阿里云季度营收首次超过200亿元 计划为全新操作系统投入20亿专项资金
热点11月19日消息,据国外媒体报道,阿里巴巴集团发布的财报显示,他们在截至9月30日的2022财年第二财季营收2006.9亿元,同比增长29%,云计算业务表现亮眼,该财季营收200.07亿元,首次超过2...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.universal-music.co.jp/search/?q=谷歌留痕教程【tg:seogoogle987】,j7J
- https://www.smarty.net/search?q=谷歌留痕教程【tg:seogoogle987】,R4G
- https://www.spoiledsweetboutique.com/search/谷歌留痕教程【tg:seogoogle987】,KCL
- https://www.springfieldlibrary.org/?s=谷歌留痕教程【tg:seogoogle987】,jBh
- https://www.kores.com/?s=谷歌留痕教程【tg:seogoogle987】,8AX
- https://www.solerpalau.com/en-en/catalogsearch/result/?q=谷歌留痕教程【tg:seogoogle987】,cCA
- https://www.mygrandma.com/Items.aspx?search=谷歌留痕教程【tg:seogoogle987】,JQh
- https://www.voices.com/talents/search?keywords=谷歌留痕教程【tg:seogoogle987】,e6Q
- https://www.nbm.org/?action=QUERY&s=谷歌留痕教程【tg:seogoogle987】,T2l
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=谷歌留痕教程【tg:seogoogle987】,Fo0
- https://www.polar-tech.com/?action=QUERY&s=谷歌留痕教程【tg:seogoogle987】,wpP
- https://www.pearson.com/en-us/search.html?aq=谷歌留痕教程【tg:seogoogle987】,BNs
- https://store.line.me/search/sticker/ja?q=谷歌留痕教程【tg:seogoogle987】ECu
- https://www.zutari.com/?s=谷歌留痕教程【tg:seogoogle987】,NZn
- https://www.wou.edu.my/?s=谷歌留痕教程【tg:seogoogle987】,ECd
- https://www.partilerdunyasi.com/index.php?route=product/search&tag=谷歌留痕教程【tg:seogoogle987】,JZZ
- https://m.shoppinghow.kakao.com/m/search/q/谷歌留痕教程【tg:seogoogle987】5YY
- https://www.tombow.com/search/谷歌留痕教程【tg:seogoogle987】,GoB
- https://www.nyif.com/catalogsearch/result/index/?f=sarbanesoxleyU&index=me_cms_pe&lit=20&mode=list&p=1&q=谷歌留痕教程【tg:seogoogle987】,Ba6
- https://www.temu.com/search_result.html?search_key=谷歌留痕教程【tg:seogoogle987】LxZ
- https://www.tullygroup.us/?ID=hbkq8jFjrQlVRrwzEmvoUJJAY4e1Lfo5&s=谷歌留痕教程【tg:seogoogle987】,ZUy
- https://hsmoa.com/search?query=谷歌留痕教程【tg:seogoogle987】ImQ
- https://www.rhymezone.com/r/rhyme.cgi?typeofrhyme=oldrel&loc=nojs&Word=谷歌留痕教程【tg:seogoogle987】,Wh8
- https://www.openstreetmap.org/search?query=谷歌留痕教程【tg:seogoogle987】,Mj4
- https://www.lneg.pt/en/?s=谷歌留痕教程【tg:seogoogle987】,IZZ
- https://www.localfootdoc.com/?s=谷歌留痕教程【tg:seogoogle987】,Hc4
- https://www.wmfe.org/?s=谷歌留痕教程【tg:seogoogle987】,Tru
- https://www.smics.com/tc/site/search?token=谷歌留痕教程【tg:seogoogle987】,FpE
- https://www.monroecounty.gov/search/?q=谷歌留痕教程【tg:seogoogle987】,9tt
- https://www.lezhin.com/ko/tags/谷歌留痕教程【tg:seogoogle987】,5WC
- https://xn--lgernevedlystskoven-lxb.dk/search/谷歌留痕教程【tg:seogoogle987】,XTg
- https://www.pmindia.gov.in/en/?s=谷歌留痕教程【tg:seogoogle987】,TbC
- https://www.pierreherme.com/catalogsearch/result/?q=谷歌留痕教程【tg:seogoogle987】,BHV
- https://www.lyrics.com/lyrics/kkk3谷歌留痕教程【tg:seogoogle987】,urA
- https://www.rollingstone.it/?ID=DVMeu3AlmzZE1cuJ2FmgmO5TSCQRb3yh&s=谷歌留痕教程【tg:seogoogle987】,Ra7
- https://www.swanagerailway.co.uk/search?q=谷歌留痕教程【tg:seogoogle987】,2ae
- https://www.roberthalf.com/us/en/jobs/all/谷歌留痕教程【tg:seogoogle987】n34
- https://www.symatoys.com/search.html?ID=UyMD6aO58bPrJvQAB1I0N69KzJBZeNPC&keyword=谷歌留痕教程【tg:seogoogle987】,gtj
- https://www.kaiyueleather.com/search/谷歌留痕教程【tg:seogoogle987】,LvV
- https://www.mintos.org/?s=谷歌留痕教程【tg:seogoogle987】,wsw
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌留痕教程【tg:seogoogle987】bMR
- https://www.usslexington.com/?action=QUERY&s=谷歌留痕教程【tg:seogoogle987】,2jN
- https://www.sensorshop24.de/catalogsearch/result/index/?type=132&q=谷歌留痕教程【tg:seogoogle987】,0zt
- https://www.youtube.com/hashtag/谷歌留痕教程【tg:seogoogle987】,YWx
- https://blvdseafood.com/?s=谷歌留痕教程【tg:seogoogle987】XYT
- https://www.therapeuticassociates.com/?cat=5&s=谷歌留痕教程【tg:seogoogle987】,fqj
- https://www.livehindustan.com/tags/谷歌留痕教程【tg:seogoogle987】,1xX
- https://www.qylaws.com/search/谷歌留痕教程【tg:seogoogle987】,ziE
- https://www.world-architects.com/en/search?term=谷歌留痕教程【tg:seogoogle987】,Gz6
- https://www.l-and-b.jp/?page=1&s=谷歌留痕教程【tg:seogoogle987】,fe6