Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for a timeout to be set for the ocr process #292

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Tesseract/Interop/BaseApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ int BaseApiInit(HandleRef handle, string datapath, string language, int mode,
[RuntimeDllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPIMeanTextConf")]
int BaseAPIMeanTextConf(HandleRef handle);

[RuntimeDllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPIProcessPage")]
int BaseAPIProcessPage(HandleRef handle, HandleRef pix, int page_index, string filename, string retry_config, int timeout_millisec, HandleRef renderer);

[RuntimeDllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPIRecognize")]
int BaseApiRecognize(HandleRef handle, HandleRef monitor);

Expand Down
71 changes: 71 additions & 0 deletions src/Tesseract/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public string GetText()
return Interop.TessApi.BaseAPIGetUTF8Text(Engine.Handle);
}

public string GetText(int timeout)
{
if (!Recognize(0, timeout))
{
return null;
}

return Interop.TessApi.BaseAPIGetUTF8Text(Engine.Handle);
}

/// <summary>
/// Gets the page's content as a HOCR text.
/// </summary>
Expand All @@ -139,6 +149,22 @@ public string GetHOCRText(int pageNum, bool useXHtml = false)
return Interop.TessApi.BaseAPIGetHOCRText(Engine.Handle, pageNum);
}

public string GetHOCRText(int pageNum, int timeout, bool useXHtml = false)
{
//Why Not Use 'nameof(pageNum)' instead of '"pageNum"'
Guard.Require("pageNum", pageNum >= 0, "Page number must be greater than or equal to zero (0).");

if (!Recognize(pageNum, timeout))
{
return null;
}

if (useXHtml)
return Interop.TessApi.BaseAPIGetHOCRText2(Engine.Handle, pageNum);
else
return Interop.TessApi.BaseAPIGetHOCRText(Engine.Handle, pageNum);
}

/// <summary>
/// Get's the mean confidence that as a percentage of the recognized text.
/// </summary>
Expand Down Expand Up @@ -295,6 +321,51 @@ internal void Recognize()
}
}

private bool Recognize(int pageNum, int timeout)
{
Guard.Verify(PageSegmentMode != PageSegMode.OsdOnly, "Cannot OCR image when using OSD only page segmentation, please use DetectBestOrientation instead.");

//string strText = null;

int success = -1;

if (!runRecognitionPhase)
{
//Interop.TessApi.BaseApiSetVariable(Engine.Handle, "tessedit_create_hocr", "1");
string fileName = Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
//IntPtr renderer = Interop.TessApi.Native.HOcrRendererCreate(fileName);
success = Interop.TessApi.Native.BaseAPIProcessPage(Engine.Handle, Image.Handle, pageNum, null, null, timeout, new HandleRef(this, IntPtr.Zero));
}

if (success == 1)
{
runRecognitionPhase = true;

// now write out the thresholded image if required to do so
bool tesseditWriteImages;
if (Engine.TryGetBoolVariable("tessedit_write_images", out tesseditWriteImages) && tesseditWriteImages)
{
using (Pix thresholdedImage = GetThresholdedImage())
{
string filePath = Path.Combine(Environment.CurrentDirectory, "tessinput.tif");
try
{
thresholdedImage.Save(filePath, ImageFormat.TiffG4);
trace.TraceEvent(TraceEventType.Information, 2,
"Successfully saved the thresholded image to '{0}'", filePath);
}
catch (Exception error)
{
trace.TraceEvent(TraceEventType.Error, 2,
"Failed to save the thresholded image to '{0}'.\nError: {1}", filePath, error.Message);
}
}
}
}

return success == 1;
}

protected override void Dispose(bool disposing)
{
if (disposing) {
Expand Down