Skip to content

Commit

Permalink
Added support for uzn files - issue charlesw#66
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesw committed Jan 27, 2014
1 parent 4d31b78 commit 75b6ff6
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.proj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Package" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Version>1.0.9</Version>
<Version>1.0.10</Version>
<SourceDir>$(MSBuildProjectDirectory)\src</SourceDir>
<BuildDir>$(MSBuildProjectDirectory)\bin</BuildDir>
<ReleaseDir>$(MSBuildProjectDirectory)\release</ReleaseDir>
Expand Down
4 changes: 2 additions & 2 deletions src/Tesseract.Tests.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ static void Main(string[] args)
System.Console.WriteLine("Is 64bit process: {0}", Environment.Is64BitProcess);

try {
var testFixture = new Tesseract.Tests.Leptonica.PixTests.ImageManipulationTests();
testFixture.OtsuBinarizationTest();
var testFixture = new Tesseract.Tests.EngineTests();
testFixture.CanParseUznFile();
} catch (Exception e) {
System.Console.WriteLine("Unhandled exception occured: \r\n{0}", e);
}
Expand Down
Binary file added src/Tesseract.Tests/Data/Ocr/uzn-test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/Tesseract.Tests/Data/Ocr/uzn-test.uzn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100 130 200 30 Text
29 changes: 29 additions & 0 deletions src/Tesseract.Tests/EngineTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using System;
using System.IO;
using NUnit.Framework;

namespace Tesseract.Tests
Expand Down Expand Up @@ -52,6 +53,24 @@ public void CanParseText()
}
}

[Test]
public void CanParseUznFile()
{
using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) {
var inputFilename = TestFilePath(@"Ocr\uzn-test.png");
using(var img = Pix.LoadFromFile(inputFilename)) {
using(var page = engine.Process(img, inputFilename, PageSegMode.SingleLine)) {
var text = page.GetText();

const string expectedText =
"This is another test\n\n";

Assert.That(text, Is.EqualTo(expectedText));
}
}
}
}

#region Variable set\get

[Test]
Expand Down Expand Up @@ -128,6 +147,16 @@ public void CanSetStringVariable(string variableName, string variableValue)
}
}

#endregion

#region File Helpers

private string TestFilePath(string path)
{
var basePath = Path.Combine(Environment.CurrentDirectory, "Data");
return Path.Combine(basePath, path);
}

