AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁(yè) > 科技  > 軟件

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

來(lái)源: 責(zé)編: 時(shí)間:2023-11-30 09:29:09 281觀看
導(dǎo)讀想法大型語(yǔ)言模型已經(jīng)席卷了互聯(lián)網(wǎng),導(dǎo)致更多的人沒(méi)有認(rèn)真關(guān)注使用這些模型最重要的部分:高質(zhì)量的數(shù)據(jù)!本文旨在提供一些有效從任何類(lèi)型文檔中提取文本的技術(shù)。Python庫(kù)本文專(zhuān)注于Pytesseract、easyOCR、PyPDF2和LangChai

想法

大型語(yǔ)言模型已經(jīng)席卷了互聯(lián)網(wǎng),導(dǎo)致更多的人沒(méi)有認(rèn)真關(guān)注使用這些模型最重要的部分:高質(zhì)量的數(shù)據(jù)!本文旨在提供一些有效從任何類(lèi)型文檔中提取文本的技術(shù)。Uoj28資訊網(wǎng)——每日最新資訊28at.com

Uoj28資訊網(wǎng)——每日最新資訊28at.com

Python庫(kù)

本文專(zhuān)注于Pytesseract、easyOCR、PyPDF2和LangChain庫(kù)。實(shí)驗(yàn)數(shù)據(jù)是一個(gè)單頁(yè)P(yáng)DF文件,可在以下鏈接獲取:Uoj28資訊網(wǎng)——每日最新資訊28at.com

https://github.com/keitazoumana/Experimentation-Data/blob/main/Experimentation_file.pdfUoj28資訊網(wǎng)——每日最新資訊28at.com

由于Pytesseract和easyOCR可以處理圖像,因此在執(zhí)行內(nèi)容提取之前需要將PDF文件轉(zhuǎn)換為圖像。可以使用pypdfium2進(jìn)行轉(zhuǎn)換,這是一個(gè)用于處理PDF文件的強(qiáng)大庫(kù),其實(shí)現(xiàn)如下:Uoj28資訊網(wǎng)——每日最新資訊28at.com

pip install pypdfium2

以下函數(shù)以PDF作為輸入,并將PDF的每一頁(yè)作為圖像列表返回。Uoj28資訊網(wǎng)——每日最新資訊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

現(xiàn)在,我們可以使用display_images函數(shù)來(lái)可視化PDF文件的所有頁(yè)面。Uoj28資訊網(wǎng)——每日最新資訊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()

通過(guò)組合上述兩個(gè)函數(shù),我們可以得到以下結(jié)果:Uoj28資訊網(wǎng)——每日最新資訊28at.com

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

Uoj28資訊網(wǎng)——每日最新資訊28at.com

圖片PDF以圖像格式可視化Uoj28資訊網(wǎng)——每日最新資訊28at.com

深入文本提取過(guò)程

1.Pytesseract

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

pip install pytesseract

以下的輔助函數(shù)使用了 Pytesseract 的 image_to_string() 函數(shù)從輸入圖像中提取文本。Uoj28資訊網(wǎng)——每日最新資訊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 函數(shù)提取文本,如下所示:Uoj28資訊網(wǎng)——每日最新資訊28at.com

text_with_pytesseract = extract_text_with_pytesseract(convert_pdf_to_images)print(text_with_pytesseract)

成功執(zhí)行以上代碼將生成以下結(jié)果:Uoj28資訊網(wǎng)——每日最新資訊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 首先通過(guò)從圖像的右上角到右下角識(shí)別矩形形狀。然后它提取各個(gè)圖像的內(nèi)容,最終的結(jié)果是這些提取內(nèi)容的串聯(lián)。這種方法在處理基于列的 PDF 和圖像文檔時(shí)效果非常好。Uoj28資訊網(wǎng)——每日最新資訊28at.com

2.easyOCR

easyOCR 也是一個(gè)用于光學(xué)字符識(shí)別的開(kāi)源 Python 庫(kù),目前支持提取 80 多種語(yǔ)言的文本。easyOCR需要安裝Pytorch 和 OpenCV,可以使用以下指令安裝:Uoj28資訊網(wǎng)——每日最新資訊28at.com

!pip install opencv-python-headless==4.1.2.30

根據(jù)您的操作系統(tǒng),安裝 Pytorch 模塊的方法可能不同。但所有的說(shuō)明都可以在官方頁(yè)面上找到。現(xiàn)在我們來(lái)安裝 easyOCR 庫(kù):Uoj28資訊網(wǎng)——每日最新資訊28at.com

