小编And*_*rra的帖子

如何从AVCapturePhoto生成具有正确方向的UIImage?

我正在调用AVFoundation的委托方法来处理照片捕获,但是我很难将它生成的AVCapturePhoto转换为具有正确方向的UIImage.虽然下面的例程是成功的,但我总是得到一个面向右侧的UIImage(UIImage.imageOrientation = 3).使用UIImage(数据:图像)并尝试首先使用photo.cgImageRepresentation()时,我无法提供方向?takeRetainedValue()也没有帮助.请协助.

在此处,图像方向至关重要,因为生成的图像将被输入Vision框架工作流程.

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    // capture image finished
    print("Image captured.")
    if let imageData = photo.fileDataRepresentation() {
        if let uiImage = UIImage(data: imageData){
            // do stuff to UIImage
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新1: 阅读Apple的Photo Capture编程指南(iOS11过时),我确实找到了一件我做错的事情:

  1. 在每次捕获调用(self.capturePhotoOutput.capturePhoto)时,必须与PhotoOutput对象建立连接并更新其方向以匹配拍摄照片时设备的方向.为此,我创建了UIDeviceOrientation的扩展,并在我创建的snapPhoto()函数上使用它来调用捕获例程并等待执行didFinishProcessingPhoto委托方法.我添加了代码的快照,因为这里的代码示例分隔符似乎没有正确显示它们. 在此输入图像描述 在此输入图像描述

更新2 链接到GitHub上的完整项目:https://github.com/agu3rra/Out-Loud

avfoundation avcapturesession swift4 ios11

15
推荐指数
4
解决办法
6399
查看次数

有没有办法在触发视觉框架请求(VNDetectTextRectanglesRequest)后取消它?

我正在为 iOS 应用程序运行 Vision Framework 请求,如下所示:

let textDetectionRequest = VNDetectTextRectanglesRequest(completionHandler: self.findTextBoxes)
let textDetectionHandler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
DispatchQueue.global(qos: .background).async {
   do {
        try textDetectionHandler.perform([textDetectionRequest])
   } catch {
        print(error)
   }
}
Run Code Online (Sandbox Code Playgroud)

它工作正常,我正在使用通知/观察者模式在请求完成后采取进一步的操作。有时,在应用程序中,我需要在请求完成其操作之前取消进程,那么我可以使用 Vision Framework 中的任何类型的协议/委托方法来调用取消吗?

谢谢。

computer-vision ios swift-protocols swift4

6
推荐指数
1
解决办法
433
查看次数

如何从 AVCapturePhoto 获取元数据值?

我目前正在使用 AVFoundation 捕获图像。由于我想在 Vision Framework 工作流程中使用捕获的图像,因此在将其转换为 UIImage 时需要其方向。我怎样才能做到这一点?从文档中我发现 AVCapturePhoto 有一个 .metadata 字典来访问该信息,但如果我使用相应的密钥,结果为零。

这是我的捕获例程的委托方法:

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    // capture image finished
    print("Image captured.")
    print(photo)
    print(photo.metadata["kCGImagePropertyOrientation"]) // CGImageProperties for metadata keys for value retrieval.
Run Code Online (Sandbox Code Playgroud)

我在“CGImageProperties>Individual Image Properties”下找到了该键。打印(照片)确实向我显示了实际上已捕获的图像,返回:

AVCapturePhoto:0x1c1013940 点:98386.095931 1/1 设置:uid:3 照片:{4032x3024 SIS:ON}

请用 Swift 回答。:)

谢谢!

avfoundation ios avcapturesession swift

4
推荐指数
1
解决办法
1465
查看次数

为什么人脸检测只能与 CGImagePropertyOrientation.right 一起使用?

我已经创建了以下链接中提供的 Xcode 项目,以便探索 Apple 使用 Xcode9 发布的新 Vision Framework。它基于我在 GitHub 和 Youtube 上找到的代码所做的调整,但我无法理解为什么 VNImageRequestHandler 上的方向参数上的 CGImagePropertyOrientation.right 。有人可以阐明这一点吗?

GitHub Xcode 项目

ios swift4 apple-vision

1
推荐指数
1
解决办法
2426
查看次数