Saltar al pie de página
USANDO IRONOCR

Cómo realizar OCR en subtítulos en C# (Tutorial)

In this tutorial, we will learn about extracting hardcoded subtitles from video files. We will take a sample video file and extract the hardcoded subtitles into a text file. We will develop a C# .NET program that will extract the hardcoded subtitles using the OCR Process. I will keep this tutorial simple and easy so that even a beginner C# Programmer can understand it.

We need an efficient Optical Character Recognition (OCR) engine that can process the video and get subtitle files irrespective of the subtitle language.

There are many libraries available that provide OCR results. Some of them are paid, some of them are difficult to use, and some of them are not efficient or accurate, so it is very difficult to find a library that is free, efficient, easy to use, and provides accurate results.

IronOCR, which is free for development, provides a one-month free trial for commercial purposes. It supports over 150 languages and provides better accuracy than most other OCR libraries available. It is also efficient and easy to use. We will use this library for our demonstration.

IronOCR

IronOCR is a library developed and maintained by Iron Software that helps C# Software Engineers perform OCR, Barcode Scanning, and Text Extraction in .NET projects.

The features of IronOCR include:

  • Reading text from many formats such as images (JPEG, PNG, BMP), GIF, TIF/TIFF, Streams, and PDFs
  • Correction of low-quality scans and photos with a plethora of filters such as Deskew, Denoise, Binarize, Enhance Resolution, Dilate, and many more
  • Reading barcodes from over 20 different formats, along with QR Code Support
  • Utilizing the latest build of Tesseract OCR, with its performance tuned above and beyond other libraries of its kind
  • Exporting Searchable PDFs, hOCR / HTML Exporting, and image content text.

Let's develop a demo application to read license plate numbers.

Create a Visual Studio Project

The first step is to create a new project.

Open Visual Studio. Click on Create New Project, and select the Console Application project template.

Click on the Next button, and name the project (I have named it "OCR Subtitles", you can name it as per your choice).

Click on the Next button, and select your target Framework. Finally, Click on the Create button to create the project.

The project will be created as shown below.

How to OCR Subtitles in C# (Tutorial), Figure 1: Creating a New Project in Visual Studio Creating a New Project in Visual Studio

Now, we need to install the IronOCR library to use it in our project. The easiest way is to install it via NuGet Package Manager for Solution.

Install IronOCR NuGet Package

Click on Tools from the top menu bar, and select NuGet Package Manager > Manage NuGet Packages for Solution, as shown below.

How to OCR Subtitles in C# (Tutorial), Figure 2: Installing IronOCR within Visual Studio Installing IronOCR within Visual Studio

The following window will appear.

How to OCR Subtitles in C# (Tutorial), Figure 3: Visual Studio NuGet Package Manager UI Visual Studio NuGet Package Manager UI

Click on browse, and search for IronOCR. Select IronOCR Package and click on the Install button, as shown below.

How to OCR Subtitles in C# (Tutorial), Figure 4: Searching for IronOCR in the NuGet Package Manager UI Searching for IronOCR in the NuGet Package Manager UI

The IronOCR Library will be installed and ready to use.

Extract Hardcoded Subtitles

Let's write a program to extract hardcoded subtitles.

We are going to use the following screenshot for extracting subtitles.

How to OCR Subtitles in C# (Tutorial), Figure 5: Sample video screenshot from which text will be extracted Sample video screenshot from which text will be extracted

Add the following namespace:

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

Write the following code below the namespace declaration.

// 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

The code above works as follows:

  1. Initialize IronTesseract object. It will create a default instance of IronTesseract.
  2. Create a new OcrInput object populated with an input image file or PDF document. OcrInput is the preferred input type because it allows for OCR of multi-paged documents, and allows images to be enhanced before OCR to obtain faster, more accurate results.
  3. Read a text from an OCR Input Object and return an OCR Result object. ocr.Read will extract subtitles from the given input screenshot.
  4. result.Text will return the entire content extracted from the given input.

The sample program produces the console output below:

How to OCR Subtitles in C# (Tutorial), Figure 7: Console output generated from performing text extraction on the sample image using IronOCR Console output generated from performing text extraction on the sample image using IronOCR

Let's suppose that you have a video frame that contains both the title of the video and the subtitles:

