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

解鎖 PaddleOCR 的超能力

人工智能 深度學(xué)習(xí)
在本文中,我們將探討如何使用 PaddleOCR,一款基于深度學(xué)習(xí)的先進(jìn)OCR工具包,進(jìn)行文本檢測和識別任務(wù)。

光學(xué)字符識別(OCR)是一項強(qiáng)大的技術(shù),使機(jī)器能夠從圖像或掃描文檔中識別和提取文本。OCR 在各個領(lǐng)域都有應(yīng)用,包括文件數(shù)字化、從圖像中提取文本以及基于文本的數(shù)據(jù)分析。在本文中,我們將探討如何使用 PaddleOCR,一款基于深度學(xué)習(xí)的先進(jìn)OCR工具包,進(jìn)行文本檢測和識別任務(wù)。我們將逐步演示一個代碼片段,展示了整個過程。

一、先決條件

在我們深入代碼之前,讓我們確保我們已經(jīng)準(zhǔn)備好運(yùn)行 PaddleOCR 庫。確保您的計算機(jī)上安裝了以下必要先決條件:

  • Python(3.6 或更高版本)
  • PaddleOCR 庫
  • 其他必要的依賴項(例如 NumPy、pandas 等)

您可以使用以下 pip 命令安裝 PaddleOCR:

pip install paddleocr

二、設(shè)置 PaddleOCR

一旦您安裝了 Python 和所需的庫,我們來設(shè)置 PaddleOCR。您可以使用 PaddleOCR 的預(yù)訓(xùn)練模型,這些模型可用于文本檢測和識別。

使用 PaddleOCR 進(jìn)行文本檢測和識別的代碼片段包括以下主要組件:

  • 圖像預(yù)處理:加載輸入圖像并執(zhí)行必要的預(yù)處理步驟,例如調(diào)整大小或歸一化。
  • 文本檢測:使用 PaddleOCR 文本檢測模型來定位輸入圖像中文本區(qū)域的邊界框。
  • 文本識別:對于每個檢測到的邊界框,使用 PaddleOCR 文本識別模型來提取相應(yīng)的文本。
  • 后處理:整理檢測到的文本和識別結(jié)果以進(jìn)行進(jìn)一步分析或顯示。

三、逐步實現(xiàn)

讓我們分解代碼片段,詳細(xì)解釋每個步驟:

1.文本檢測

該代碼是一個名為 DecMain 的類的一部分,該類專為使用真實數(shù)據(jù)進(jìn)行光學(xué)字符識別(OCR)評估而設(shè)計。它使用 PaddleOCR 從圖像中提取文本,然后計算指標(biāo)(如準(zhǔn)確率、召回率和字符錯誤率 [CER])來評估 OCR 系統(tǒng)的性能。

class DecMain:
    def __init__(self, image_folder_path, label_file_path, output_file):
        self.image_folder_path = image_folder_path
        self.label_file_path = label_file_path
        self.output_file = output_file

    def run_dec(self):
        # Check and update the ground truth file
        CheckAndUpdateGroundTruth(self.label_file_path).check_and_update_ground_truth_file()

        df = OcrToDf(image_folder=self.image_folder_path, label_file=self.label_file_path, det=True, rec=True, cls=False).ocr_to_df()

        ground_truth_data = ReadGroundTruthFile(self.label_file_path).read_ground_truth_file()

        # Get the extracted text as a list of dictionaries (representing the OCR results)
        ocr_results = df.to_dict(orient="records")

        # Calculate precision, recall, and CER
        precision, recall, total_samples = CalculateMetrics(ground_truth_data, ocr_results).calculate_precision_recall()

        CreateSheet(dataframe=df, precision=precision, recall=recall, total_samples=total_samples,
                    file_name=self.output_file).create_sheet()

讓我們分解代碼并解釋每個部分:

class DecMain:

def __init__(self, image_folder_path, label_file_path, output_file):

self.image_folder_path = image_folder_path

self.label_file_path = label_file_path

self.output_file = output_file

DecMain 類有一個 __init__方法,用以下參數(shù)初始化對象:

  • image_folder_path:用于 OCR 的輸入圖像所在文件夾的路徑。
  • label_file_path:包含圖像的實際文本內(nèi)容的真實標(biāo)簽文件的路徑。
  • output_file:評估結(jié)果將保存在的輸出文件的文件名。
def run_dec(self):
       # Check and update the ground truth file
       CheckAndUpdateGroundTruth(self.label_file_path).check_and_update_ground_truth_file()

run_dec方法負(fù)責(zé)運(yùn)行 OCR 評估過程。首先,它使用 CheckAndUpdateGroundTruth 類來檢查并更新真實標(biāo)簽文件。

df = OcrToDf(image_folder=self.image_folder_path, label_file=self.label_file_path, det=True, rec=True, cls=False).ocr_to_df()

OcrToDf 類用于將 OCR 結(jié)果轉(zhuǎn)換為 pandas DataFrame(df)。它接受以下參數(shù):

  • image_folder:包含 OCR 輸入圖像的文件夾的路徑。
  • label_file:真實標(biāo)簽文件的路徑。
  • det=True和 rec=True參數(shù)表示 DataFrame 將包含文本檢測和識別結(jié)果。

