Skip to content

Commit 20f3b1f

Browse files
author
Sowmiya Loganathan
committed
Add samples for OCR Azure & AWS
1 parent d07c848 commit 20f3b1f

22 files changed

+723
-0
lines changed
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.3.32819.101
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Perform-OCR-AWS-Textract", "Perform-OCR-AWS-Textract\Perform-OCR-AWS-Textract.csproj", "{5E130BCC-E31C-4B60-AE1C-DD02937C32DA}"
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+
{5E130BCC-E31C-4B60-AE1C-DD02937C32DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5E130BCC-E31C-4B60-AE1C-DD02937C32DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5E130BCC-E31C-4B60-AE1C-DD02937C32DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5E130BCC-E31C-4B60-AE1C-DD02937C32DA}.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 = {C33A5020-F48D-4553-922B-094C1D344A93}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using Syncfusion.OCRProcessor;
5+
using Syncfusion.Pdf.Graphics;
6+
using Syncfusion.Drawing;
7+
using Amazon;
8+
using Amazon.Textract;
9+
using Amazon.Textract.Model;
10+
11+
12+
namespace OCR.Test
13+
{
14+
class AWSExternalOcrEngine : IOcrEngine
15+
{
16+
private string awsAccessKeyId = "AccessKeyID";
17+
private string awsSecretAccessKey = "SecretAccessKey";
18+
private float imageHeight;
19+
private float imageWidth;
20+
public OCRLayoutResult PerformOCR(Stream stream)
21+
{
22+
AmazonTextractClient clientText = Authenticate();
23+
DetectDocumentTextResponse textResponse = GetAWSTextractResult(clientText, stream).Result;
24+
OCRLayoutResult oCRLayoutResult = ConvertAWSTextractResultToOcrLayoutResult(textResponse);
25+
return oCRLayoutResult;
26+
}
27+
28+
public AmazonTextractClient Authenticate()
29+
{
30+
AmazonTextractClient client = new AmazonTextractClient(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.USEast1);
31+
return client;
32+
}
33+
34+
public async Task<DetectDocumentTextResponse> GetAWSTextractResult(AmazonTextractClient client, Stream stream)
35+
{
36+
stream.Position = 0;
37+
MemoryStream memoryStream = new MemoryStream();
38+
stream.CopyTo(memoryStream);
39+
PdfBitmap bitmap = new PdfBitmap(memoryStream);
40+
imageHeight = bitmap.Height;
41+
imageWidth = bitmap.Width;
42+
43+
DetectDocumentTextResponse response = await client.DetectDocumentTextAsync(new DetectDocumentTextRequest
44+
{
45+
Document = new Document
46+
{
47+
Bytes = memoryStream
48+
}
49+
});
50+
return response;
51+
}
52+
53+
public OCRLayoutResult ConvertAWSTextractResultToOcrLayoutResult(DetectDocumentTextResponse textResponse)
54+
{
55+
OCRLayoutResult layoutResult = new OCRLayoutResult();
56+
Syncfusion.OCRProcessor.Page ocrPage = new Page();
57+
Syncfusion.OCRProcessor.Line ocrLine;
58+
Syncfusion.OCRProcessor.Word ocrWord;
59+
layoutResult.ImageHeight = imageHeight;
60+
layoutResult.ImageWidth = imageWidth;
61+
foreach (var page in textResponse.Blocks)
62+
{
63+
ocrLine = new Line();
64+
if (page.BlockType == "WORD")
65+
{
66+
ocrWord = new Word();
67+
ocrWord.Text = page.Text;
68+
69+
float left = page.Geometry.BoundingBox.Left;
70+
float top = page.Geometry.BoundingBox.Top;
71+
float width = page.Geometry.BoundingBox.Width;
72+
float height = page.Geometry.BoundingBox.Height;
73+
Rectangle rect = GetBoundingBox(left,top,width,height);
74+
ocrWord.Rectangle = rect;
75+
ocrLine.Add(ocrWord);
76+
ocrPage.Add(ocrLine);
77+
}
78+
}
79+
layoutResult.Add(ocrPage);
80+
return layoutResult;
81+
}
82+
public Rectangle GetBoundingBox(float left, float top, float width, float height)
83+
{
84+
int x = Convert.ToInt32(left * imageWidth);
85+
int y = Convert.ToInt32(top * imageHeight);
86+
int bboxWidth = Convert.ToInt32((width * imageWidth) + x);
87+
int bboxHeight = Convert.ToInt32((height * imageHeight) + y);
88+
Rectangle rect = new Rectangle(x,y, bboxWidth, bboxHeight);
89+
return rect;
90+
}
91+
}
92+
}
93+
41.9 KB
Loading
27.8 KB
Loading
59.4 KB
Loading
66.7 KB
Loading
53.3 KB
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<RootNamespace>Perform_OCR_AWS_Textract</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="AWSSDK.Textract" Version="3.7.100.6" />
13+
<PackageReference Include="Syncfusion.PDF.OCR.NET" Version="20.3.0.60" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// See https://aka.ms/new-console-template for more information
2+
3+
using Syncfusion.OCRProcessor;
4+
using Syncfusion.Pdf.Parsing;
5+
6+
namespace OCR.Test
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
//Initialize the OCR processor.
13+
using (OCRProcessor processor = new OCRProcessor())
14+
{
15+
//Load an existing PDF document.
16+
FileStream stream = new FileStream("../../../Region.pdf", FileMode.Open);
17+
PdfLoadedDocument lDoc = new PdfLoadedDocument(stream);
18+
19+
//Set the OCR language.
20+
processor.Settings.Language = Languages.English;
21+
22+
//Initialize the AWS Textract external OCR engine.
23+
IOcrEngine azureOcrEngine = new AWSExternalOcrEngine();
24+
processor.ExternalEngine = azureOcrEngine;
25+
26+
//Perform OCR with input document.
27+
string text = processor.PerformOCR(lDoc);
28+
29+
//Create file stream.
30+
FileStream fileStream = new FileStream("../../../OCR.pdf", FileMode.CreateNew);
31+
32+
//Save the document into stream.
33+
lDoc.Save(fileStream);
34+
35+
//Close the document.
36+
lDoc.Close();
37+
stream.Dispose();
38+
fileStream.Dispose();
39+
}
40+
}
41+
}
42+
}
43+
Binary file not shown.

0 commit comments

Comments
 (0)