#endregion
}
}
9 changes: 8 additions & 1 deletion src/Tesseract.Tests/Tesseract.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release40|AnyCPU' ">
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug40Client|AnyCPU' ">
</PropertyGroup>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release40Client|AnyCPU' ">
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug35|AnyCPU' ">
Expand Down Expand Up @@ -130,9 +130,16 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Data\Ocr" />
<Folder Include="tessdata" />
</ItemGroup>
<ItemGroup>
<None Include="Data\Ocr\uzn-test.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Data\Ocr\uzn-test.uzn">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
<None Include="tessdata\eng.cube.bigrams">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
5 changes: 4 additions & 1 deletion src/Tesseract.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 2013 for Windows Desktop
# Visual Studio 2012
# SharpDevelop 4.4
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{90E7305F-2B8F-4E99-9667-56945609ABBE}"
Expand All @@ -18,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NuGet", "NuGet", "{8599BCF5
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{D8814461-FB10-489F-83CE-8968987784F5}"
ProjectSection(SolutionItems) = postProject
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tesseract", "Tesseract\Tesseract.csproj", "{DA780CA0-2606-4259-91C4-3F38C5C90592}"
EndProject
Expand Down
3 changes: 3 additions & 0 deletions src/Tesseract/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Version 1.10

* Added support for uzn files - [Issue 66](https://github.com/charlesw/tesseract/issues/66)
4 changes: 4 additions & 0 deletions src/Tesseract/Interop/BaseApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public static string BaseApiGetStringVariable(HandleRef handle, string name)
[DllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPISetImage2")]
public static extern void BaseApiSetImage(HandleRef handle, HandleRef pixHandle);

[DllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPISetInputName")]
public static extern void BaseApiSetInputName(HandleRef handle, string value);


[DllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPISetRectangle")]
public static extern void BaseApiSetRectangle(HandleRef handle, int left, int top, int width, int height);

Expand Down
1 change: 1 addition & 0 deletions src/Tesseract/Tesseract.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<Link>x86\libtesseract302.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="ChangeLog.md" />
</ItemGroup>
<ItemGroup>
<Folder Include="Internal" />
Expand Down
76 changes: 72 additions & 4 deletions src/Tesseract/TesseractEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ private void Initialise(string datapath, string language, EngineMode engineMode)
/// <param name="pageSegMode">The page layout analyasis method to use.</param>
public Page Process(Pix image, PageSegMode? pageSegMode = null)
{
return Process(image, new Rect(0, 0, image.Width, image.Height), pageSegMode);
return Process(image, null, new Rect(0, 0, image.Width, image.Height), pageSegMode);
}

/// <summary>
/// Processes a specified region in the image using the specified page layout analysis mode.
/// </summary>
Expand All @@ -186,6 +186,37 @@ public Page Process(Pix image, PageSegMode? pageSegMode = null)
/// <param name="pageSegMode">The page layout analyasis method to use.</param>
/// <returns>A result iterator</returns>
public Page Process(Pix image, Rect region, PageSegMode? pageSegMode = null)
{
return Process(image, null, new Rect(0, 0, image.Width, image.Height), pageSegMode);
}


/// <summary>
/// Processes the specific image.
/// </summary>
/// <remarks>
/// You can only have one result iterator open at any one time.
/// </remarks>
/// <param name="image">The image to process.</param>
/// <param name="inputName">Sets the input file's name, only needed for training or loading a uzn file.</param>
/// <param name="pageSegMode">The page layout analyasis method to use.</param>
public Page Process(Pix image, string inputName, PageSegMode? pageSegMode = null)
{
return Process(image, inputName, new Rect(0, 0, image.Width, image.Height), pageSegMode);
}

/// <summary>
/// Processes a specified region in the image using the specified page layout analysis mode.
/// </summary>
/// <remarks>
/// You can only have one result iterator open at any one time.
/// </remarks>
/// <param name="image">The image to process.</param>
/// <param name="inputName">Sets the input file's name, only needed for training or loading a uzn file.</param>
/// <param name="region">The image region to process.</param>
/// <param name="pageSegMode">The page layout analyasis method to use.</param>
/// <returns>A result iterator</returns>
public Page Process(Pix image, string inputName, Rect region, PageSegMode? pageSegMode = null)
{
if (image == null) throw new ArgumentNullException("image");
if (region.X1 < 0 || region.Y1 < 0 || region.X2 > image.Width || region.Y2 > image.Height)
Expand All @@ -197,7 +228,9 @@ public Page Process(Pix image, Rect region, PageSegMode? pageSegMode = null)
Interop.TessApi.BaseAPISetPageSegMode(handle, pageSegMode.HasValue ? pageSegMode.Value : DefaultPageSegMode);
Interop.TessApi.BaseApiSetImage(handle, image.Handle);
Interop.TessApi.BaseApiSetRectangle(handle, region.X1, region.Y1, region.Width, region.Height);

if(!String.IsNullOrEmpty(inputName)) {
Interop.TessApi.BaseApiSetInputName(handle, inputName);
}
var page = new Page(this);
page.Disposed += OnIteratorDisposed;
return page;
Expand All @@ -218,6 +251,23 @@ public Page Process(Bitmap image, PageSegMode? pageSegMode = null)
{
return Process(image, new Rect(0, 0, image.Width, image.Height), pageSegMode);
}

/// <summary>
/// Process the specified bitmap image.
/// </summary>
/// <remarks>
/// Please consider <see cref="Process(Pix, String, PageSegMode?)"/> instead. This is because
/// this method must convert the bitmap to a pix for processing which will add additional overhead.
/// Leptonica also supports a large number of image pre-processing functions as well.
/// </remarks>
/// <param name="image">The image to process.</param>
/// <param name="inputName">Sets the input file's name, only needed for training or loading a uzn file.</param>
/// <param name="pageSegMode">The page segmentation mode.</param>
/// <returns></returns>
public Page Process(Bitmap image, string inputName, PageSegMode? pageSegMode = null)
{
return Process(image, inputName, new Rect(0, 0, image.Width, image.Height), pageSegMode);
}

/// <summary>
/// Process the specified bitmap image.
Expand All @@ -232,9 +282,27 @@ public Page Process(Bitmap image, PageSegMode? pageSegMode = null)
/// <param name="pageSegMode">The page segmentation mode.</param>
/// <returns></returns>
public Page Process(Bitmap image, Rect region, PageSegMode? pageSegMode = null)
{
return Process(image, null, region, pageSegMode);
}

/// <summary>
/// Process the specified bitmap image.
/// </summary>
/// <remarks>
/// Please consider <see cref="TesseractEngine.Process(Pix, String, Rect, PageSegMode?)"/> instead. This is because
/// this method must convert the bitmap to a pix for processing which will add additional overhead.
/// Leptonica also supports a large number of image pre-processing functions as well.
/// </remarks>
/// <param name="image">The image to process.</param>
/// <param name="inputName">Sets the input file's name, only needed for training or loading a uzn file.</param>
/// <param name="region">The region of the image to process.</param>
/// <param name="pageSegMode">The page segmentation mode.</param>
/// <returns></returns>
public Page Process(Bitmap image, string inputName, Rect region, PageSegMode? pageSegMode = null)
{
using (var pix = PixConverter.ToPix(image)) {
return Process(pix, region, pageSegMode);
return Process(pix, inputName, region, pageSegMode);
}
}

Expand Down

0 comments on commit 75b6ff6

Please sign in to comment.