How to OCR Subtitles in C# (Tutorial), Figure 6: A single frame of a longer video containing text regions for the video title and the video subtitles A single frame of a longer video containing text regions for the video title and the video subtitles

Our goal is to extract the hardcoded subtitles from the bottom region of the image. In this case, we need to specify the text region in which the subtitle is displayed.

Specify Subtitle Location in the Frame

We can use a System.Drawing.Rectangle to specify a region in which we will read a subtitle from the video frame. The unit of measurement is always pixels.

We will use the following sample code to specify the text region.

// 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

This yields a 41% speed increase - and allows us to be specific. In contentArea, we have specified the start point in x and y, and then the height and width of the required subtitle region.

Save Subtitle into a Subtitle Text File

Let's save the extracted subtitles into a text file.

// 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 will take the output path as an argument, and save the file in the given path.

How to OCR Subtitles in C# (Tutorial), Figure 8: A single frame of a longer video containing text regions for the video title and the video subtitles A single frame of a longer video containing text regions for the video title and the video subtitles

Summary

In this tutorial, we have learned to use IronOCR and develop a very simple program to read subtitles from the video screenshot. We can also specify the region for which we want to extract the text.

IronOCR provides the features of OpenCV for Computer Vision. We have seen that IronOCR enables us to read text from blurred or low-resolution images. This library is efficient and provides accuracy. It supports 125+ languages with full accuracy. It's free for development and has no restriction on production.

In summary, IronOCR provides:

  • The ability to scan and read images and scanned documents
  • Support for 150+ global languages
  • Output as text, structured data, or searchable PDFs
  • Supports .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 scraping content from websites. Purchase the complete Iron Suite for the price of two individual libraries.

Preguntas Frecuentes

¿Cómo puedo extraer subtítulos incrustados de archivos de video en C#?

Puedes extraer subtítulos incrustados de archivos de video en C# usando IronOCR. Instala la biblioteca a través del Administrador de Paquetes NuGet y úsala para procesar cuadros de video y extraer texto.

¿Cuál es la ventaja de usar IronOCR sobre Tesseract para la extracción de subtítulos?

IronOCR proporciona una alternativa mejorada a Tesseract con mayor precisión, facilidad de uso y soporte para más de 150 idiomas, lo que lo hace adecuado para extraer subtítulos de videos.

¿Cómo especificas las ubicaciones de subtítulos en IronOCR para mejorar la velocidad de procesamiento?

Puedes especificar las ubicaciones de subtítulos en IronOCR usando un System.Drawing.Rectangle para enfocarte en la región de interés, lo que puede mejorar la velocidad de procesamiento hasta en un 41%.

¿Se puede usar IronOCR para idiomas distintos al inglés al extraer subtítulos?

Sí, IronOCR soporta más de 150 idiomas, lo que le permite extraer subtítulos de videos en varios idiomas con precisión.

¿Cuáles son los requisitos previos para seguir el tutorial de OCR de subtítulos en C#?

El tutorial requiere conocimientos básicos de programación en C# y la capacidad de usar Visual Studio para instalar la biblioteca IronOCR a través del Administrador de Paquetes NuGet.

¿Cómo maneja IronOCR cuadros de video de baja calidad?

IronOCR incluye funciones para corregir escaneos de baja calidad, mejorando la precisión de la extracción de texto de cuadros de video subóptimos.

¿Qué formatos de salida están disponibles después de extraer subtítulos con IronOCR?

Los subtítulos extraídos se pueden guardar como archivos de texto, datos estructurados o PDFs buscables usando IronOCR.

¿Hay algún costo asociado con el uso de IronOCR para proyectos comerciales?

IronOCR es gratis para fines de desarrollo y ofrece un mes de prueba gratuita para proyectos comerciales. Para uso comercial continuo, se requiere una licencia.

¿Puede integrarse IronOCR con otras bibliotecas para funciones adicionales?

Sí, IronOCR puede integrarse con otras bibliotecas de Iron Software para tareas como manipulación de PDF y raspado web, mejorando su funcionalidad.

Kannaopat Udonpant
Ingeniero de Software
Antes de convertirse en Ingeniero de Software, Kannapat completó un doctorado en Recursos Ambientales de la Universidad de Hokkaido en Japón. Mientras perseguía su grado, Kannapat también se convirtió en miembro del Laboratorio de Robótica de Vehículos, que es parte del Departamento de Ingeniería ...
Leer más