站点图标 AI技术聚合

使用 Python 和深度学习加快笔记速度

原文标题Faster Notes with Python and Deep Learning

使用 Python 和深度学习加快笔记速度

使用基于深度学习的 OCR 转录 pdf 幻灯片

用于记笔记的管道

传统的讲座通常伴随着一组 pdf 幻灯片。传统上,对此类讲座做笔记的过程涉及复制或转录 pdf 中的大量内容。

最近我一直在尝试通过使用 OCR(对象字符识别)来自动转录 pdf 幻灯片以直接在 markdown 文件中操作其内容,从而避免手动复制和粘贴 pdf 的内容,从而使这一过程自动化。

在本文中,我将向您展示如何使用基于深度学习的 OCR 自动将 pdf 幻灯片转录为文本的过程。

为什么不使用传统的 pdf 转文本工具?

我选择不使用传统的 pdf 到文本工具的原因是我发现它们往往会给出更多的问题然后解决它们。我尝试使用传统的 python 包,但它们会引发一些问题,比如必须使用复杂的正则表达式模式解析最终输出,所以我决定尝试使用对象检测和 OCR 看看我是否可以做得更好。

Steps

要遵循的步骤是:

  1. 将pdf转换为图像
  2. 检测和识别图像中的文本
  3. Showcase example outputs

在这种情况下,我主要改编了来自这个存储库的代码,它使用 CTPN 模型的 pytorch 改编以及来自 pytorch_ctpn 的代码用于文本检测,以及使用来自 crnn.pytorch 的代码用于文本识别的 CRNN 模型。[0][1][2][3][4]

现在,让我们完成每一步。

1.将pdf转成图片

我将使用 David Silver 的强化学习介绍中的 pdf 幻灯片。让我们首先编写一些代码,使用 pdf2image 包将每张幻灯片转换为 png 格式。[0]

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")

现在我有了所有的图像,

让我们在每张幻灯片上运行文本检测和识别。

2.检测和识别图像中的文字

为此,我们将使用 ocr.pytorch 存储库中的文本检测器。按照那里的说明下载模型并将它们保存在检查点文件夹中。[0]

# 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 pytesseract

def single_pic_proc(image_file):
    image = np.array(Image.open(image_file).convert('RGB'))
    result, image_framed = ocr(image)
    return 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 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('.')[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 幻灯片)运行一个循环,然后通过运行位于 ocr 模块中的检测和识别模型的 single_pic_proc() 函数。然后,我们将输出保存到输出文件夹。

检测继承自 Pytorch 的 CTPN 模型,识别模型继承自 Pytorch 的 CRNN 模型,两者都存在于 ocr 模块中。[0][1]

4. Showcase example outputs

import cv2 as cv

output_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()

检测输出示例:

filename = f"{output_dir}/image7.txt"
with open(filename, "r") as text:
    for line in text.readlines():
        print(line.strip("\n"))

Text recognition output:

- AboutRL
Characteristics of Reinforcement Learning
What makes reinforcement learningdifferent from other machine
learning paradigms?
There isnosupervisor,only areward signal
Feedback is delavyed,not instantaneous
Time really matters(sequential,non ii.d data)
Agent sactions affectthe subseauent datait receives

不再复制和粘贴

我喜欢这种方法的地方在于,您最终可以获得一个非常强大的工具来转录各种文档,从检测和识别手写笔记到检测和识别照片中的随机文本。

拥有自己的 OCR 工具来处理您通常与之交互的内容比依赖外部软件来处理您想要转录的每个文档要好。

对我来说,这是一个有趣的机会来探索 OCR 并建立一个管道来快速转录一个附带 pdf 幻灯片的讲座。

你可以在这里查看源代码。[0]

如果您喜欢这篇文章,请在 Twitter、LinkedIn 上与我联系,并在 Medium 上关注我。谢谢,下次见! :)[0][1][2]

References

  • ocr.pytorch repository[0]
  • pytorch.ctpn[0]
  • CRNN[0]
  • CTPN[0]
  • David Silver 关于强化学习的讲座[0]

文章出处登录后可见!

已经登录?立即刷新
退出移动版