【问题标题】:Getting depth data from custom camera从自定义相机获取深度数据
【发布时间】:2018-10-01 22:18:03
【问题描述】:

我已关注Capturing Photos with Depth 并查看了the similar question 中的所有建议,但是,我无法从我的自定义相机中获取任何深度数据。这是我对代码的最新编辑,您对这个问题有任何想法吗?

当我点击相机按钮时,我得到:

libc++abi.dylib:以 NSException 类型的未捕获异常终止

我也查看了解决方案。它们主要与segue有关,但我仔细检查了这部分代码和故事板,看起来还不错。 (在添加深度代码之前我没有任何问题!)

class CameraViewController : UIViewController {
  @IBOutlet weak var cameraButton: UIButton!

  var captureSession = AVCaptureSession()
  var captureDevice: AVCaptureDevice?
  var photoOutput: AVCapturePhotoOutput?
  var cameraPreviewLayer: AVCaptureVideoPreviewLayer?

  var image: UIImage?

  var depthDataMap: CVPixelBuffer?
  var depthData: AVDepthData?

  override func viewDidLoad() {
    super.viewDidLoad()

    setupDevice()
    setupIO()
    setupPreviewLayer()
    startRunningCaptureSession()
  }

  func setupDevice() {
    self.captureDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
  }

  func setupIO() {
    guard let captureInputDevice = try? AVCaptureDeviceInput(device: self.captureDevice!),
      self.captureSession.canAddInput(captureInputDevice)
      else { fatalError("Can't add video input.") }
    self.captureSession.beginConfiguration()
    self.captureSession.addInput(captureInputDevice)

    self.photoOutput = AVCapturePhotoOutput()
    self.photoOutput!.isDepthDataDeliveryEnabled = photoOutput!.isDepthDataDeliverySupported
    guard self.captureSession.canAddOutput(photoOutput!)
      else { fatalError("Can't add photo output.") }
    self.captureSession.addOutput(photoOutput!)
    self.captureSession.sessionPreset = .photo
    self.captureSession.commitConfiguration()
  }

  func setupPreviewLayer() {
    self.cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
    self.cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    self.cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
    self.cameraPreviewLayer?.frame = self.view.frame
    self.view.layer.insertSublayer(self.cameraPreviewLayer!, at: 0)             
  }
  func startRunningCaptureSession() {
    self.captureSession.startRunning()
  }

  @IBAction func cameraButtonDidTap(_ sender: Any) {    
    let setting = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.hevc])
    setting.isDepthDataDeliveryEnabled = self.photoOutput!.isDepthDataDeliverySupported
    self.photoOutput?.capturePhoto(with: setting, delegate: self)
  }

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showPhoto" {
      let nav = segue.destination as! UINavigationController
      let previewVC = nav.topViewController as! PhotoViewController

      previewVC.image = self.image
      previewVC.depthData = self.depthData
      previewVC.depthDataMap = self.depthDataMap
    }
  }
}

extension CameraViewController: AVCapturePhotoCaptureDelegate{
  func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    if let imageData = photo.fileDataRepresentation() {
      image = UIImage(data: imageData)
      let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil)
      let auxiliaryData = CGImageSourceCopyAuxiliaryDataInfoAtIndex(imageSource!, 0, kCGImageAuxiliaryDataTypeDisparity) as? [AnyHashable: Any]

      let depthData = try? AVDepthData(fromDictionaryRepresentation: auxiliaryData!)
      self.depthDataMap = depthData?.depthDataMap

      self.performSegue(withIdentifier: "showPhoto", sender: self)
    }
  }
}

【问题讨论】:

    标签: swift camera ios11 iphone-x


    【解决方案1】:

    This is the problem with my code:

    除非照片输出被添加到会话并且会话的输入被正确配置以提供深度,否则将不支持深度数据传递。

    1. 先设置会话预设:

      self.captureSession.sessionPreset = .photo

    2. 添加双摄像头输入后,添加照片输出。

      保护 self.captureSession.canAddOutput(photoOutput!)

    3. 现在设置启用深度传递:

      self.photoOutput!.isDepthDataDeliveryEnabled = photoOutput!.isDepthDataDeliverySupported

    【讨论】:

      猜你喜欢
      • 2019-11-30
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 2022-06-16
      相关资源
      最近更新 更多