gpt4 book ai didi

ios - CIDetector,检测到的人脸图像没有显示?

转载 作者:行者123 更新时间:2023-11-28 15:44:38 26 4
gpt4 key购买 nike

我正在使用 CIDetector 检测 UIImage 中的人脸。我正确地得到了面部矩形,但是当我将图像裁剪为检测到的面部矩形时。它没有显示在我的 ImageView 中。

我已经检查过了。我的形象不是零

这是我的代码:-

@IBAction func detectFaceOnImageView(_: UIButton) {
let image = myImageView.getFaceImage()
myImageView.image = image
}


extension UIView {

func getFaceImage() -> UIImage? {
let faceDetectorOptions: [String: AnyObject] = [CIDetectorAccuracy: CIDetectorAccuracyHigh as AnyObject]

let faceDetector: CIDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: faceDetectorOptions)!

let viewScreenShotImage = generateScreenShot(scaleTo: 1.0)

if viewScreenShotImage.cgImage != nil {
let sourceImage = CIImage(cgImage: viewScreenShotImage.cgImage!)
let features = faceDetector.features(in: sourceImage)
if features.count > 0 {
var faceBounds = CGRect.zero
var faceImage: UIImage?
for feature in features as! [CIFaceFeature] {
faceBounds = feature.bounds
let faceCroped: CIImage = sourceImage.cropping(to: faceBounds)
faceImage = UIImage(ciImage: faceCroped)
}
return faceImage
} else {
return nil
}
} else {
return nil
}
}

func generateScreenShot(scaleTo: CGFloat = 3.0) -> UIImage {
let rect = self.bounds
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
let context = UIGraphicsGetCurrentContext()
self.layer.render(in: context!)
let screenShotImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let aspectRatio = screenShotImage.size.width / screenShotImage.size.height
let resizedScreenShotImage = screenShotImage.scaleImage(toSize: CGSize(width: self.bounds.size.height * aspectRatio * scaleTo, height: self.bounds.size.height * scaleTo))
return resizedScreenShotImage!
}
}

有关更多信息,我附上了值的屏幕截图。

屏幕截图 1 Screen Shot 1

屏幕截图 2 Screen Shot 2

屏幕截图 3 Screen Shot 3

最佳答案

试试这个:

let faceCroped: CIImage = sourceImage.cropping(to: faceBounds)
//faceImage = UIImage(ciImage: faceCroped)
let cgImage: CGImage = {
let context = CIContext(options: nil)
return context.createCGImage(faceCroped, from: faceCroped.extent)!
}()

faceImage = UIImage(cgImage: cgImage)

关于ios - CIDetector,检测到的人脸图像没有显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43308621/

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