!pip install easyocr

在使用 easyOCR 時(shí),因?yàn)樗С侄嗾Z(yǔ)言,所以在處理文檔時(shí)需要指定語(yǔ)言。通過(guò)其 Reader 模塊設(shè)置語(yǔ)言,指定語(yǔ)言列表。例如,fr 用于法語(yǔ),en 用于英語(yǔ)。語(yǔ)言的詳細(xì)列表在此處可用。Uoj28資訊網(wǎng)——每日最新資訊28at.com

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

文本提取過(guò)程在extract_text_with_easyocr 函數(shù)中實(shí)現(xiàn):Uoj28資訊網(wǎng)——每日最新資訊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)

我們可以如下執(zhí)行上述函數(shù):Uoj28資訊網(wǎng)——每日最新資訊28at.com

text_with_easy_ocr = extract_text_with_easyocr(convert_pdf_to_images)print(text_with_easy_ocr)

Uoj28資訊網(wǎng)——每日最新資訊28at.com

easyOCR 的結(jié)果Uoj28資訊網(wǎng)——每日最新資訊28at.com

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

3.PyPDF2

PyPDF2 也是一個(gè)專(zhuān)門(mén)用于 PDF 處理任務(wù)的 Python 庫(kù),例如文本和元數(shù)據(jù)的檢索、合并、裁剪等。Uoj28資訊網(wǎng)——每日最新資訊28at.com

!pip install PyPDF2

提取邏輯實(shí)現(xiàn)在 extract_text_with_pyPDF 函數(shù)中:Uoj28資訊網(wǎng)——每日最新資訊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)

Uoj28資訊網(wǎng)——每日最新資訊28at.com

使用 PyPDF 庫(kù)進(jìn)行文本提取Uoj28資訊網(wǎng)——每日最新資訊28at.com

提取過(guò)程快速而準(zhǔn)確,甚至保留了原始字體大小。PyPDF 的主要問(wèn)題是它不能有效地從圖像中提取文本。Uoj28資訊網(wǎng)——每日最新資訊28at.com

4.LangChain

LangChain 的 UnstructuredImageLoader 和 UnstructuredFileLoader 模塊可分別用于從圖像和文本/PDF 文件中提取文本,并且在本節(jié)中將探討這兩個(gè)選項(xiàng)。Uoj28資訊網(wǎng)——每日最新資訊28at.com

首先,我們需要按照以下方式安裝 langchain 庫(kù):Uoj28資訊網(wǎng)——每日最新資訊28at.com

!pip install langchain

(1) 從圖像中提取文本Uoj28資訊網(wǎng)——每日最新資訊28at.com

from langchain.document_loaders.image import UnstructuredImageLoader

以下是提取文本的函數(shù):Uoj28資訊網(wǎng)——每日最新資訊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)

現(xiàn)在,我們可以提取內(nèi)容:Uoj28資訊網(wǎng)——每日最新資訊28at.com

text_with_langchain_image = extract_text_with_langchain_image(convert_pdf_to_images)print(text_with_langchain_image)

Uoj28資訊網(wǎng)——每日最新資訊28at.com

來(lái)自 langchain UnstructuredImageLoader 的文本提取。Uoj28資訊網(wǎng)——每日最新資訊28at.com

該庫(kù)成功高效地提取了圖像的內(nèi)容。Uoj28資訊網(wǎng)——每日最新資訊28at.com

(2) 從 PDF 中提取文本Uoj28資訊網(wǎng)——每日最新資訊28at.com

以下是從 PDF 中提取內(nèi)容的實(shí)現(xiàn):Uoj28資訊網(wǎng)——每日最新資訊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)

類(lèi)似于 PyPDF 模塊,langchain 模塊能夠生成準(zhǔn)確的結(jié)果,同時(shí)保持原始字體大小。Uoj28資訊網(wǎng)——每日最新資訊28at.com

Uoj28資訊網(wǎng)——每日最新資訊28at.com

從 langchain 的 UnstructuredFileLoader 中提取文本。Uoj28資訊網(wǎng)——每日最新資訊28at.com

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

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 全網(wǎng)最細(xì):Jest+Enzyme測(cè)試React組件(包含交互、DOM、樣式測(cè)試)

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

標(biāo)簽:
  • 熱門(mén)焦點(diǎn)
Top