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 283
/
Copy pathocr.ps1
56 lines (44 loc) · 1.69 KB
/
ocr.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
$key="YOUR_KEY"
$endpoint="YOUR_ENDPOINT"
# Code to call OCR service for text in image analysis
$img_file = "advert.jpg"
if ($args.count -gt 0 -And $args[0] -in ("advert.jpg", "letter.jpg", "note.jpg"))
{
$img_file = $args[0]
}
$img = "https://raw.githubusercontent.com/MicrosoftLearning/AI-900-AIFundamentals/main/data/vision/$img_file"
$headers = @{}
$headers.Add( "Ocp-Apim-Subscription-Key", $key )
$headers.Add( "Content-Type","application/json" )
$body = "{'url' : '$img'}"
write-host "Analyzing Image...`n"
$result = Invoke-Webrequest -Method Post `
-Uri "$endpoint/vision/v3.2/read/analyze?language=en" `
-Headers $headers `
-Body $body
# Extract the URL from the response of the Read operation
# to call the API to getting the analysis results
$resultUrl = $($result.Headers['Operation-Location'])
# Create the header for the REST GET with only the subscription key
$resultHeaders = @{}
$resultHeaders.Add( "Ocp-Apim-Subscription-Key", $key )
$body = "{'url' : '$img'}"
# Get the read 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")
# Access the relevant fields from the analysis
$analysisFields = $analysis.analyzeResult.readResults.lines
# Print out the text and bounding boxes
foreach ($listofdict in $analysisFields)
{
foreach($dict in $listofdict)
{
Write-Host ("Text:", $($dict.text), "| Text Bounding Box:", $($dict.boundingBox))
}
}