Wednesday, January 22, 2025

Azure AI services - Explore Computer Vision, Exploring Form Recognition, Optical Character Recognition, Face detection

Explore Computer Vision:

$key = "key"
$endpoint = "https://endpinturl.cognitiveservices.azure.com/"
 
$img = "https://raw.githubusercontent.com/MicrosoftLearning/AI-900-AIFundamentals/main/data/vision/store-camera-1.jpg"
 
$headers = @{ }
$headers.Add("Ocp-Apim-Subscription-Key", $key)
$headers.Add("Content-Type", "application/json")
 
$body = "{'url' : '$img'}"
 
write - host "Analyzing image..."
$result = Invoke - RestMethod - Method Post `
          -Uri "$endpoint/vision/v3.2/analyze?visualFeatures=Categories,Description,Objects" `
          -Headers $headers `
          -Body $body | ConvertTo-Json -Depth 5
 
$analysis = $result | ConvertFrom-Json
Write-Host("`nDescription:")
foreach ($caption in $analysis.description.captions)
{
    Write-Host ($caption.text)
}
 
Write - Host("`nObjects in this image:")
foreach ($object in $analysis.objects)
{
    Write-Host (" -", $object.object)
}
 
Write - Host("`nTags relevant to this image:")
foreach ($tag in $analysis.description.tags)
{
    Write-Host (" -", $tag)
}
 
Write - Host("`n")
 
 
 OutPut:


Exploring Form Recognition:


$key="CvVuM7D8IfIksWxIzvrUBHLZ6rbeBjFXJ3w3AAALACOGZr3Y"

$endpoint="https://sreecognitive.cognitiveservices.azure.com/"

# Create the URL where the raw receipt image can be found

$img = "https://raw.githubusercontent.com/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))

OutPut:



Exploring Optical Character Recognition:



$key="8oOnmgDqT5BTfAxQC5V32OM5HDKdN6BACYeBjFXJ3w3AAAFACOGMoYe"
$endpoint="https://mysreecompvision1.cognitiveservices.azure.com/"
 
$img = "https://raw.githubusercontent.com/MicrosoftLearning/AI-900-AIFundamentals/main/data/vision/advert.jpg"
 
$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))
    }
}
$key="8oOnmgDqT5BTfAxQC5V32OM5tg8aEYr00g7JHDKdN6BzYfDMVLiqJQQJ99BAACYeBjFXJ3w3AAAFACOGMoYe"
$endpoint="https://mysreecompvision1.cognitiveservices.azure.com/"
 
$img = "https://raw.githubusercontent.com/MicrosoftLearning/AI-900-AIFundamentals/main/data/vision/advert.jpg"
 
$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))
    }
}

OutPut:


Exploring Face detection:

$key = "15q4sAEBrUT05hm5fG4ZGWWA6bCIVRQ9BAACYeBjFXJ3w3AAAKACOGjysA"
$endpoint = "https://sreefaceai1.cognitiveservices.azure.com/"
 
$img = "https://raw.githubusercontent.com/MicrosoftLearning/AI-900-AIFundamentals/main/data/vision/store-camera-3.jpg"
 
$headers = @{ }
$headers.Add("Ocp-Apim-Subscription-Key", $key)
$headers.Add("Content-Type", "application/json")
 
$body = "{'url' : '$img'}"
 
write - host "Analyzing image...`n"
$result = Invoke - RestMethod - Method Post `
          -Uri "$endpoint/face/v1.0/detect?detectionModel=detection_01" `
          -Headers $headers `
          -Body $body | ConvertTo-Json -Depth 5
 
$analysis = ($result | ConvertFrom-Json)
Write-Host ("`nFrom June 21st 2022, Face service capabilities that return personally identifiable features are restricted.`nSee https://azure.microsoft.com/blog/responsible-ai-investments-and-safeguards-for-facial-recognition/ for details.`nThis code is restricted to returning the location of any faces detected:`n")
foreach ($face in $analysis)
{
    Write-Host("Face location: $($face.faceRectangle)`n")
}

OutPut:





No comments:

Post a Comment

Featured Post

Building Secure APIs with FastAPI and Azure AD Authentication

Building Secure APIs with FastAPI and Azure AD Authentication Published on September 2, 2025 In today's world of microservices and API-f...

Popular posts