自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

告別「復(fù)制+粘貼」,基于深度學(xué)習(xí)的OCR,實(shí)現(xiàn)PDF轉(zhuǎn)文本

新聞 深度學(xué)習(xí)
最近,來自 K1 Digital 的高級(jí)機(jī)器學(xué)習(xí)工程師 Lucas Soares 一直在嘗試通過使用 OCR(光學(xué)字符識(shí)別)自動(dòng)轉(zhuǎn)錄 pdf 幻燈片,以便直接在 markdown 文件中操作它們的內(nèi)容,從而避免手動(dòng)復(fù)制和粘貼 pdf 內(nèi)容,實(shí)現(xiàn)這一過程的自動(dòng)化。

[[403226]]

傳統(tǒng)的講座通常伴隨著一組 pdf 幻燈片。一般來說,想要對(duì)此類講座做筆記,需要從 pdf 復(fù)制、粘貼很多內(nèi)容。

最近,來自 K1 Digital 的高級(jí)機(jī)器學(xué)習(xí)工程師 Lucas Soares 一直在嘗試通過使用 OCR(光學(xué)字符識(shí)別)自動(dòng)轉(zhuǎn)錄 pdf 幻燈片,以便直接在 markdown 文件中操作它們的內(nèi)容,從而避免手動(dòng)復(fù)制和粘貼 pdf 內(nèi)容,實(shí)現(xiàn)這一過程的自動(dòng)化。

告別「復(fù)制+粘貼」,基于深度學(xué)習(xí)的OCR,實(shí)現(xiàn)PDF轉(zhuǎn)文本

左為項(xiàng)目作者 Lucas Soares。

項(xiàng)目地址:https://github.com/EnkrateiaLucca/ocr_for_transcribing_pdf_slides

為什么不使用傳統(tǒng)的 pdf 轉(zhuǎn)文本工具呢?

Lucas Soares 發(fā)現(xiàn)傳統(tǒng)工具往往會(huì)帶來更多的問題,需要花時(shí)間解決。他曾經(jīng)嘗試使用傳統(tǒng)的 Python 軟件包,但是遇到了很多問題(例如必須使用復(fù)雜的正則表達(dá)式模式解析最終輸出等),因此決定嘗試使用目標(biāo)檢測(cè)和 OCR 來解決。

基本過程可分為以下步驟:

  • 將 pdf 轉(zhuǎn)換為圖片;
  • 檢測(cè)和識(shí)別圖像中的文本;
  • 展示示例輸出。

基于深度學(xué)習(xí)的 OCR 將 pdf 轉(zhuǎn)錄為文本

將 pdf 轉(zhuǎn)換為圖像

Soares 使用的 pdf 幻燈片來自于 David Silver 的增強(qiáng)學(xué)習(xí)(參見以下 pdf 幻燈片地址)。使用「pdf2image」包將每張幻燈片轉(zhuǎn)換為 png 圖像格式。

告別「復(fù)制+粘貼」,基于深度學(xué)習(xí)的OCR,實(shí)現(xiàn)PDF轉(zhuǎn)文本

pdf 幻燈片示例。

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

