Skip to content
imaged

Object detection

Run detection on camera frames and read boxes in image coordinates.

Use your own .slab model and a runtime license. Model files carry their input sizes, preprocessing, labels, and backend preferences.

CameraDetector.swift

Swift setup →

Run inference off the main thread.

// Live object detection on camera frames.
//
// Attach a CameraDetector as the sample-buffer delegate of an
// AVCaptureVideoDataOutput whose videoSettings request 32BGRA pixels:
//
//     let output = AVCaptureVideoDataOutput()
//     output.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA]
//     output.alwaysDiscardsLateVideoFrames = true
//     output.setSampleBufferDelegate(detector, queue: DispatchQueue(label: "detector"))
//
// Boxes are reported in the camera frame's pixel coordinates.

import AVFoundation
import SlabSwift

final class CameraDetector: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
    private let session: SlabSession
    private let input = SlabInput()
    private let output = SlabOutput()
    var onDetections: (([SlabBox], Double) -> Void)?

    init(modelPath: String, license: String) throws {
        let config = try SlabConfig(cacheDirectory: NSTemporaryDirectory(), license: license)
        session = try SlabSession(config: config, path: modelPath, warmup: true)
    }

    func captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer,
                       from connection: AVCaptureConnection) {
        guard let pixels = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
        do {
            // Slab copies the frame and prepares it the way the model expects.
            input.clear()
            input.setFrame(try SlabFrame(pixelBuffer: pixels))
            try session.run(input, into: output)
            let boxes = try output.boxes(scoreThreshold: 0.4, iouThreshold: 0.45, maxBoxes: 100)
            onDetections?(boxes, output.latencyMs)
        } catch {
            print("detection failed: \(error)")
        }
    }
}