Photogrammetry on iOS 17.0+ with TrueDepth

Hello,

I came across the Object Capture for iOS example from WWDC23, which utilizes LiDAR sensor.

However, I’m interested in using the TrueDepth camera system instead. What I have tried is to save depth photos (.HEIC) to the Images/ folder (based on modifying the example below), which is hopefully used by the Photogrammetry session. But I haven’t been successful so far in starting the 3D reconstruction. Could there be something I’ve missed, or is the Object Capture sample code exclusively designed for LiDAR? Or maybe .HEIC is not the right format to use?

Thank you for your assistance.

import AVFoundation
import UIKit

class DepthPhotoCapture: NSObject, AVCapturePhotoCaptureDelegate {
    let photoOutput = AVCapturePhotoOutput()
    let captureSession = AVCaptureSession()

    override init() {
        super.init()
        setupCaptureSession()
    }

    func setupCaptureSession() {
        // Get the front camera (TrueDepth camera)
        guard let frontCamera = AVCaptureDevice.default(.builtInTrueDepthCamera, for: .video, position: .front) else {
            print("Unable to access front camera!")
            return
        }

        do {
            // Create an input object from the camera
            let input = try AVCaptureDeviceInput(device: frontCamera)
            
            // Add the input to the capture session
            captureSession.addInput(input)
        } catch {
            print("Unable to create AVCaptureDeviceInput: \(error)")
        }

        // Check if depth data capture is supported
        if photoOutput.isDepthDataDeliverySupported {
            // Enable depth data capture
            photoOutput.isDepthDataDeliveryEnabled = true
        }

        // Add the photo output to the capture session
        captureSession.addOutput(photoOutput)

        // Start the capture session
        captureSession.startRunning()
    }

    func captureDepthPhoto() {
        // Create a photo settings object
        let photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.hevc])
        photoSettings.isDepthDataDeliveryEnabled = photoOutput.isDepthDataDeliveryEnabled

        // Capture a photo with depth data
        photoOutput.capturePhoto(with: photoSettings, delegate: self)
    }

    // Implement the AVCapturePhotoCaptureDelegate method
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        guard let imageData = photo.fileDataRepresentation() else {
            print("Error while generating image from photo capture data.")
            return
        }
        
        // Get the documents directory
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        
        // Append the image directory and a unique image name
        let fileURL = documentsDirectory.appendingPathComponent("Images/").appendingPathComponent(UUID().uuidString).appendingPathExtension("heic")
        
        do {
            // Write the image data to the file
            try imageData.write(to: fileURL)
            print("Saved photo with depth data to \(fileURL)")
        } catch {
            print("Failed to write the image data to disk: \(error)")
        }
    }
}
Post not yet marked as solved Up vote post of cy_w Down vote post of cy_w
380 views
  • did you every figure this out? I am looking to do the same thing.

Add a Comment