ground_truth_data = ReadGroundTruthFile(self.label_file_path).read_ground_truth_file()

ReadGroundTruthFile 類用于讀取真實標(biāo)簽文件并將其內(nèi)容加載到 ground_truth_data變量中。

# Get the extracted text as a list of dictionaries (representing the OCR results)
        ocr_results = df.to_dict(orient="records")

從 DataFrame df 中獲取的 OCR 結(jié)果轉(zhuǎn)換為字典列表(ocr_results),每個字典代表單個圖像的 OCR 結(jié)果。

# Calculate precision, recall, and CER
        precision, recall, total_samples = CalculateMetrics(groun
        d_truth_data, ocr_results).calculate_precision_recall()

CalculateMetrics 類用于計算 OCR 評估指標(biāo):準(zhǔn)確率、召回率和評估的總樣本數(shù)。該類將真實數(shù)據(jù)和 OCR 結(jié)果作為輸入。

CreateSheet(dataframe=df, precision=precision, recall=recall, total_samples=total_samples,

                   file_name=self.output_file).create_sheet()

CreateSheet 類負(fù)責(zé)創(chuàng)建輸出表格(例如 Excel 或 CSV),其中包含評估指標(biāo)和 OCR 結(jié)果。它接受 DataFrame df、準(zhǔn)確率、召回率、總樣本數(shù)和輸出文件名作為輸入。

總的來說,DecMain 類提供了一種有條理的方式,使用真實數(shù)據(jù)和 PaddleOCR 的文本檢測和識別功能來評估 OCR 模型的性能。它計算重要的評估指標(biāo),并將結(jié)果存儲在指定的輸出文件中,以供進(jìn)一步分析。

2.注意:真實標(biāo)簽文件的格式

要使用 DecMain 類和提供的代碼進(jìn)行 OCR 評估,必須正確格式化真實標(biāo)簽文件。真實標(biāo)簽文件應(yīng)采用 JSON 格式,其結(jié)構(gòu)如下所示:

image_name.jpg [{"transcription": "215mm 18", "points": [[199, 6], [357, 6], [357, 33], [199, 33]], "difficult": False, "key_cls": "digits"}, {"transcription": "XZE SA", "points": [[15, 6], [140, 6], [140, 36], [15, 36]], "difficult": False, "key_cls": "text"}]

真實標(biāo)簽文件應(yīng)為 JSON 格式。文件的每一行代表圖像的 OCR 真實標(biāo)簽。

每一行包含圖像的文件名,后跟 JSON 對象形式的該圖像的 OCR 結(jié)果。

JSON 對象應(yīng)具有以下幾點(diǎn):

  • "transcription":圖像的真實文本轉(zhuǎn)錄。
  • "points":表示圖像中文本區(qū)域邊界框坐標(biāo)的四個點(diǎn)的列表。
  • "difficult":一個布爾值,指示文本區(qū)域是否難以識別。
  • "key_cls":OCR 結(jié)果的類別標(biāo)簽,例如 "digits" 或 "text"。

在創(chuàng)建用于準(zhǔn)確評估 OCR 模型性能的真實標(biāo)簽文件時,請確保遵循此格式。

3.文本識別

代碼定義了一個名為 RecMain 的類,該類旨在使用預(yù)訓(xùn)練的 OCR 模型在圖像文件夾上運(yùn)行文本識別(OCR)并生成一個評估 Excel 表格。

class RecMain:
    def __init__(self, image_folder, rec_file, output_file):
        self.image_folder = image_folder
        self.rec_file = rec_file
        self.output_file = output_file

    def run_rec(self):
        image_paths = GetImagePathsFromFolder(self.image_folder, self.rec_file). \
            get_image_paths_from_folder()

        ocr_model = LoadRecModel().load_model()

        results = ProcessImages(ocr=ocr_model, image_paths=image_paths).process_images()

        ground_truth_data = ConvertTextToDict(self.rec_file).convert_txt_to_dict()

        model_predictions, ground_truth_texts, image_names, precision, recall, \
            overall_model_precision, overall_model_recall, cer_data_list = EvaluateRecModel(results,
                                                                                            ground_truth_data).evaluate_model()

        # Create Excel sheet
        CreateMetricExcel(image_names, model_predictions, ground_truth_texts,
                          precision, recall, cer_data_list, overall_model_precision, overall_model_recall,
                          self.output_file).create_excel_sheet()

讓我們分解代碼并解釋每個部分:

class RecMain:
    def __init__(self, image_folder, rec_file, output_file):
        self.image_folder = image_folder
        self.rec_file = rec_file
        self.output_file = output_file

RecMain類有一個__init__方法,用以下參數(shù)初始化對象:

  • image_folder: 包含用于文本識別的輸入圖像的文件夾路徑。
  • rec_file: 包含圖像實際文本內(nèi)容的地面真實標(biāo)簽文件的路徑。
  • output_file: 保存評估結(jié)果的輸出Excel表格的文件名。
