gpt4 book ai didi

ios - 将 Vision boundingBox 从 VNFaceObservation 转换为矩形以在图像上绘制

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:35 27 4
gpt4 key购买 nike

我正在尝试使用新的 Vision API 中的 VNDetectFaceRectanglesRequest 来检测图像上的人脸。然后,我在每个检测到的脸上画一个红色矩形。

但我在将 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)
}
}

这是目前的结果。

结果 1(不翻转 y) Result 1

结果 2(翻转 y) Result 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)

但是,我将@wei-jay 的回答标记为好回答,因为它更优雅。

最佳答案

有内置方法可以为您完成。要从规范化形式转换,请使用:

func VNImageRectForNormalizedRect(_ normalizedRect: CGRect, _ imageWidth: Int, _ imageHeight: Int) -> CGRect

反之亦然:

func VNNormalizedRectForImageRect(_ imageRect: CGRect, _ imageWidth: Int, _ imageHeight: Int) -> CGRect

积分的类似方法:

func VNNormalizedFaceBoundingBoxPointForLandmarkPoint(_ faceLandmarkPoint: vector_float2, _ faceBoundingBox: CGRect, _ imageWidth: Int, _ imageHeight: Int) -> CGPoint
func VNImagePointForNormalizedPoint(_ normalizedPoint: CGPoint, _ imageWidth: Int, _ imageHeight: Int) -> CGPoint

关于ios - 将 Vision boundingBox 从 VNFaceObservation 转换为矩形以在图像上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44724257/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com