将Vision boundingBox从VNFaceObservation转换为rect以在图像上绘制

Mar*_* Dm 7 computer-vision face-detection ios swift vision-api

我试图使用VNDetectFaceRectanglesRequest新的VisionAPI来检测图像上的面.然后,我在每个检测到的脸上画一个红色矩形.

但我有一个问题,boundingBoxVNFaceObservation转换为CGRect.似乎我唯一的问题是y起源.


这是我的代码:

let request=VNDetectFaceRectanglesRequest{request, error in
    var final_image=UIImage(ciImage: image)
    if let results=request.results as? [VNFaceObservation]{
        for face_obs in results{
          UIGraphicsBeginImageContextWithOptions(final_image.size, false, 1.0)
          final_image.draw(in: CGRect(x: 0, y: 0, width: final_image.size.width, height: final_image.size.height))

          var rect=face_obs.boundingBox
/*/*/*/ RESULT 2 is when I uncomment this line to "flip" the y  /*/*/*/
          //rect.origin.y=1-rect.origin.y
          let conv_rect=CGRect(x: rect.origin.x*final_image.size.width, y: rect.origin.y*final_image.size.height, width: rect.width*final_image.size.width, height: rect.height*final_image.size.height)

          let c=UIGraphicsGetCurrentContext()!
          c.setStrokeColor(UIColor.red.cgColor)
          c.setLineWidth(0.01*final_image.size.width)
          c.stroke(conv_rect)

          let result=UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()

          final_image=result!
        }
    }
    DispatchQueue.main.async{
        self.image_view.image=final_image
    }
}


let handler=VNImageRequestHandler(ciImage: image)
DispatchQueue.global(qos: .userInteractive).async{
    do{
        try handler.perform([request])
    }catch{
        print(error)
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是迄今为止的结果.

结果1(没有翻转y) 结果1

结果2(翻转y) 结果2



我自己为rect找到了一个解决方案.

let rect=face_obs.boundingBox
let x=rect.origin.x*final_image.size.width
let w=rect.width*final_image.size.width
let h=rect.height*final_image.size.height
let y=final_image.size.height*(1-rect.origin.y)-h
let conv_rect=CGRect(x: x, y: y, width: w, height: h)
Run Code Online (Sandbox Code Playgroud)

但是,我认为@ wei-jay的答案是好的,因为它更优雅.

Wil*_*jay 6

您必须根据图像进行转换和缩放.

func drawVisionRequestResults(_ results: [VNFaceObservation]) {
    print("face count = \(results.count) ")
    previewView.removeMask()

    let transform = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -self.view.frame.height)

    let translate = CGAffineTransform.identity.scaledBy(x: self.view.frame.width, y: self.view.frame.height)

    for face in results {
        // The coordinates are normalized to the dimensions of the processed image, with the origin at the image's lower-left corner.
        let facebounds = face.boundingBox.applying(translate).applying(transform)
        previewView.drawLayer(in: facebounds)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个答案对我不起作用.为了使它工作,我不得不将变换更改为:let transform = CGAffineTransform(scaleX:-1,y:-1).translatedBy(x:-self.view.frame.width,y:-self.view.frame.高度) (2认同)

Vib*_*yal 5

我尝试了多种方法,以下是最适合我的方法:

dispatch_async(dispatch_get_main_queue(), ^{
    VNDetectedObjectObservation * newObservation = request.results.firstObject;
    if (newObservation) {
        self.lastObservation = newObservation;
        CGRect transformedRect = newObservation.boundingBox;
        CGRect convertedRect = [self.previewLayer rectForMetadataOutputRectOfInterest:transformedRect];
        self.highlightView.frame = convertedRect;
    }
});
Run Code Online (Sandbox Code Playgroud)


bit*_*yte 5

有内置的方法可以帮助您。要从规范化格式转换,请使用以下命令:

func VNImageRectForNormalizedRect(_ normalizedRect: CGRect, _ imageWidth: Int, _ imageHeight: Int) -> CGRect
Run Code Online (Sandbox Code Playgroud)

反之亦然:

func VNNormalizedRectForImageRect(_ imageRect: CGRect, _ imageWidth: Int, _ imageHeight: Int) -> CGRect
Run Code Online (Sandbox Code Playgroud)

类似的积分方法:

func VNNormalizedFaceBoundingBoxPointForLandmarkPoint(_ faceLandmarkPoint: vector_float2, _ faceBoundingBox: CGRect, _ imageWidth: Int, _ imageHeight: Int) -> CGPoint
func VNImagePointForNormalizedPoint(_ normalizedPoint: CGPoint, _ imageWidth: Int, _ imageHeight: Int) -> CGPoint
Run Code Online (Sandbox Code Playgroud)