Skip to content

Commit 8ac994c

Browse files
authored
Merge pull request #118 from papabearyang2/wayneyang/add-code-sample-for-4.0GA
Add python/C# code sample for 4.0 GA
2 parents 312638e + 8cb37f1 commit 8ac994c

47 files changed

Lines changed: 4604 additions & 1295 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>CodeSnippetForApiVer_2024-11-30</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Azure.AI.DocumentIntelligence" Version="1.0.0" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// #define RUN_AS_ENTRY_OF_TOP_LEVEL_STATEMENT
2+
#if RUN_AS_ENTRY_OF_TOP_LEVEL_STATEMENT
3+
4+
/*
5+
This code sample shows Custom Model operations with the Azure AI Document Intelligence client library.
6+
7+
To learn more, please visit the documentation - Quickstart: Document Intelligence (formerly Form Recognizer) SDKs
8+
https://learn.microsoft.com/azure/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api?pivots=programming-language-csharp
9+
*/
10+
11+
using Azure;
12+
using Azure.AI.DocumentIntelligence;
13+
14+
/*
15+
Remember to remove the key from your code when you're done, and never post it publicly. For production, use
16+
secure methods to store and access your credentials. For more information, see
17+
https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-security?tabs=command-line%2Ccsharp#environment-variables-and-application-configuration
18+
*/
19+
20+
string endpoint = Environment.GetEnvironmentVariable("DOCUMENTINTELLIGENCE_ENDPOINT") ?? string.Empty;
21+
string key = Environment.GetEnvironmentVariable("DOCUMENTINTELLIGENCE_API_KEY") ?? string.Empty;
22+
string modelId = Environment.GetEnvironmentVariable("CUSTOM_BUILT_MODEL_ID") ?? string.Empty;
23+
24+
if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(key) || string.IsNullOrEmpty(modelId))
25+
{
26+
Console.WriteLine("Please set the environment variables for the endpoint, key, and model ID.");
27+
return;
28+
}
29+
30+
string documentUri = "YOUR_FILE_URI"; // Replace with the URI of the document you want to analyze
31+
32+
AzureKeyCredential credential = new AzureKeyCredential(key);
33+
DocumentIntelligenceClient client = new DocumentIntelligenceClient(new Uri(endpoint), credential);
34+
35+
Uri fileUri = new Uri(documentUri);
36+
Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, modelId, fileUri);
37+
38+
AnalyzeResult result = operation.Value;
39+
40+
Console.WriteLine($"Document was analyzed with model with ID: {result.ModelId}");
41+
42+
foreach (AnalyzedDocument document in result.Documents)
43+
{
44+
Console.WriteLine($"Document of type: {document.DocumentType}");
45+
46+
foreach (KeyValuePair<string, DocumentField> fieldKvp in document.Fields)
47+
{
48+
string fieldName = fieldKvp.Key;
49+
DocumentField field = fieldKvp.Value;
50+
51+
Console.WriteLine($"Field '{fieldName}': ");
52+
53+
Console.WriteLine($" Content: '{field.Content}'");
54+
Console.WriteLine($" Confidence: '{field.Confidence}'");
55+
}
56+
}
57+
58+
// Iterate over lines and selection marks on each page
59+
foreach (DocumentPage page in result.Pages)
60+
{
61+
Console.WriteLine($"Lines found on page {page.PageNumber}");
62+
foreach (var line in page.Lines)
63+
{
64+
Console.WriteLine($" {line.Content}");
65+
}
66+
67+
Console.WriteLine($"Selection marks found on page {page.PageNumber}");
68+
foreach (var selectionMark in page.SelectionMarks)
69+
{
70+
Console.WriteLine($" Selection mark is '{selectionMark.State}' with confidence {selectionMark.Confidence}");
71+
}
72+
}
73+
74+
// Iterate over the document tables
75+
for (int i = 0; i < result.Tables.Count; i++)
76+
{
77+
Console.WriteLine($"Table {i + 1}");
78+
foreach (var cell in result.Tables[i].Cells)
79+
{
80+
Console.WriteLine($" Cell[{cell.RowIndex}][{cell.ColumnIndex}] has content '{cell.Content}' with kind '{cell.Kind}'");
81+
}
82+
}
83+
84+
#endif
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// #define RUN_AS_ENTRY_OF_TOP_LEVEL_STATEMENT
2+
#if RUN_AS_ENTRY_OF_TOP_LEVEL_STATEMENT
3+
4+
/*
5+
This code sample shows Prebuilt Layout operations with the Azure AI Document Intelligence client library.
6+
7+
To learn more, please visit the documentation - Quickstart: Document Intelligence (formerly Form Recognizer) SDKs
8+
https://learn.microsoft.com/azure/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api?pivots=programming-language-csharp
9+
*/
10+
11+
using Azure;
12+
using Azure.AI.DocumentIntelligence;
13+
14+
/*
15+
Remember to remove the key from your code when you're done, and never post it publicly. For production, use
16+
secure methods to store and access your credentials. For more information, see
17+
https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-security?tabs=command-line%2Ccsharp#environment-variables-and-application-configuration
18+
*/
19+
20+
string endpoint = Environment.GetEnvironmentVariable("DOCUMENTINTELLIGENCE_ENDPOINT") ?? string.Empty;
21+
string key = Environment.GetEnvironmentVariable("DOCUMENTINTELLIGENCE_API_KEY") ?? string.Empty;
22+
if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(key))
23+
{
24+
Console.WriteLine("Please set the environment variables for the endpoint and key.");
25+
return;
26+
}
27+
28+
AzureKeyCredential credential = new AzureKeyCredential(key);
29+
DocumentIntelligenceClient client = new DocumentIntelligenceClient(new Uri(endpoint), credential);
30+
31+
// sample document
32+
Uri fileUri = new Uri("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf");
33+
34+
Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-layout", fileUri);
35+
36+
AnalyzeResult result = operation.Value;
37+
38+
foreach (DocumentPage page in result.Pages)
39+
{
40+
Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),");
41+
Console.WriteLine($"and {page.SelectionMarks.Count} selection mark(s).");
42+
43+
for (int i = 0; i < page.Lines.Count; i++)
44+
{
45+
DocumentLine line = page.Lines[i];
46+
Console.WriteLine($" Line {i} has content: '{line.Content}'.");
47+
48+
Console.WriteLine($" Its bounding box is:");
49+
Console.WriteLine($" Upper left => X: {line.Polygon[0]}, Y= {line.Polygon[1]}");
50+
Console.WriteLine($" Upper right => X: {line.Polygon[2]}, Y= {line.Polygon[3]}");
51+
Console.WriteLine($" Lower right => X: {line.Polygon[4]}, Y= {line.Polygon[5]}");
52+
Console.WriteLine($" Lower left => X: {line.Polygon[6]}, Y= {line.Polygon[7]}");
53+
}
54+
55+
for (int i = 0; i < page.SelectionMarks.Count; i++)
56+
{
57+
DocumentSelectionMark selectionMark = page.SelectionMarks[i];
58+
59+
Console.WriteLine($" Selection Mark {i} is {selectionMark.State}.");
60+
Console.WriteLine($" Its bounding box is:");
61+
Console.WriteLine($" Upper left => X: {selectionMark.Polygon[0]}, Y= {selectionMark.Polygon[1]}");
62+
Console.WriteLine($" Upper right => X: {selectionMark.Polygon[2]}, Y= {selectionMark.Polygon[3]}");
63+
Console.WriteLine($" Lower right => X: {selectionMark.Polygon[4]}, Y= {selectionMark.Polygon[5]}");
64+
Console.WriteLine($" Lower left => X: {selectionMark.Polygon[6]}, Y= {selectionMark.Polygon[7]}");
65+
}
66+
}
67+
68+
foreach (DocumentStyle style in result.Styles)
69+
{
70+
// Check the style and style confidence to see if text is handwritten.
71+
// Note that value '0.8' is used as an example.
72+
73+
bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true;
74+
75+
if (isHandwritten && style.Confidence > 0.8)
76+
{
77+
Console.WriteLine($"Handwritten content found:");
78+
79+
foreach (DocumentSpan span in style.Spans)
80+
{
81+
Console.WriteLine($" Content: {result.Content.Substring(span.Offset, span.Length)}");
82+
}
83+
}
84+
}
85+
86+
Console.WriteLine("The following tables were extracted:");
87+
88+
for (int i = 0; i < result.Tables.Count; i++)
89+
{
90+
DocumentTable table = result.Tables[i];
91+
Console.WriteLine($" Table {i} has {table.RowCount} rows and {table.ColumnCount} columns.");
92+
93+
foreach (DocumentTableCell cell in table.Cells)
94+
{
95+
Console.WriteLine($" Cell ({cell.RowIndex}, {cell.ColumnIndex}) has kind '{cell.Kind}' and content: '{cell.Content}'.");
96+
}
97+
}
98+
99+
#endif
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#define RUN_AS_ENTRY_OF_TOP_LEVEL_STATEMENT
2+
#if RUN_AS_ENTRY_OF_TOP_LEVEL_STATEMENT
3+
4+
/*
5+
This code sample shows Prebuilt Read operations with the Azure AI Document Intelligence client library.
6+
7+
To learn more, please visit the documentation - Quickstart: Document Intelligence (formerly Form Recognizer) SDKs
8+
https://learn.microsoft.com/azure/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api?pivots=programming-language-csharp
9+
*/
10+
11+
using Azure;
12+
using Azure.AI.DocumentIntelligence;
13+
14+
/*
15+
Remember to remove the key from your code when you're done, and never post it publicly. For production, use
16+
secure methods to store and access your credentials. For more information, see
17+
https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-security?tabs=command-line%2Ccsharp#environment-variables-and-application-configuration
18+
*/
19+
20+
string endpoint = Environment.GetEnvironmentVariable("DOCUMENTINTELLIGENCE_ENDPOINT") ?? string.Empty;
21+
string key = Environment.GetEnvironmentVariable("DOCUMENTINTELLIGENCE_API_KEY") ?? string.Empty;
22+
if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(key))
23+
{
24+
Console.WriteLine("Please set the environment variables for the endpoint and key.");
25+
return;
26+
}
27+
28+
AzureKeyCredential credential = new AzureKeyCredential(key);
29+
DocumentIntelligenceClient client = new DocumentIntelligenceClient(new Uri(endpoint), credential);
30+
31+
//sample document
32+
Uri fileUri = new Uri("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/read.png");
33+
34+
Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-read", fileUri);
35+
36+
AnalyzeResult result = operation.Value;
37+
38+
foreach (DocumentPage page in result.Pages)
39+
{
40+
Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),");
41+
42+
for (int i = 0; i < page.Lines.Count; i++)
43+
{
44+
DocumentLine line = page.Lines[i];
45+
Console.WriteLine($" Line {i} has content: '{line.Content}'.");
46+
47+
Console.WriteLine($" Its bounding box is:");
48+
Console.WriteLine($" Upper left => X: {line.Polygon[0]}, Y= {line.Polygon[1]}");
49+
Console.WriteLine($" Upper right => X: {line.Polygon[2]}, Y= {line.Polygon[3]}");
50+
Console.WriteLine($" Lower right => X: {line.Polygon[4]}, Y= {line.Polygon[5]}");
51+
Console.WriteLine($" Lower left => X: {line.Polygon[6]}, Y= {line.Polygon[7]}");
52+
}
53+
}
54+
55+
foreach (DocumentStyle style in result.Styles)
56+
{
57+
// Check the style and style confidence to see if text is handwritten.
58+
// Note that value '0.8' is used as an example.
59+
60+
bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true;
61+
62+
if (isHandwritten && style.Confidence > 0.8)
63+
{
64+
Console.WriteLine($"Handwritten content found:");
65+
66+
foreach (DocumentSpan span in style.Spans)
67+
{
68+
Console.WriteLine($" Content: {result.Content.Substring(span.Offset, span.Length)}");
69+
}
70+
}
71+
}
72+
73+
foreach (DocumentLanguage language in result.Languages)
74+
{
75+
Console.WriteLine($" Found language '{language.Locale}' with confidence {language.Confidence}.");
76+
}
77+
78+
#endif

0 commit comments

Comments
 (0)