跳至頁尾內容
USING IRONOCR

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

在本教程中,我們將學習如何從影片文件中提取硬編字幕。 我們將採用一個範例影片文件並將硬編字幕提取到一個文字文件。我們將開發一個C# .NET程式,利用OCR過程提取硬編字幕。 我將保持這個教程簡單易懂,以便即使是初學者的C#程式設計師也能理解。

我們需要一個高效的光學字元識別 (OCR) 引擎,可以處理影片並獲取字幕文件而無需介意字幕語言。

有很多可用的程式庫可以提供OCR結果。 其中一些是付費的,一些使用困難,而有些效率或準確性不佳,因此找到一個免費、高效、易於使用且提供準確結果的程式庫非常困難。

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

IronOCR

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

IronOCR的功能包括:

  • 從多種格式中讀取文字,如圖像(JPEG、PNG、BMP)、GIFTIF/TIFF、流和PDFs
  • 使用一系列濾鏡(如Deskew、Denoise、Binarize、增強解析度、膨脹等)校正低質量掃描和照片
  • 從超過20種不同格式中讀取條形碼,並支持QR碼支持
  • 利用最新版本的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提供了OpenCV計算機視覺功能。 我們已經看到IronOCR能夠讓我們從模糊或低解析度的圖像中讀取文字。 此程式庫高效並提供準確性。 它支持125多種語言,且完全準確。 開發時免費,生產時無限制。

總而言之,IronOCR提供:

  • 掃描和讀取圖像和掃描文件的能力
  • 支持150多種全球語言
  • 以文字、結構化資料或可搜索的PDF格式輸出
  • 支援.NET 6、5、Core、Standard、Framework

IronOCR是Iron Software的程式庫套件的一部分,對讀取和寫入PDFs操作Excel文件從圖像讀取文字以及從網站中提取內容非常有用。 購買完整的Iron Suite價格相當於兩個單獨的程式庫。

常見問題

如何在 C# 中從影片文件中提取硬字幕?

您可以使用 IronOCR 在 C# 中從影片文件中提取硬字幕。通過 NuGet 套件管理器安裝該程式庫,然後使用它來處理影片幀並提取文字。

using IronOCR 提取字幕的優勢是什麼?

IronOCR 提供升級版的 Tesseract 替代方案,具有增強的準確性、易用性和對 150 多種語言的支持,非常適合從影片中提取字幕。

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

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

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

可以,IronOCR 支援超過 150 種語言,能夠準確地從多語言影片中提取字幕。

遵循 C# 字幕OCR教學的先決條件是什麼?

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

IronOCR 如何處理低品質影片幀?

IronOCR 包含矯正低品質掃描的功能,提高從次優影片幀中提取文字的準確性。

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

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

using 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。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話