-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0519dcc
Showing
13 changed files
with
565 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*bin | ||
*obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.1.32421.90 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspose.OCR.Cloud.CommandLineTool", "Aspose.OCR.Cloud.CommandLineTool\Aspose.OCR.Cloud.CommandLineTool.csproj", "{232FD59F-E915-46B2-AA9F-34C54AEE5A74}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{232FD59F-E915-46B2-AA9F-34C54AEE5A74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{232FD59F-E915-46B2-AA9F-34C54AEE5A74}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{232FD59F-E915-46B2-AA9F-34C54AEE5A74}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{232FD59F-E915-46B2-AA9F-34C54AEE5A74}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {C562B4E6-8ACC-4956-8174-8FB203BCC685} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using Aspose.OCR.Cloud; | ||
using Aspose.OCR.Cloud.SDK.Api; | ||
using Aspose.OCR.Cloud.SDK.Model; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Aspose.OCR.Cloud.CommandLineTool.API | ||
{ | ||
public static class RecognizeImage | ||
{ | ||
public static void Run(string srcFilePath, | ||
string dstFilePath, | ||
Language language = Language.English, | ||
bool makeBinarization = true, | ||
bool makeSkewCorrect = false, | ||
bool makeUpsampling = false, | ||
DsrMode dsrMode = DsrMode.DsrPlusDetector, | ||
ResultType resultType = ResultType.Text) | ||
{ | ||
try | ||
{ | ||
|
||
Config config = Config.GetConfig(); | ||
|
||
RecognizeImageApi api = new RecognizeImageApi(config.ClientId, config.ClientSecret); | ||
|
||
OCRRecognizeImageBody requestBody = new OCRRecognizeImageBody( | ||
image: File.ReadAllBytes(srcFilePath), | ||
settings: new OCRSettingsRecognizeImage( | ||
language: language, | ||
makeBinarization: makeBinarization, | ||
makeSkewCorrect: makeSkewCorrect, | ||
makeUpsampling: makeUpsampling, | ||
dsrMode: dsrMode, | ||
resultType: resultType | ||
)); | ||
|
||
Console.Write($"Sending file {srcFilePath} to API..."); | ||
string taskId = api.PostRecognizeImage(requestBody); | ||
Console.WriteLine("done."); | ||
|
||
Console.Write($"Waiting for completion of task ID {taskId}..."); | ||
Utilities.WaitTaskCompletion(taskId); | ||
Console.WriteLine("done."); | ||
|
||
Console.Write($"Saving result to {dstFilePath}..."); | ||
OCRResponse response = api.GetRecognizeImage(taskId); | ||
|
||
if (response == null) throw new Exception("API response is empty"); | ||
if (response.ResponseStatusCode != ResponseStatusCode.Ok) | ||
throw new Exception($"Response status code is {response.ResponseStatusCode.Value}"); | ||
if (response.Error != null) throw new Exception($"API response contains error:{response.Error.Messages.First()}"); | ||
if (response.TaskStatus != OCRTaskStatus.Completed) throw new Exception($"OCR task returned status {response.TaskStatus.Value}"); | ||
if (response.Results == null) throw new Exception($"Results is null"); | ||
|
||
|
||
string? directoryPath = Path.GetDirectoryName(dstFilePath); | ||
if (!string.IsNullOrEmpty(directoryPath) & !Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath); | ||
|
||
var result = response.Results.First(); | ||
switch (result.Type) | ||
{ | ||
case "Text": | ||
File.WriteAllText($"{dstFilePath}{(dstFilePath.ToLower().EndsWith(".txt")? "" : ".txt")}", | ||
Encoding.UTF8.GetString(result.Data)); | ||
break; | ||
case "Pdf": | ||
File.WriteAllBytes($"{dstFilePath}{(dstFilePath.ToLower().EndsWith(".pdf") ? "" : ".pdf")}", | ||
result.Data); | ||
break; | ||
case "Hocr": | ||
File.WriteAllBytes( | ||
$"{dstFilePath}{(dstFilePath.ToLower().EndsWith(".html") ? "" : ".html")}", result.Data); | ||
break; | ||
} | ||
|
||
|
||
Console.WriteLine("done."); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error while processing image recognition: {ex.Message}"); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Aspose.OCR.Cloud.SDK.Api; | ||
using Aspose.OCR.Cloud.SDK.Model; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Aspose.OCR.Cloud.CommandLineTool.API | ||
{ | ||
public static class Utilities | ||
{ | ||
|
||
/// <summary> | ||
/// Uses Utilities API to get task status until it become completed or failed | ||
/// </summary> | ||
public static void WaitTaskCompletion(string taskid) | ||
{ | ||
Config config = Config.GetConfig(); | ||
int getTaskStatusRetriesCount = 0; | ||
OCRResponse getTaskStatusResponse = new OCRResponse(); | ||
UtilitiesApi api = new UtilitiesApi(config.ClientId, config.ClientSecret); | ||
while (getTaskStatusRetriesCount <= 10 | ||
& getTaskStatusResponse.TaskStatus != OCRTaskStatus.Completed | ||
& getTaskStatusResponse.TaskStatus != OCRTaskStatus.Error) | ||
{ | ||
getTaskStatusRetriesCount += 1; | ||
// Console.WriteLine($"Getting task status attempt {getTaskStatusRetriesCount}"); | ||
Thread.Sleep((int)(5000 + getTaskStatusRetriesCount * 1000)); | ||
getTaskStatusResponse = api.GetTaskStatus(taskid); | ||
switch (getTaskStatusResponse.TaskStatus) | ||
{ | ||
case OCRTaskStatus.Pending: | ||
break; | ||
case OCRTaskStatus.Error: | ||
throw new Exception("Task completed with error. Please, try again later."); | ||
} | ||
}; | ||
} | ||
} | ||
} |
Oops, something went wrong.