关于java:GitHub-又一-OCR-神器面世让你快速告别复制-粘贴

14次阅读

共计 2934 个字符,预计需要花费 8 分钟才能阅读完成。

对很多人来说,将 PDF 转换为可编辑的文本是个刚需,却苦于没有简略办法。在本文介绍的我的项目中,来自 K1 Digital 的高级机器学习工程师 Lucas Soares,尝试应用 OCR(光学字符识别)主动转录 pdf 幻灯片,转录成果还不错。

传统的讲座通常随同着一组 pdf 幻灯片。一般来说,想要对此类讲座做笔记,须要从 pdf 复制、粘贴很多内容。

最近,来自 K1 Digital 的高级机器学习工程师 Lucas Soares 始终在尝试通过应用 OCR(光学字符识别)主动转录 pdf 幻灯片,以便间接在 markdown 文件中操作它们的内容,从而防止手动复制和粘贴 pdf 内容,实现这一过程的自动化。

我的项目作者 Lucas Soares。
我的项目地址:https://github.com/EnkrateiaL…

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

Lucas Soares 发现传统工具往往会带来更多的问题,须要花工夫解决。他已经尝试应用传统的 Python 软件包,然而遇到了很多问题(例如必须应用简单的正则表达式模式解析最终输入等),因而决定尝试应用指标检测和 OCR 来解决。

根本过程可分为以下步骤:

  • 将 pdf 转换为图片;
  • 检测和辨认图像中的文本;
  • 展现示例输入。

基于深度学习的 OCR 将 pdf 转录为文本

将 pdf 转换为图像

Soares 应用的 pdf 幻灯片来自于 David Silver 的加强学习(参见以下 pdf 幻灯片地址)。应用「pdf2image」包将每张幻灯片转换为 png 图像格式。

pdf 幻灯片示例。地址:https://www.davidsilver.uk/wp…

代码如下:

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…

代码如下:

# 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 幻灯片),而后通过 single_pic_proc () 函数运行 OCR 模块中的检测和辨认模型,最初将输入保留到输入文件夹。

其中检测继承(inherit)了 Pytorch CTPN 模型,辨认继承了 Pytorch CRNN 模型,两者都存在于 OCR 模块中。

示例输入

代码如下:

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

下图左为原始 pdf 幻灯片,图右为转录后的输入文本,转录后的准确率十分高。

文本辨认输入如下:

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

通过上述办法,最终你能够失去一个十分弱小的工具来转录各种文档,从检测和辨认手写笔记到检测和辨认照片中的随机文本。领有本人的 OCR 工具来解决一些文本内容,这比依赖内部软件来转录文档要好的多。

原文链接:https://towardsdatascience.co…

正文完
 0