简体   繁体   English

无法使AVPortraitEffectsMatte代码正常工作

[英]Can't get AVPortraitEffectsMatte code to work

I'm trying to follow some code provided by Apple to retrieve an image mask from portrait mode photos using some new classes and objects introduced in iOS 12. The code is here: 我正在尝试遵循Apple提供的一些代码,以使用iOS 12中引入的一些新类和对象从人像模式照片中检索图像蒙版。代码在这里:

https://developer.apple.com/documentation/avfoundation/avportraiteffectsmatte/extracting_portrait_effects_matte_image_data_from_a_photo https://developer.apple.com/documentation/avfoundation/avportraiteffectsmatte/extracting_portrait_effects_matte_image_data_from_a_photo

func portraitEffectsMatteImageAt(_ path: String) -> UIImage? {
    let bundlePath = Bundle.main.bundlePath

    // Check that the image at given path contains auxiliary PEM data:
    guard let fileURL = NSURL(fileURLWithPath: bundlePath).appendingPathComponent(path),
    let source = CGImageSourceCreateWithURL(fileURL as CFURL, nil),
    let auxiliaryInfoDict = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypePortraitEffectsMatte) as? [AnyHashable: Any],
    let matteData = try? AVPortraitEffectsMatte(fromDictionaryRepresentation: auxiliaryInfoDict),
    let matteCIImage = CIImage(portaitEffectsMatte: matteData)
    else {
        return nil
    }
    return UIImage(ciImage: matteCIImage)
}

My only change is basically modifying the fileURL to use a jpg in my bundle. 我唯一的变化是基本上将fileURL修改为在我的捆绑包中使用jpg。 :

guard let fileURL = Bundle.main.url(forResource: "custom00", withExtension: "jpg")

However, stepping through the code allows me to see that the assignment of auxiliaryInfoDict is nil . 但是,单步执行代码使我看到auxiliaryInfoDict的分配为nil I imported these JPGs from a previous project that utilized older techniques to create depth masks ( https://www.raywenderlich.com/314-image-depth-maps-tutorial-for-ios-getting-started ) so the jpg files should be fine. 我从以前的项目中导入了这些JPG,该项目使用了较旧的技术来创建深度蒙版( https://www.raywenderlich.com/314-image-depth-maps-tutorial-for-ios-getting-started ),因此jpg文件应该没事的。

Does anyone have a working sample project? 有人有工作示例项目吗? Thanks 谢谢

You can load the portrait effects matte saved in a photo only if that photo has a portrait effects matte saved in it. 仅当照片中保存了肖像效果遮罩时,才能加载照片中保存的肖像效果遮罩。 That sounds like a tautology , so let me expand it: 这听起来像是重言式 ,所以让我扩展一下:

  • If you captured the image yourself using AVCapturePhotoOutput , you get a portrait effects matte if and only if: 如果您使用AVCapturePhotoOutput自己捕获图像,则在且仅在以下情况下,才能获得哑光肖像效果:

    • It's available/supported on the current capture device and configuration. 当前捕获设备和配置上可用/受支持。 Portrait effects require depth capture, so you need to have selected the back dual camera or the front TrueDepth camera (on a device so equipped) and enabled depth delivery . 纵向效果需要深度捕捉,因此您需要选择后置双摄像头或前置TrueDepth摄像头(在配备如此的设备上)并启用深度传送

    • You request it. 您要求它。 Set isPortraitEffectsMatteDeliveryEnabled in your photo settings before capture. 拍摄前在照片设置中设置isPortraitEffectsMatteDeliveryEnabled

    • The device can generate one. 该设备可以生成一个。 Portrait effects mattes come from a machine learning model trained to recognize human features. 人像效果遮罩来自经过训练可识别人脸特征的机器学习模型。 If there's no identifiable person in your photo, you don't get a matte. 如果您的照片中没有可识别的人,那么您就不会有遮罩。 (Sorry, pet lovers.) (对不起,宠物爱好者。)

    • You don't opt out of saving it. 您不会选择不保存它。 You can turn off embedsPortraitEffectsMatteInPhoto , or use AVCapturePhotoFileDataRepresentationCustomizer to replace/remove a photo's matte (or other elements) after capture and before saving. 您可以关闭embedsPortraitEffectsMatteInPhoto ,或使用AVCapturePhotoFileDataRepresentationCustomizer在捕获后和保存前替换/删除照片的遮罩(或其他元素)。 Obviously, if you want the matte, don't get rid of it. 显然,如果您想要遮罩,就不要摆脱它。

  • The same goes for images saved by any third-party app that uses the camera capture APIs. 由使用相机捕获API的任何第三方应用程序保存的图像也是如此。 (That is, you can read mattes from images saved by other apps if they followed the above steps, just the same as you would if you were trying to capture an image with a matte.) See Configuring Camera Capture to Collect a Portrait Effects Matte . (也就是说,如果其他应用遵循上述步骤,则可以从其他应用程序保存的图像中读取遮罩,与尝试使用遮罩捕获图像时一样。)请参阅配置相机捕捉以收集人像效果遮罩。

  • If you captured a photo using Apple's built-in Camera app, it needs to be a Portrait Mode photo (back dual camera or front TrueDepth camera) captured on iOS 12. 如果您使用Apple的内置Camera应用程序捕获了照片,则该照片必须是在iOS 12上捕获的人像模式照片(后置双摄像头或前置TrueDepth摄像头)。

该API似乎适用于前置摄像头照片,但是尚不适用于前置摄像头照片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM