-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
61 lines (50 loc) · 2.39 KB
/
Program.cs
File metadata and controls
61 lines (50 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using DotNetEnv;
using Azure;
using Azure.AI.FormRecognizer.DocumentAnalysis;
// Store connection information
// string endpoint = "<Endpoint URL>";
// string apiKey = "<API Key>";
// Load the .env file
Env.Load();
string endpoint = "https://docuintelstudio-sea-f0-1.cognitiveservices.azure.com/";
string apiKey = Environment.GetEnvironmentVariable("API_KEY");
// Uri fileUri = new Uri("https://github.com/MicrosoftLearning/mslearn-ai-document-intelligence/blob/main/Labfiles/01-prebuild-models/sample-invoice/sample-invoice.pdf?raw=true");
Uri fileUri = new Uri("https://github.com/miozilla/docuintel/blob/main/italian-sample-invoice.pdf?raw=true");
Console.WriteLine("\nConnecting to Forms Recognizer at: {0}", endpoint);
Console.WriteLine("Analyzing invoice at: {0}\n", fileUri.ToString());
// Create the client
var cred = new AzureKeyCredential(apiKey);
var client = new DocumentAnalysisClient(new Uri(endpoint), cred);
// Analyze the invoice
AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-invoice", fileUri);
// Display invoice information to the user
AnalyzeResult result = operation.Value;
foreach (AnalyzedDocument invoice in result.Documents)
{
if (invoice.Fields.TryGetValue("VendorName", out DocumentField? vendorNameField))
{
if (vendorNameField.FieldType == DocumentFieldType.String)
{
string vendorName = vendorNameField.Value.AsString();
Console.WriteLine($"Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}.");
}
}
if (invoice.Fields.TryGetValue("CustomerName", out DocumentField? customerNameField))
{
if (customerNameField.FieldType == DocumentFieldType.String)
{
string customerName = customerNameField.Value.AsString();
Console.WriteLine($"Customer Name: '{customerName}', with confidence {customerNameField.Confidence}.");
}
}
if (invoice.Fields.TryGetValue("InvoiceTotal", out DocumentField? invoiceTotalField))
{
if (invoiceTotalField.FieldType == DocumentFieldType.Currency)
{
CurrencyValue invoiceTotal = invoiceTotalField.Value.AsCurrency();
Console.WriteLine($"Invoice Total: '{invoiceTotal.Symbol}{invoiceTotal.Amount}', with confidence {invoiceTotalField.Confidence}.");
}
}
}
Console.WriteLine("\nAnalysis complete.\n");