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

 告别「复制+粘贴」,基于深度学习的OCR,实现PDF转文本(PDF转ocr) 深度学习 编程 人工智能 第1张

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

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

告别「复制+粘贴」,基于深度学习的OCR,实现PDF转文本(PDF转ocr)  深度学习 编程 人工智能 第2张

左为项目作者 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 图像格式。

告别「复制+粘贴」,基于深度学习的OCR,实现PDF转文本(PDF转ocr)  深度学习 编程 人工智能 第3张

pdf 幻灯片示例。

地址:https://www.davidsilver.uk/wp-content/uploads/2020/03/intro_RL.pdf

代码如下:

  1. frompdf2imageimportconvert_from_path
  2. frompdf2image.exceptionsimport(
  3. PDFInfoNotInstalledError,
  4. PDFPageCountError,
  5. PDFSyntaxError
  6. )
  7. pdf_path="path/to/file/intro_RL_Lecture1.pdf"
  8. images=convert_from_path(pdf_path)
  9. fori,imageinenumerate(images):
  10. fname="image"+str(i)+".png"
  11. image.save(fname,"PNG")

经过处理后,所有的 pdf 幻灯片都转换成 png 格式的图像:

告别「复制+粘贴」,基于深度学习的OCR,实现PDF转文本(PDF转ocr)  深度学习 编程 人工智能 第4张

检测和识别图像中的文本

为了检测和识别 png 图像中的文本,Soares 使用 ocr.pytorch 库中的文本检测器。按照说明下载模型并将模型保存在 checkpoints 文件夹中。

ocr.pytorch 库地址:https://github.com/courao/ocr.pytorch

代码如下:

  1. #adaptedfromthissource:https://github.com/courao/ocr.pytorch
  2. %load_extautoreload
  3. %autoreload2
  4. importos
  5. fromocrimportocr
  6. importtime
  7. importshutil
  8. importnumpyasnp
  9. importpathlib
  10. fromPILimportImage
  11. fromglobimportglob
  12. importmatplotlib.pyplotasplt
  13. importseabornassns
  14. sns.set()
  15. importpytesseract
  16. defsingle_pic_proc(image_file):
  17. image=np.array(Image.open(image_file).convert('RGB'))
  18. result,image_framed=ocr(image)
  19. returnresult,image_framed
  20. image_files=glob('./input_images/*.*')
  21. result_dir='./output_images_with_boxes/'
  22. #Iftheoutputfolderexistswewillremoveitandredoit.
  23. ifos.path.exists(result_dir):
  24. shutil.rmtree(result_dir)
  25. os.mkdir(result_dir)
  26. forimage_fileinsorted(image_files):
  27. result,image_framed=single_pic_proc(image_file)#detectingandrecognizingthetext
  28. filename=pathlib.Path(image_file).name
  29. output_file=os.path.join(result_dir,image_file.split('/')[-1])
  30. txt_file=os.path.join(result_dir,image_file.split('/')[-1].split('.')[0]+'.txt')
  31. txt_f=open(txt_file,'w')
  32. Image.fromarray(image_framed).save(output_file)
  33. forkeyinresult:
  34. txt_f.write(result[key][1]+'\n')
  35. txt_f.close()

设置输入和输出文件夹,接着遍历所有输入图像(转换后的 pdf 幻灯片),然后通过 single_pic_proc() 函数运行 OCR 模块中的检测和识别模型,最后将输出保存到输出文件夹。

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

示例输出

代码如下:

  1. importcv2ascv
  2. output_dir=pathlib.Path("./output_images_with_boxes")
  3. #image=cv.imread(str(np.random.choice(list(output_dir.iterdir()),1)[0]))
  4. image=cv.imread(f"{output_dir}/image7.png")
  5. size_reshaped=(int(image.shape[1]),int(image.shape[0]))
  6. image=cv.resize(image,size_reshaped)
  7. cv.imshow("image",image)
  8. cv.waitKey(0)
  9. cv.destroyAllWindows()

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

告别「复制+粘贴」,基于深度学习的OCR,实现PDF转文本(PDF转ocr)  深度学习 编程 人工智能 第5张

文本识别输出如下:

  1. filename=f"{output_dir}/image7.txt"
  2. withopen(filename,"r")astext:
  3. forlineintext.readlines():
  4. print(line.strip("\n"))

通过上述方法,最终你可以得到一个非常强大的工具来转录各种文档,从检测和识别手写笔记到检测和识别照片中的随机文本。拥有自己的 OCR 工具来处理一些文本内容,这比依赖外部软件来转录文档要好的多。

转载请说明出处
知优网 » 告别「复制+粘贴」,基于深度学习的OCR,实现PDF转文本(PDF转ocr)

发表评论

您需要后才能发表评论