【问题标题】:How to crop image based on Azure object detection output如何根据 Azure 对象检测输出裁剪图像
【发布时间】:2022-12-19 12:55:27
【问题描述】:

我在 customvision.ai 网站上有一个 MS Azure 对象检测模型。我使用它的 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 脚本中可能有一些错误。我无法理解出了什么问题。有人可以帮帮我吗?

【问题讨论】:

    标签: azure image powershell crop microsoft-custom-vision


    【解决方案1】:

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

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

    然后我将图形单元更改为

    [System.Drawing.GraphicsUnit]::Pixel
    

    现在它按预期工作

    【讨论】:

      【解决方案2】:

      我是 Azure Functions 的新手..请更详细地告诉我你是如何运行这个 powershell 脚本的。我有一个强大的应用程序可以捕获图像并使用自定义视觉 ai 来获得预测。我需要一种方法来根据预测裁剪图像。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 2020-06-26
        • 1970-01-01
        • 2012-11-02
        相关资源
        最近更新 更多