Skip to content

Commit 0519dcc

Browse files
Release 24.01.0 Rev.0
0 parents  commit 0519dcc

13 files changed

+565
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*bin
2+
*obj

Aspose.OCR.Cloud.CommandLineTool.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32421.90
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspose.OCR.Cloud.CommandLineTool", "Aspose.OCR.Cloud.CommandLineTool\Aspose.OCR.Cloud.CommandLineTool.csproj", "{232FD59F-E915-46B2-AA9F-34C54AEE5A74}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{232FD59F-E915-46B2-AA9F-34C54AEE5A74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{232FD59F-E915-46B2-AA9F-34C54AEE5A74}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{232FD59F-E915-46B2-AA9F-34C54AEE5A74}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{232FD59F-E915-46B2-AA9F-34C54AEE5A74}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {C562B4E6-8ACC-4956-8174-8FB203BCC685}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Aspose.OCR.Cloud;
2+
using Aspose.OCR.Cloud.SDK.Api;
3+
using Aspose.OCR.Cloud.SDK.Model;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Aspose.OCR.Cloud.CommandLineTool.API
11+
{
12+
public static class RecognizeImage
13+
{
14+
public static void Run(string srcFilePath,
15+
string dstFilePath,
16+
Language language = Language.English,
17+
bool makeBinarization = true,
18+
bool makeSkewCorrect = false,
19+
bool makeUpsampling = false,
20+
DsrMode dsrMode = DsrMode.DsrPlusDetector,
21+
ResultType resultType = ResultType.Text)
22+
{
23+
try
24+
{
25+
26+
Config config = Config.GetConfig();
27+
28+
RecognizeImageApi api = new RecognizeImageApi(config.ClientId, config.ClientSecret);
29+
30+
OCRRecognizeImageBody requestBody = new OCRRecognizeImageBody(
31+
image: File.ReadAllBytes(srcFilePath),
32+
settings: new OCRSettingsRecognizeImage(
33+
language: language,
34+
makeBinarization: makeBinarization,
35+
makeSkewCorrect: makeSkewCorrect,
36+
makeUpsampling: makeUpsampling,
37+
dsrMode: dsrMode,
38+
resultType: resultType
39+
));
40+
41+
Console.Write($"Sending file {srcFilePath} to API...");
42+
string taskId = api.PostRecognizeImage(requestBody);
43+
Console.WriteLine("done.");
44+
45+
Console.Write($"Waiting for completion of task ID {taskId}...");
46+
Utilities.WaitTaskCompletion(taskId);
47+
Console.WriteLine("done.");
48+
49+
Console.Write($"Saving result to {dstFilePath}...");
50+
OCRResponse response = api.GetRecognizeImage(taskId);
51+
52+
if (response == null) throw new Exception("API response is empty");
53+
if (response.ResponseStatusCode != ResponseStatusCode.Ok)
54+
throw new Exception($"Response status code is {response.ResponseStatusCode.Value}");
55+
if (response.Error != null) throw new Exception($"API response contains error:{response.Error.Messages.First()}");
56+
if (response.TaskStatus != OCRTaskStatus.Completed) throw new Exception($"OCR task returned status {response.TaskStatus.Value}");
57+
if (response.Results == null) throw new Exception($"Results is null");
58+
59+
60+
string? directoryPath = Path.GetDirectoryName(dstFilePath);
61+
if (!string.IsNullOrEmpty(directoryPath) & !Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath);
62+
63+
var result = response.Results.First();
64+
switch (result.Type)
65+
{
66+
case "Text":
67+
File.WriteAllText($"{dstFilePath}{(dstFilePath.ToLower().EndsWith(".txt")? "" : ".txt")}",
68+
Encoding.UTF8.GetString(result.Data));
69+
break;
70+
case "Pdf":
71+
File.WriteAllBytes($"{dstFilePath}{(dstFilePath.ToLower().EndsWith(".pdf") ? "" : ".pdf")}",
72+
result.Data);
73+
break;
74+
case "Hocr":
75+
File.WriteAllBytes(
76+
$"{dstFilePath}{(dstFilePath.ToLower().EndsWith(".html") ? "" : ".html")}", result.Data);
77+
break;
78+
}
79+
80+
81+
Console.WriteLine("done.");
82+
}
83+
catch (Exception ex)
84+
{
85+
Console.WriteLine($"Error while processing image recognition: {ex.Message}");
86+
}
87+
}
88+
89+
}
90+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Aspose.OCR.Cloud.SDK.Api;
2+
using Aspose.OCR.Cloud.SDK.Model;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Aspose.OCR.Cloud.CommandLineTool.API
10+
{
11+
public static class Utilities
12+
{
13+
14+
/// <summary>
15+
/// Uses Utilities API to get task status until it become completed or failed
16+
/// </summary>
17+
public static void WaitTaskCompletion(string taskid)
18+
{
19+
Config config = Config.GetConfig();
20+
int getTaskStatusRetriesCount = 0;
21+
OCRResponse getTaskStatusResponse = new OCRResponse();
22+
UtilitiesApi api = new UtilitiesApi(config.ClientId, config.ClientSecret);
23+
while (getTaskStatusRetriesCount <= 10
24+
& getTaskStatusResponse.TaskStatus != OCRTaskStatus.Completed
25+
& getTaskStatusResponse.TaskStatus != OCRTaskStatus.Error)
26+
{
27+
getTaskStatusRetriesCount += 1;
28+
// Console.WriteLine($"Getting task status attempt {getTaskStatusRetriesCount}");
29+
Thread.Sleep((int)(5000 + getTaskStatusRetriesCount * 1000));
30+
getTaskStatusResponse = api.GetTaskStatus(taskid);
31+
switch (getTaskStatusResponse.TaskStatus)
32+
{
33+
case OCRTaskStatus.Pending:
34+
break;
35+
case OCRTaskStatus.Error:
36+
throw new Exception("Task completed with error. Please, try again later.");
37+
}
38+
};
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)