in depth-anything-example/DepthCLI/MainCommand.swift [26:67]
mutating func run() async throws {
// Compile and load the model
let config = MLModelConfiguration()
config.computeUnits = .cpuAndGPU
let compiledURL = try await MLModel.compileModel(at: URL(filePath: model))
let model = try MLModel(contentsOf: compiledURL, configuration: config)
// Load the input image
guard let inputImage = CIImage(contentsOf: URL(filePath: input)) else {
print("Failed to load image.")
throw ExitCode(EXIT_FAILURE)
}
print("Original image size \(inputImage.extent)")
// Resize the image to match the model's expected input
let resizedImage = inputImage.resized(to: targetSize)
// Convert to a pixel buffer
guard let pixelBuffer = context.render(resizedImage, pixelFormat: kCVPixelFormatType_32ARGB) else {
print("Failed to create a pixel buffer.")
throw ExitCode(EXIT_FAILURE)
}
// Execute the model
let clock = ContinuousClock()
let start = clock.now
let featureProvider = try MLDictionaryFeatureProvider(dictionary: ["image": pixelBuffer])
let result = try await model.prediction(from: featureProvider)
guard let outputPixelBuffer = result.featureValue(for: "depth")?.imageBufferValue else {
print("The model did not return a 'depth' feature with an image.")
throw ExitCode(EXIT_FAILURE)
}
let duration = clock.now - start
print("Model inference took \(duration.formatted(.units(allowed: [.seconds, .milliseconds])))")
// Undo the scale to match the original image size
var outputImage = CIImage(cvPixelBuffer: outputPixelBuffer)
outputImage = outputImage.resized(to: CGSize(width: inputImage.extent.width, height: inputImage.extent.height))
// Save the depth image
context.writePNG(outputImage, to: URL(filePath: output))
}