日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

從PDF和圖像中提取文本,以供大型語言模型使用

來源: 責編: 時間:2023-11-30 09:29:09 288觀看
導讀想法大型語言模型已經席卷了互聯網,導致更多的人沒有認真關注使用這些模型最重要的部分:高質量的數據!本文旨在提供一些有效從任何類型文檔中提取文本的技術。Python庫本文專注于Pytesseract、easyOCR、PyPDF2和LangChai

想法

大型語言模型已經席卷了互聯網,導致更多的人沒有認真關注使用這些模型最重要的部分:高質量的數據!本文旨在提供一些有效從任何類型文檔中提取文本的技術。8VF28資訊網——每日最新資訊28at.com

8VF28資訊網——每日最新資訊28at.com

Python庫

本文專注于Pytesseract、easyOCR、PyPDF2和LangChain庫。實驗數據是一個單頁PDF文件,可在以下鏈接獲取:8VF28資訊網——每日最新資訊28at.com

https://github.com/keitazoumana/Experimentation-Data/blob/main/Experimentation_file.pdf8VF28資訊網——每日最新資訊28at.com

由于Pytesseract和easyOCR可以處理圖像,因此在執行內容提取之前需要將PDF文件轉換為圖像。可以使用pypdfium2進行轉換,這是一個用于處理PDF文件的強大庫,其實現如下:8VF28資訊網——每日最新資訊28at.com

pip install pypdfium2

以下函數以PDF作為輸入,并將PDF的每一頁作為圖像列表返回。8VF28資訊網——每日最新資訊28at.com

def convert_pdf_to_images(file_path, scale=300/72):      pdf_file = pdfium.PdfDocument(file_path)      page_indices = [i for i in range(len(pdf_file))]      renderer = pdf_file.render(       pdfium.PdfBitmap.to_pil,       page_indices = page_indices,        scale = scale,   )      final_images = []       for i, image in zip(page_indices, renderer):              image_byte_array = BytesIO()       image.save(image_byte_array, format='jpeg', optimize=True)       image_byte_array = image_byte_array.getvalue()       final_images.append(dict({i:image_byte_array}))      return final_images

現在,我們可以使用display_images函數來可視化PDF文件的所有頁面。8VF28資訊網——每日最新資訊28at.com

def display_images(list_dict_final_images):      all_images = [list(data.values())[0] for data in list_dict_final_images]      for index, image_bytes in enumerate(all_images):              image = Image.open(BytesIO(image_bytes))       figure = plt.figure(figsize = (image.width / 100, image.height / 100))              plt.title(f"----- Page Number {index+1} -----")       plt.imshow(image)       plt.axis("off")       plt.show()

通過組合上述兩個函數,我們可以得到以下結果:8VF28資訊網——每日最新資訊28at.com

convert_pdf_to_images = convert_pdf_to_images('Experimentation_file.pdf')display_images(convert_pdf_to_images)

8VF28資訊網——每日最新資訊28at.com

圖片PDF以圖像格式可視化8VF28資訊網——每日最新資訊28at.com

深入文本提取過程

1.Pytesseract

Pytesseract(Python-tesseract)是用于從圖像中提取文本信息的Python OCR工具,可以使用以下pip命令進行安裝:8VF28資訊網——每日最新資訊28at.com

pip install pytesseract

以下的輔助函數使用了 Pytesseract 的 image_to_string() 函數從輸入圖像中提取文本。8VF28資訊網——每日最新資訊28at.com

from pytesseract import image_to_stringdef extract_text_with_pytesseract(list_dict_final_images):      image_list = [list(data.values())[0] for data in list_dict_final_images]   image_content = []      for index, image_bytes in enumerate(image_list):              image = Image.open(BytesIO(image_bytes))       raw_text = str(image_to_string(image))       image_content.append(raw_text)      return "/n".join(image_content)

可以使用 extract_text_with_pytesseract 函數提取文本,如下所示:8VF28資訊網——每日最新資訊28at.com

text_with_pytesseract = extract_text_with_pytesseract(convert_pdf_to_images)print(text_with_pytesseract)

成功執行以上代碼將生成以下結果:8VF28資訊網——每日最新資訊28at.com

This document provides a quick summary of some of Zoumana’s article on Medium.It can be considered as the compilation of his 80+ articles about Data Science, Machine Learning andMachine Learning Operations....Pytesseract was able to extract the content of the image.Here is how it managed to do it!Pytesseract starts by identifying rectangular shapes within the input image from top-right to bottom-right. Then it extracts the content of the individual images, and the final result is the concatenation of those extracted content. This approach works perfectly when dealing with column-based PDFs and image documents....

Pytesseract 首先通過從圖像的右上角到右下角識別矩形形狀。然后它提取各個圖像的內容,最終的結果是這些提取內容的串聯。這種方法在處理基于列的 PDF 和圖像文檔時效果非常好。8VF28資訊網——每日最新資訊28at.com

2.easyOCR

easyOCR 也是一個用于光學字符識別的開源 Python 庫,目前支持提取 80 多種語言的文本。easyOCR需要安裝Pytorch 和 OpenCV,可以使用以下指令安裝:8VF28資訊網——每日最新資訊28at.com

!pip install opencv-python-headless==4.1.2.30

根據您的操作系統,安裝 Pytorch 模塊的方法可能不同。但所有的說明都可以在官方頁面上找到。現在我們來安裝 easyOCR 庫:8VF28資訊網——每日最新資訊28at.com

!pip install easyocr

在使用 easyOCR 時,因為它支持多語言,所以在處理文檔時需要指定語言。通過其 Reader 模塊設置語言,指定語言列表。例如,fr 用于法語,en 用于英語。語言的詳細列表在此處可用。8VF28資訊網——每日最新資訊28at.com

