如何基于Azure对象检测输出裁剪图像

hc2pp10m  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(137)

我在www.example.com网站上有一个MS Azure对象检测模型customvision.ai。我使用它的API进行预测,然后在图像中绘制输出边界框为矩形。以下是实现此功能的powershell代码:

#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")
}

假设我的目标检测模型预测了一个目标,现在我想使用这个预测的目标边界框,并用这些边界框坐标裁剪原始图像,下面是实现这一目的的powershell代码:

#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")

我准备了第二个powershell脚本的帮助下,这个链接:Powershell use .NET .DrawImage in System.Drawing
第一段代码运行良好,我可以做预测,在输入图像中画出预测对象的边界框。
我在第二个脚本中使用了相同的预测边界框坐标,并试图裁剪预测对象区域,但裁剪不正确。
第一个脚本正确地绘制了边界框,但第二个脚本正在裁剪图像的其他部分。
我想第二个powershell脚本可能有一些错误。我不明白是什么问题。有人能帮帮我吗?

db2dz4w8

db2dz4w81#

在第二个powershell脚本中,我对位图做了如下更改:

$bmp=new-object System.Drawing.Bitmap($srcImage.Width,$srcImage.Height)

然后我将图形单元更改为

[System.Drawing.GraphicsUnit]::Pixel

现在它按预期工作

相关问题