跳過到頁腳內容
使用 IRONOCR

如何在 C# 中對字幕進行 OCR(教學)

在本教程中,我們將學習如何從視頻文件中提取硬編碼字幕。 我們將提取示例視頻文件的硬編碼字幕,以開發一個 C# .NET 程序該程序將使用 OCR 流程來提取硬編碼字幕。 我會讓這個教程簡單易懂,使即使是初學者的 C# 程序員也能理解。

我們需要一個高效的光學字符識別 (OCR) 引擎,可以處理視頻並獲取字幕文件而不考慮字幕語言。

有很多可用的庫提供 OCR 結果。 其中一些是付費的,有些難以使用,有些不高效或不準確,因此很難找到一個免費、高效、易用並提供準確結果的庫。

IronOCR 對於開發是免費的,並提供一個月的商業用途免費試用。 它支持超過 150 種語言,並提供比大多數其他可用的 OCR 庫更高的準確性。 它還高效且易於使用。 我們將在演示中使用此庫。

class="hsg-featured-snippet">

在 C# 中如何對字幕進行 OCR

  1. 安裝 C# 庫以對字幕執行 OCR
  2. 將含有字幕的圖像導入到新的 OcrInput 實例中
  3. 通過應用所選過濾器對圖像進行預處理
  4. 指定圖像中的字幕位置以提高 OCR 性能和準確性
  5. 將檢索到的文本導出為文本文件

IronOCR

IronOCR 是由 Iron Software 開發和維護的庫,可幫助 C# 軟件工程師在 .NET 項目中執行 OCR、條形碼掃描和文本提取。

IronOCR 的特點包括:

  • Reading text from many formats such as images (JPEG, PNG, BMP), GIF, TIF/TIFF, Streams, and PDFs
  • 通過眾多過濾器,如糾偏、去噪、二值化、增強分辨率、膨脹等修正低質量掃描和照片
  • 從超過 20 種不同格式讀取條形碼,還有 QR Code 支持
  • 使用最新構建的 Tesseract OCR,其性能優於其他同類庫
  • 導出可搜索的 PDF,hOCR / HTML 導出,和圖像內容文本。

讓我們開發一個用於讀取車牌號碼的示例應用程序。

創建一個Visual Studio項目

第一步是創建一個新項目。

打開 Visual Studio。 點擊創建新項目,並選擇控制台應用項目模板。

點擊下一步按鈕,並為項目命名(我命名為“OCR Subtitles”,您可以根據需要命名)。

點擊下一步按鈕,選擇您的目標框架。 最後,點擊創建按鈕創建項目。

該專案將如下面所示創建。

如何在 C# 中對字幕進行 OCR(教程),圖 1:在 Visual Studio 中創建新項目 在Visual Studio中創建一個新項目

現在,我們需要安裝 IronOCR 庫以在我們的項目中使用它。 最簡單的方法是通過解決方案的 NuGet 包管理器安裝它。

安裝 IronOCR NuGet 包

從頂部菜單欄中點擊 工具,選擇 NuGet 包管理器 > 為解決方案管理 NuGet 包,如下所示。

如何在 C# 中對字幕進行 OCR(教程),圖 2:在 Visual Studio 中安裝 IronOCR 在 Visual Studio 中安裝 IronOCR

以下窗口將出現。

如何在 C# 中對字幕進行 OCR(教程),圖 3:Visual Studio NuGet 包管理器 UI Visual Studio NuGet 包管理器 UI

點擊瀏覽,並搜索 IronOCR。 選擇 IronOCR 包並點擊安裝按鈕,如下所示。

如何在 C# 中對字幕進行 OCR(教程),圖 4:在 NuGet 包管理器 UI 中搜索 IronOCR 在 NuGet 包管理器 UI 中搜索 IronOCR

IronOCR 庫將安裝完成且準備使用。

提取硬編碼字幕

讓我們編寫一個程序來提取硬編碼字幕。

我們將使用以下截圖來提取字幕。

如何在 C# 中對字幕進行 OCR(教程),圖 5:樣本視頻截圖,將從中提取文本 樣本視頻截圖,將從中提取文本

添加以下命名空間:

using IronOcr;
using IronOcr;
Imports IronOcr
$vbLabelText   $csharpLabel

在命名空間聲明下編寫以下代碼。

// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input using the specified image path
using (var input = new OcrInput(@"D:\License Plate\plate3.jpg"))
{
    // Perform OCR on the input image to extract text
    var result = ocr.Read(input);
    // Output the extracted text to the console
    Console.WriteLine(result.Text);
}
// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input using the specified image path
using (var input = new OcrInput(@"D:\License Plate\plate3.jpg"))
{
    // Perform OCR on the input image to extract text
    var result = ocr.Read(input);
    // Output the extracted text to the console
    Console.WriteLine(result.Text);
}
' Initialize IronTesseract object
Dim ocr = New IronTesseract()
' Create an OCR Input using the specified image path
Using input = New OcrInput("D:\License Plate\plate3.jpg")
	' Perform OCR on the input image to extract text
	Dim result = ocr.Read(input)
	' Output the extracted text to the console
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

上面的代碼工作方式如下:

  1. 初始化 IronTesseract 對象。 它將創建一個默認的 IronTesseract 實例。
  2. 創建一個新的 OcrInput 對象,其中包含輸入圖像文件或 PDF 文檔。 OcrInput 是首選的輸入類型,因為它允許對多頁文檔進行 OCR,並允許在進行 OCR 之前增強圖像,以獲得更快、更準確的結果。
  3. 從 OCR 輸入對象中讀取文本並返回 OCR 結果對象。 ocr.Read 將從給定輸入截圖中提取字幕。
  4. result.Text 將返回從給定輸入中提取的全部內容。

範例程序生成如下控制台輸出:

如何在 C# 中對字幕進行 OCR(教程),圖 7:使用 IronOCR 對樣本圖片進行文本提取後生成的控制台輸出 使用 IronOCR 對樣本圖片進行文本提取後生成的控制台輸出

假設您有一個視頻幀,其中包含視頻的標題和字幕:

如何在 C# 中對字幕進行 OCR(教程),圖 6:包含視頻標題和視頻字幕的更長視頻的單幀 包含視頻標題和視頻字幕的更長視頻的單幀

我們的目標是從圖像的底部區域提取硬編碼字幕。 在這種情況下,我們需要指定字幕顯示的文本區域。

在框架中指定字幕位置

我們可以使用 System.Drawing.Rectangle 來指定從視頻幀中讀取字幕的區域。 測量單位始終為像素

我們將使用以下範例代碼指定文本區域。

// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input and specify the region of interest
using (var input = new OcrInput())
{
    // Define the area within the image where subtitles are located for a 41% improvement on speed
    var contentArea = new CropRectangle(x: 189, y: 272, height: 252, width: 77);
    // Add the specific region of the image to the OCR input
    input.AddImage(@"D:\subtitle\image.png", contentArea);
    // Perform OCR on the specified region
    var result = ocr.Read(input);
    // Output the extracted text to the console
    Console.WriteLine(result.Text);
}
// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input and specify the region of interest
using (var input = new OcrInput())
{
    // Define the area within the image where subtitles are located for a 41% improvement on speed
    var contentArea = new CropRectangle(x: 189, y: 272, height: 252, width: 77);
    // Add the specific region of the image to the OCR input
    input.AddImage(@"D:\subtitle\image.png", contentArea);
    // Perform OCR on the specified region
    var result = ocr.Read(input);
    // Output the extracted text to the console
    Console.WriteLine(result.Text);
}
' Initialize IronTesseract object
Dim ocr = New IronTesseract()
' Create an OCR Input and specify the region of interest
Using input = New OcrInput()
	' Define the area within the image where subtitles are located for a 41% improvement on speed
	Dim contentArea = New CropRectangle(x:= 189, y:= 272, height:= 252, width:= 77)
	' Add the specific region of the image to the OCR input
	input.AddImage("D:\subtitle\image.png", contentArea)
	' Perform OCR on the specified region
	Dim result = ocr.Read(input)
	' Output the extracted text to the console
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

此操作能使速度提高41%,並允許我們做到具體。 在 contentArea 中,我們已指定 x 和 y 的起始點,然後是所需字幕區域的高度和寬度。

將字幕保存到字幕文本文件中

我們將提取的字幕保存到文本文件中。

// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input with the specified image path
using (var input = new OcrInput(@"D:\subtitle\subtitle1.png"))
{
    // Perform OCR on the input image to extract text
    var result = ocr.Read(input);
    // Save the extracted text to a specified file path
    result.SaveAsTextFile(@"D:\subtitle\subtitlefile.txt");
}
// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input with the specified image path
using (var input = new OcrInput(@"D:\subtitle\subtitle1.png"))
{
    // Perform OCR on the input image to extract text
    var result = ocr.Read(input);
    // Save the extracted text to a specified file path
    result.SaveAsTextFile(@"D:\subtitle\subtitlefile.txt");
}
' Initialize IronTesseract object
Dim ocr = New IronTesseract()
' Create an OCR Input with the specified image path
Using input = New OcrInput("D:\subtitle\subtitle1.png")
	' Perform OCR on the input image to extract text
	Dim result = ocr.Read(input)
	' Save the extracted text to a specified file path
	result.SaveAsTextFile("D:\subtitle\subtitlefile.txt")
End Using
$vbLabelText   $csharpLabel

result.SaveAsTextFile 將接收輸出路徑作為參數,並將文件保存到給定路徑。

如何在 C# 中對字幕進行 OCR(教程),圖 8:包含視頻標題和視頻字幕的更長視頻的單幀 包含視頻標題和視頻字幕的更長視頻的單幀

總結

在本教程中,我們學習了使用 IronOCR 並開發一個非常簡單的程序來從視頻截圖中讀取字幕。 我們還可以指定區域以提取所需的文本。

IronOCR provides the features of OpenCV for Computer Vision. 我們已見證 IronOCR 使我們能夠從模糊或低分辨率圖像中讀取文本。 此庫高效且準確。 它支持 125 多種語言,並具有完整的準確性。 它對於開發是免費的,對生產沒有任何限制。

總而言之,IronOCR 提供:

  • 掃描和閱讀圖像及掃描文檔的能力
  • 支持150 多種全球語言
  • 將輸出作為文本、結構化數據或可搜索的 PDF
  • 支持 .NET 6, 5, Core, Standard, Framework

IronOCR is part of Iron Software's suite of libraries useful for reading and writing PDFs, manipulating Excel files, reading text from images, and 從網站抓取內容。 Purchase the complete Iron Suite for the price of two individual libraries.

常見問題解答

如何在 C# 中從視頻文件中提取硬編碼字幕?

您可以使用 IronOCR 在 C# 中從視頻文件中提取硬編碼字幕。通過 NuGet 套件管理器安裝該庫,然後使用它處理視頻框架並提取文本。

使用 IronOCR 提取字幕相比於使用 Tesseract 有什麼優勢?

IronOCR 提供了一種升級替代 Tesseract 的方法,具有更高的準確性、易用性和支持超過 150 種語言的能力,使其非常適合從視頻中提取字幕。

如何在 IronOCR 中指定字幕位置以提高處理速度?

您可以使用 System.Drawing.Rectangle 指定 IronOCR 中的字幕位置,以集中在感興趣的區域,這可以將處理速度提高多達 41%。

IronOCR 可以用於提取非英文語言的字幕嗎?

是的,IronOCR 支持超過 150 種語言,使其能夠準確地從多語言視頻中提取字幕。

哪些先決條件適用於 C# 字幕 OCR 教程?

本教程需要基本的 C# 編程知識,以及使用 Visual Studio 通過 NuGet 套件管理器安裝 IronOCR 庫的能力。

IronOCR 如何處理低質量的視頻幀?

IronOCR 包含糾正低質量掃描的功能,提高了從次佳視頻幀中提取文字的準確性。

在用 IronOCR 提取字幕後有哪些輸出格式可用?

提取的字幕可以使用 IronOCR 保存為文本文件、結構化數據或可搜索的 PDF。

使用 IronOCR 用於商業項目是否有成本?

IronOCR 可免費用於開發目的,並提供一個月的商業項目免費試用。持續商業用途需獲購許可。

IronOCR 可以與其他庫集成以獲得額外的功能嗎?

是的,IronOCR 可以與其他 Iron Software 庫集成,以完成例如 PDF 操作和網絡抓取等任務,增強其功能。

Kannaopat Udonpant
軟體工程師
在成為软件工程師之前,Kannapat 從日本北海道大學完成了環境資源博士學位。在追逐學位期间,Kannapat 還成為了生產工程系一部份——汽車机器人实验室的成員。2022 年,他利用他的 C# 技能加入 Iron Software 的工程團隊, 專注於 IronPDF。Kannapat 珍惜他的工作,因为他直接向编写大部分 IronPDF 使用的代码的开发者学习。除了同行学习,Kannapat 还喜欢在 Iron Software 工作的社交十环。当他不编写代码或文档时,Kannapat 通常在他的 PS5 上打游戏或重看《The Last of Us》。