from easyocr import Reader# Load model for the English languagelanguage_reader = Reader(["en"])

文本提取過程在extract_text_with_easyocr 函數中實現:8VF28資訊網——每日最新資訊28at.com

def extract_text_with_easyocr(list_dict_final_images):      image_list = [list(data.values())[0] for data in list_dict_final_images]   image_content = []      for index, image_bytes in enumerate(image_list):              image = Image.open(BytesIO(image_bytes))       raw_text = language_reader.readtext(image)       raw_text = " ".join([res[1] for res in raw_text])                             image_content.append(raw_text)      return "/n".join(image_content)

我們可以如下執行上述函數:8VF28資訊網——每日最新資訊28at.com

text_with_easy_ocr = extract_text_with_easyocr(convert_pdf_to_images)print(text_with_easy_ocr)

8VF28資訊網——每日最新資訊28at.com

easyOCR 的結果8VF28資訊網——每日最新資訊28at.com

與 Pytesseract 相比,easyOCR 的效果似乎不太高效。例如,它能夠有效地讀取前兩個段落。然而,它不是將每個文本塊視為獨立的文本,而是使用基于行的方法進行讀取。例如,第一個文本塊中的字符串“Data Science section covers basic to advanced”已與第二個文本塊中的“overfitting when training computer vision”組合在一起,這種組合完全破壞了文本的結構并使最終結果產生偏差。8VF28資訊網——每日最新資訊28at.com

3.PyPDF2

PyPDF2 也是一個專門用于 PDF 處理任務的 Python 庫,例如文本和元數據的檢索、合并、裁剪等。8VF28資訊網——每日最新資訊28at.com

!pip install PyPDF2

提取邏輯實現在 extract_text_with_pyPDF 函數中:8VF28資訊網——每日最新資訊28at.com

def extract_text_with_pyPDF(PDF_File):    pdf_reader = PdfReader(PDF_File)        raw_text = ''    for i, page in enumerate(pdf_reader.pages):                text = page.extract_text()        if text:            raw_text += text    return raw_texttext_with_pyPDF = extract_text_with_pyPDF("Experimentation_file.pdf")print(text_with_pyPDF)

8VF28資訊網——每日最新資訊28at.com

使用 PyPDF 庫進行文本提取8VF28資訊網——每日最新資訊28at.com

提取過程快速而準確,甚至保留了原始字體大小。PyPDF 的主要問題是它不能有效地從圖像中提取文本。8VF28資訊網——每日最新資訊28at.com

4.LangChain

LangChain 的 UnstructuredImageLoader 和 UnstructuredFileLoader 模塊可分別用于從圖像和文本/PDF 文件中提取文本,并且在本節中將探討這兩個選項。8VF28資訊網——每日最新資訊28at.com

首先,我們需要按照以下方式安裝 langchain 庫:8VF28資訊網——每日最新資訊28at.com

!pip install langchain

(1) 從圖像中提取文本8VF28資訊網——每日最新資訊28at.com

from langchain.document_loaders.image import UnstructuredImageLoader

以下是提取文本的函數:8VF28資訊網——每日最新資訊28at.com

def extract_text_with_langchain_image(list_dict_final_images):   image_list = [list(data.values())[0] for data in list_dict_final_images]   image_content = []      for index, image_bytes in enumerate(image_list):              image = Image.open(BytesIO(image_bytes))       loader = UnstructuredImageLoader(image)       data = loader.load()       raw_text = data[index].page_content                             image_content.append(raw_text)      return "/n".join(image_content)

現在,我們可以提取內容:8VF28資訊網——每日最新資訊28at.com

text_with_langchain_image = extract_text_with_langchain_image(convert_pdf_to_images)print(text_with_langchain_image)

8VF28資訊網——每日最新資訊28at.com

來自 langchain UnstructuredImageLoader 的文本提取。8VF28資訊網——每日最新資訊28at.com

該庫成功高效地提取了圖像的內容。8VF28資訊網——每日最新資訊28at.com

(2) 從 PDF 中提取文本8VF28資訊網——每日最新資訊28at.com

以下是從 PDF 中提取內容的實現:8VF28資訊網——每日最新資訊28at.com

from langchain.document_loaders import UnstructuredFileLoaderdef extract_text_with_langchain_pdf(pdf_file):      loader = UnstructuredFileLoader(pdf_file)   documents = loader.load()   pdf_pages_content = '/n'.join(doc.page_content for doc in documents)      return pdf_pages_contenttext_with_langchain_files = extract_text_with_langchain_pdf("Experimentation_file.pdf")print(text_with_langchain_files)

類似于 PyPDF 模塊,langchain 模塊能夠生成準確的結果,同時保持原始字體大小。8VF28資訊網——每日最新資訊28at.com

8VF28資訊網——每日最新資訊28at.com

從 langchain 的 UnstructuredFileLoader 中提取文本。8VF28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-35306-0.html從PDF和圖像中提取文本,以供大型語言模型使用

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 全網最細:Jest+Enzyme測試React組件(包含交互、DOM、樣式測試)

下一篇: 聊聊Clickhouse分布式表的操作

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 宜兰市| 兰溪市| 改则县| 唐河县| 应城市| 舒城县| 长顺县| 吴忠市| 普陀区| 阿克苏市| 合川市| 巴彦淖尔市| 临夏市| 勃利县| 宣汉县| 鄂伦春自治旗| 北票市| 大渡口区| 腾冲县| 辛集市| 山东省| 亳州市| 霍林郭勒市| 克东县| 金昌市| 祥云县| 琼海市| 枞阳县| 嘉定区| 耿马| 广州市| 内乡县| 沙湾县| 漯河市| 桐城市| 会同县| 晋城| 高唐县| 桃园县| 棋牌| 宿松县|