I've an MS Azure Object detection model in customvision.ai website. I make predictions using it's API, then draw output bounding box as rectangle in the image. Following is the powershell code to achieve that:
#First powershell script
#API call to object detection model to predict objects and their bounding boxes
$filePath = "C:\ImageOne.jpg"
$fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
$Url = "<MyCustomVisionModelEndpoint>"
$headers = @{
'Prediction-Key' = $customVisionPredictionKey
'Content-Type' = 'application/octet-stream'
}
$proxy = [System.Net.WebRequest]::GetSystemWebProxy().GetProxy("https://<CustomVisionResourceName>.cognitiveservices.azure.com")
$APIresult = Invoke-RestMethod -Method 'Post' -Uri $url -Body $fileBytes -Headers $headers -UseDefaultCredentials -Proxy $proxy -ProxyUseDefaultCredentials
#Get Results and draw the predicted bounding boxes in the input image
$results = $APIresult.predictions | Where-Object {!$_.tagName.toString().contains("not") -and !$_.tagName.toString().contains("door") -and !$_.tagName.toString().contains("tyre") -and $_.probability -ge 0.02}
Add-Type -AssemblyName System.Drawing
$srcImage=[System.Drawing.Image]::FromFile($FilePath)
$height = $srcImage.height
$width = $srcImage.width
$graphics=[System.Drawing.Graphics]::FromImage($srcImage)
$pen = New-Object Drawing.Pen([System.Drawing.Color]::Blue,3);
$font = New-Object Drawing.Font("Arial", 7);
$brush = New-Object Drawing.SolidBrush([System.Drawing.Color]::Red);
foreach($result in $results)
{
$left = $width * $result.boundingBox.left
$top = $height * $result.boundingBox.top
$PHeight = $height * $result.boundingBox.height
$PWidth = $width * $result.boundingBox.width
$rect = New-Object Drawing.Rectangle($left, $top, $PWidth, $PHeight);
$graphics.DrawRectangle($pen, $rect);
$tag = "Tag: " + $result.tagName + " ,Probability: " + [math]::Round($result.probability * 100,2)
$graphics.DrawString($tag,$font,$brush,$left, $top);
}
$graphics.Dispose()
if($results)
{
$srcImage.Save("D:\Output.jpg")
}
Assume that my object detection model predicted one object. Now I want to use this predicted object bounding boxes and crop the original image with these boudning box co-ordinates. Following is the powershell code to achieve that:
#Second powershell script
Add-Type -AssemblyName System.Drawing
$filepath = "C:\ImageOne.jpg"
$srcImage=[System.Drawing.Image]::FromFile($FilePath)
$srcRect = New-Object Drawing.Rectangle(0,0,$srcImage.Width,$srcImage.Height)
#Here left, top, width, height co-ordinates comes from above powershell code which does predictions and draws rectangular bounding boxes
$destRect = New-Object Drawing.Rectangle([UInt32]($left),[UInt32]($top),[UInt32]($PWidth),[UInt32]($PHeight))
$bmp=new-object System.Drawing.Bitmap($destRect.Width,$destRect.Height)
$graphics=[System.Drawing.Graphics]::FromImage($bmp)
#I tried with [System.Drawing.GraphicsUnit]::Pixel also, but no use
$units = [System.Drawing.GraphicsUnit]::Point
$destRect = new-object Drawing.Rectangle($destRect.Left, $destRect.Top, $destRect.Width, $destRect.Height)
$srcRect = new-object Drawing.Rectangle($srcRect.Left, $srcRect.Top, $srcRect.Width, $srcRect.Height)
$graphics.DrawImage($srcImage, $srcRect, $destRect, $units)
$graphics.Dispose()
$bmp.Save("D:\test.jpg")
I prepared second powershell script with the help of this link: Powershell use .NET .DrawImage in System.Drawing
The first code is working fine, I'm able to do predictions, draw predicted objects bounding box in the input image.
I'm using same predicted bounding box co-ordinates in the second script and trying to crop that predicted object area. But it's not cropping correctly.
The first script drew bounding box correctly, but second script is cropping some other part of the image.
I assume there might be some mistake in second powershell script. I'm not able to understand what's going wrong. Can someone please help me?