使用视觉框架点裁剪的图像不正确

时间:2019-05-28 09:34:18

标签: ios swift vision

  

我正在尝试使用面部界标点修剪眉毛,但是由于某些原因,使用界标点修剪的图像不正确。

//Delegate which captures the image from camera and assign it to the caputuredImage which as an UIImage

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    // 1
    guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
      return
    }

    let ciImage = CIImage(cvPixelBuffer: imageBuffer)
    guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else {
      return
    }
    caputuredImage = UIImage(cgImage: cgImage)
}

// rect is calculated using lefteyebrow cgpoints from vision framework
let rect = self.pointToRect(pointsArray: self.faceView.leftEyebrow)

someImageView = self.cropImage(image: caputuredImage, toRect: rect)

func cropImage(image: UIImage, toRect: CGRect) -> UIImage? {
    // Cropping is available trhough CGGraphics
    let cgImage :CGImage! = image.cgImage
    let croppedCGImage: CGImage! = cgImage.cropping(to: toRect)

    return UIImage(cgImage: croppedCGImage, scale: 1.0, orientation: .upMirrored)
}

0 个答案:

没有答案