Skip to content

Commit

Permalink
Tesseract5 support (Tentacule#37)
Browse files Browse the repository at this point in the history
* added tesseract 5 support

* added tesseract version log

* set tesseract version 4 as default for non windows platform

* fixed default tesseract version

* updated doc
  • Loading branch information
Tentacule committed Sep 4, 2023
1 parent d5de23e commit e6f6d88
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dotnet PgsToSrt.dll [parameters]
| `--trackLanguage` | Convert all tracks of the specified language (only works with `.mkv` input) |
| `--tesseractlanguage` | Tesseract language to use if multiple languages are available in the tesseract data directory. |
| `--tesseractdata` | Path of tesseract language data files, by default `tessdata` in the executable directory. |
| `--tesseractversion` | libtesseract version, support 4 and 5 (default: 4) (ignored on Windows platform) |

## Example (Command Line)

Expand Down
6 changes: 5 additions & 1 deletion src/PgsToSrt/CommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CommandLine;
using CommandLine;

namespace PgsToSrt
{
Expand All @@ -21,5 +21,9 @@ internal class CommandLineOptions

[Option(HelpText = "Path of tesseract language data files, by default 'tessdata' in the executable directory.")]
public string TesseractData { get; set; }

[Option(HelpText = $"Tesseract version", Default = Runner.DefaultTesseractVersion)]
public string TesseractVersion { get; set; }

}
}
10 changes: 7 additions & 3 deletions src/PgsToSrt/PgsOcr.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Nikse.SubtitleEdit.Core;
using Nikse.SubtitleEdit.Core.BluRaySup;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
Expand All @@ -16,14 +16,17 @@ public class PgsOcr
{
private readonly Microsoft.Extensions.Logging.ILogger _logger;
private readonly Subtitle _subtitle = new Subtitle();
private readonly string _tesseractVersion;

private List<BluRaySupParser.PcsData> _bluraySubtitles;

public string TesseractDataPath { get; set; }
public string TesseractLanguage { get; set; } = "eng";

public PgsOcr(Microsoft.Extensions.Logging.ILogger logger)
public PgsOcr(Microsoft.Extensions.Logging.ILogger logger, string tesseractVersion)
{
_logger = logger;
_tesseractVersion = tesseractVersion;
}

public bool ToSrt(List<BluRaySupParser.PcsData> subtitles, string outputFileName)
Expand Down Expand Up @@ -57,8 +60,9 @@ private void Save(string outputFileName)
private bool DoOcr()
{
_logger.LogInformation($"Starting OCR for {_bluraySubtitles.Count} items...");
_logger.LogInformation($"Tesseract verion {_tesseractVersion}");

var exception = TesseractApi.Initialize();
var exception = TesseractApi.Initialize(_tesseractVersion);
if (exception != null)
{
_logger.LogError(exception, $"Failed: {exception.Message}");
Expand Down
58 changes: 31 additions & 27 deletions src/PgsToSrt/PgsToSrt.csproj
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<AssemblyVersion>1.4.2.0</AssemblyVersion>
<FileVersion>1.4.2.0</FileVersion>
<Version>1.4.2</Version>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<AssemblyVersion>1.4.3.0</AssemblyVersion>
<FileVersion>1.4.3.0</FileVersion>
<Version>1.4.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.0-preview1" />
<PackageReference Include="libse" Version="3.5.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
<PackageReference Include="NLog" Version="4.7.5" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.5" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0010" />
<PackageReference Include="Tesseract" Version="4.1.1" />
<PackageReference Include="Tncl.NativeLoader" Version="1.0.6" />
</ItemGroup>
<PropertyGroup>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
<None Update="NLog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.0-preview1" />
<PackageReference Include="libse" Version="3.5.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
<PackageReference Include="NLog" Version="4.7.5" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.5" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0010" />
<PackageReference Include="Tesseract" Version="5.2.0" />
<PackageReference Include="Tncl.NativeLoader" Version="1.0.6" />
</ItemGroup>

<ItemGroup>
<None Update="NLog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
32 changes: 29 additions & 3 deletions src/PgsToSrt/Runner.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
using CommandLine;
using CommandLine;
using Microsoft.Extensions.Logging;
using PgsToSrt.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;

namespace PgsToSrt
{
internal class Runner
{
public const string DefaultTesseractVersion = "4";

private readonly string[] _tesseractSupportedVersions = new[] { "4", "5" };
private readonly ILogger _logger;

private string _tesseractData;
private string _tesseractLanguage;
private string _tesseractVersion = DefaultTesseractVersion;

public Runner(ILogger<Runner> logger)
{
Expand Down Expand Up @@ -44,6 +50,26 @@ public void Run(Parsed<CommandLineOptions> values)
var trackLanguage = values.Value.TrackLanguage;
var track = values.Value.Track;

// Windows uses tesseract50.dll installed by nuget package, so always use v5
// Other systems can uses different libtesseract versions, keep v4 as default.
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (values.Value.TesseractVersion != null)
{
_tesseractVersion = values.Value.TesseractVersion;

if (!_tesseractSupportedVersions.Contains(_tesseractVersion))
{
_logger.LogError($"Unsupported Tesseract version '{_tesseractVersion}' (Supported versions: 4, 5)");
result = false;
}
}
}
else
{
_tesseractVersion = "5";
}

_tesseractData = !string.IsNullOrEmpty(values.Value.TesseractData)
? values.Value.TesseractData
: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tessdata");
Expand Down Expand Up @@ -107,7 +133,7 @@ private bool ConvertPgs(string input, int? track, string output)
if (subtitles is null)
return false;

var pgsOcr = new PgsOcr(_logger)
var pgsOcr = new PgsOcr(_logger, _tesseractVersion)
{
TesseractDataPath = _tesseractData,
TesseractLanguage = _tesseractLanguage
Expand All @@ -116,6 +142,6 @@ private bool ConvertPgs(string input, int? track, string output)
pgsOcr.ToSrt(subtitles, output);

return true;
}
}
}
}
8 changes: 4 additions & 4 deletions src/PgsToSrt/TesseractApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand All @@ -13,15 +13,15 @@ static class TesseractApi
{
private const string _assemblyName = "Assembly.Tesseract";

public static Exception Initialize()
public static Exception Initialize(string tesseractVersion)
{
Exception exception = null;

var tessApiType = typeof(Tesseract.Page).Assembly.GetType("Tesseract.Interop.TessApi");
var leptApiType = typeof(Tesseract.Page).Assembly.GetType("Tesseract.Interop.LeptonicaApi");

var tessApiCustomType = CreateInterfaceType<ITessApiSignatures>("tesseract41", "tesseract", "4");
var leptApiCustomType = CreateInterfaceType<ILeptonicaApiSignatures>("leptonica-1.80.0", "lept", "5");
var tessApiCustomType = CreateInterfaceType<ITessApiSignatures>("tesseract50", "tesseract", tesseractVersion);
var leptApiCustomType = CreateInterfaceType<ILeptonicaApiSignatures>("leptonica-1.82.0", "lept", "5");

var loader = new NativeLoader();
loader.WindowsOptions.UseSetDllDirectory = true;
Expand Down

0 comments on commit e6f6d88

Please sign in to comment.