代碼如下: 

  1. from pdf2image import convert_from_path 
  2. from pdf2image.exceptions import ( 
  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. for i, image in enumerate(images): 
  10.     fname = "image" + str(i) + ".png" 
  11.     image.save(fname, "PNG"

經(jīng)過處理后,所有的 pdf 幻燈片都轉(zhuǎn)換成 png 格式的圖像:

告別「復(fù)制+粘貼」,基于深度學(xué)習(xí)的OCR,實(shí)現(xiàn)PDF轉(zhuǎn)文本

檢測(cè)和識(shí)別圖像中的文本

為了檢測(cè)和識(shí)別 png 圖像中的文本,Soares 使用 ocr.pytorch 庫中的文本檢測(cè)器。按照說明下載模型并將模型保存在 checkpoints 文件夾中。

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

代碼如下: 

  1. # adapted from this source: https://github.com/courao/ocr.pytorch 
  2. %load_ext autoreload 
  3. %autoreload 2 
  4. import os 
  5. from ocr import ocr 
  6. import time 
  7. import shutil 
  8. import numpy as np 
  9. import pathlib 
  10. from PIL import Image 
  11. from glob import glob 
  12. import matplotlib.pyplot as plt 
  13. import seaborn as sns 
  14. sns.set() 
  15. import pytesseract 
  16.  
  17. def single_pic_proc(image_file): 
  18.     image = np.array(Image.open(image_file).convert('RGB')) 
  19.     result, image_framed = ocr(image) 
  20.     return result,image_framed 
  21.  
  22. image_files = glob('./input_images/*.*'
  23. result_dir = './output_images_with_boxes/' 
  24.  
  25. # If the output folder exists we will remove it and redo it. 
  26. if os.path.exists(result_dir): 
  27.     shutil.rmtree(result_dir) 
  28. os.mkdir(result_dir) 
  29.  
  30. for image_file in sorted(image_files): 
  31.     result, image_framed = single_pic_proc(image_file) # detecting and recognizing the text 
  32.     filename = pathlib.Path(image_file).name 
  33.     output_file = os.path.join(result_dir, image_file.split('/')[-1]) 
  34.     txt_file = os.path.join(result_dir, image_file.split('/')[-1].split('.')[0]+'.txt'
  35.     txt_f = open(txt_file, 'w'
  36.     Image.fromarray(image_framed).save(output_file) 
  37.     for key in result: 
  38.         txt_f.write(result[key][1]+'\n'
  39.     txt_f.close() 

設(shè)置輸入和輸出文件夾,接著遍歷所有輸入圖像(轉(zhuǎn)換后的 pdf 幻燈片),然后通過 single_pic_proc() 函數(shù)運(yùn)行 OCR 模塊中的檢測(cè)和識(shí)別模型,最后將輸出保存到輸出文件夾。

其中檢測(cè)繼承(inherit)了 Pytorch CTPN 模型,識(shí)別繼承了 Pytorch CRNN 模型,兩者都存在于 OCR 模塊中。

示例輸出

代碼如下: 

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

下圖左為原始 pdf 幻燈片,圖右為轉(zhuǎn)錄后的輸出文本,轉(zhuǎn)錄后的準(zhǔn)確率非常高。

告別「復(fù)制+粘貼」,基于深度學(xué)習(xí)的OCR,實(shí)現(xiàn)PDF轉(zhuǎn)文本

文本識(shí)別輸出如下: 

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

通過上述方法,最終你可以得到一個(gè)非常強(qiáng)大的工具來轉(zhuǎn)錄各種文檔,從檢測(cè)和識(shí)別手寫筆記到檢測(cè)和識(shí)別照片中的隨機(jī)文本。擁有自己的 OCR 工具來處理一些文本內(nèi)容,這比依賴外部軟件來轉(zhuǎn)錄文檔要好的多。

 

責(zé)任編輯:張燕妮 來源: 機(jī)器之心Pro
相關(guān)推薦

2021-09-24 09:59:59

復(fù)制粘貼PythonPDF

2020-09-14 17:10:16

微信搜索移動(dòng)應(yīng)用

2024-10-25 11:56:33

OCRVisRAGRAG

2024-08-29 08:23:22

EasyOCRSpring文字識(shí)別

2017-05-22 13:15:45

TensorFlow深度學(xué)習(xí)

2017-05-12 16:25:44

深度學(xué)習(xí)圖像補(bǔ)全tensorflow

2018-07-19 15:13:15

深度學(xué)習(xí)圖像

2023-05-22 08:00:00

深度學(xué)習(xí)機(jī)器學(xué)習(xí)人工智能

2023-09-26 07:39:21

2019-05-22 14:28:08

AI人工智能深度學(xué)習(xí)

2018-08-03 09:42:01

人工智能深度學(xué)習(xí)人臉識(shí)別

2021-11-03 09:00:00

深度學(xué)習(xí)自然語言機(jī)器學(xué)習(xí)

2024-12-03 09:59:00

2022-10-26 15:41:38

深度學(xué)習(xí)Deepfake機(jī)器學(xué)習(xí)

2024-11-04 08:14:48

2017-09-21 15:43:02

深度序列學(xué)習(xí)

2017-08-03 16:20:42

深度學(xué)習(xí)文本摘要遞歸神經(jīng)網(wǎng)絡(luò)

2020-10-17 09:03:06

使用JS創(chuàng)建復(fù)制&粘貼

2023-09-07 10:37:43

OCR項(xiàng)目字符串

2017-07-06 15:02:53

OpenGL ES架構(gòu)GPU
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)