def run_rec(self):
        image_paths = GetImagePathsFromFolder(self.image_folder, self.rec_file).get_image_paths_from_folder()

run_rec方法負(fù)責(zé)運(yùn)行文本識別過程。它首先使用GetImagePathsFromFolder類來獲取指定image_folder內(nèi)所有圖像的圖像路徑列表。這一步確保OCR模型將處理給定目錄內(nèi)的所有圖像。

ocr_model = LoadRecModel().load_model()

LoadRecModel類用于加載用于文本識別的預(yù)訓(xùn)練OCR模型。它可能使用PaddleOCR或其他OCR庫來加載模型。

results = ProcessImages(ocr=ocr_model, image_paths=image_paths).process_images()

ProcessImages類負(fù)責(zé)使用加載的OCR模型來處理圖像。它以O(shè)CR模型(ocr_model)和圖像路徑列表(image_paths)作為輸入。

ground_truth_data = ConvertTextToDict(self.rec_file).convert_txt_to_dict()

ConvertTextToDict類用于讀取地面實況標(biāo)簽文件并將其轉(zhuǎn)換為字典格式(ground_truth_data)。這一轉(zhuǎn)換準(zhǔn)備了地面實況數(shù)據(jù),以便與OCR模型的預(yù)測進(jìn)行比較。

model_predictions, ground_truth_texts, image_names, precision, recall, \
            overall_model_precision, overall_model_recall, cer_data_list = EvaluateRecModel(results,
                                                                                            ground_truth_data).evaluate_model()

EvaluateRecModel類負(fù)責(zé)將OCR模型的預(yù)測與地面實況數(shù)據(jù)進(jìn)行比較,并計算評估指標(biāo),如精度、召回率和字符錯誤率(CER)。它以O(shè)CR模型的預(yù)測(results)和地面實況數(shù)據(jù)(ground_truth_data)作為輸入。

# Create Excel sheet
        CreateMetricExcel(image_names, model_predictions, ground_truth_texts,
                          precision, recall, cer_data_list, overall_model_precision, overall_model_recall,
                          self.output_file).create_excel_sheet()

CreateMetricExcel類負(fù)責(zé)創(chuàng)建包含評估指標(biāo)和OCR結(jié)果的輸出Excel表。它接受各種輸入數(shù)據(jù),包括圖像名稱、模型預(yù)測、地面實況文本、評估指標(biāo)和輸出文件名(self.output_file)。

總之,RecMain類組織了整個文本識別過程,從加載OCR模型到生成包含詳細(xì)指標(biāo)的評估Excel表。它提供了一種有組織和可重復(fù)使用的方法,用于評估OCR模型在給定一組圖像上的性能。

注:地面實況文本文件格式

使用RecMain類和提供的代碼進(jìn)行OCR評估時,正確格式化地面實況(GT)文本文件至關(guān)重要。GT文本文件應(yīng)采用以下格式:

image_name.jpg text

文件的每一行表示一個圖像的GT文本。

每一行包含圖像的文件名,后跟一個制表符(\t),然后是該圖像的GT文本。

確保GT文本文件包含圖像文件夾中指定的所有圖像的GT文本條目。GT文本應(yīng)與圖像中實際文本內(nèi)容相匹配。這種格式對于準(zhǔn)確評估OCR模型的性能是必需的。

您可以在這里找到源代碼:https://github.com/vinodbaste/paddleOCR_rec_dec?source=post_page

結(jié)論

我們探討了如何使用基于深度學(xué)習(xí)的PaddleOCR進(jìn)行文本檢測和識別的過程。我們逐步演示了文本檢測和識別的實現(xiàn)。有了PaddleOCR強(qiáng)大的預(yù)訓(xùn)練模型和易于使用的API,對圖像執(zhí)行OCR變得更加容易。

責(zé)任編輯:趙寧寧 來源: 小白玩轉(zhuǎn)Python
相關(guān)推薦

2011-02-22 17:48:34

Konqueror

2015-03-13 11:23:21

編程編程超能力編程能力

2024-03-14 08:28:45

2024-05-15 16:07:03

Python框架

2021-08-03 21:24:13

ARVR

2023-12-22 14:31:52

2024-11-26 00:41:23

Python編程腳本

2013-12-02 10:30:29

瀏覽器

2024-11-04 19:46:38

2019-02-28 22:10:30

AI人工智能預(yù)測

2016-12-01 09:32:47

AWS re:InveAWS云計算超能力

2020-11-03 20:44:35

快手實時隱身技術(shù)隱身超能力

2019-05-08 14:19:19

貝斯平BespinMSP

2021-03-11 11:00:38

IBM自動化AI

2013-03-11 13:35:26

腕帶

2017-08-22 11:06:22

Android谷歌

2019-03-28 09:26:26

數(shù)據(jù)科學(xué)模型機(jī)器學(xué)習(xí)

2020-08-16 08:30:33

PythonExcel集成

2022-01-06 15:35:31

LinuxWindows英特爾

2025-04-10 08:33:05

點(diǎn)贊
收藏

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