Basic usage
Note
For a complete sample of real-time recognition of a video stream from the device's camera, see example app.
Before proceeding with this procedure, you must have followed the steps described in Adding SDK and authenticating.
The basic use of Live Sense SDK includes the detection of cars, pedestrians,signs, or other supported objects in a live camera feed.
For details on what can be detected by each model, see Models.
In your view controller, use the following code to run Roadbasics
, the default model for detection.
@IBOutlet weak var previewView: UIView!
var videoCapture: LSDVideoCapture = LSDVideoCapture()
var lsdRecognition : LSDPixelBufferRecognition?
override func viewDidLoad() {
videoCapture.delegate = self
lsdRecognition = LSDPixelBufferRecognition()
lsdRecognition?.delegate = self
var modelOptions : ModelOptions = ModelOptions()
let rbModelOptions : RoadBasicsModelOptions = RoadBasicsModelOptions()
let rbModel = RoadBasicsModel(options: modelOptions, rbModelOptions: rbModelOptions)
videoCapture.setUp(sessionPreset: .hd1280x720) { [unowned self] success in
if success {
if let previewLayer = self.videoCapture.previewLayer {
self.previewView.layer.addSublayer(previewLayer)
self.resizePreviewLayer()
}
self.setUpBoundingBoxViews()
self.videoCapture.start()
self.lsdRecognition?.initiateDetectionRequest(isParallel: true, forModels:[rbModel]) { (success) in
}
}
}
}
For passing other models, you can pass the model key for that model. You can get the model keys for the following code, or refer to Models for more details:
public func getModelData() -> Array<ModelConfiguration>
To get the stats for detections, implement the LSDVideoCaptureStatisticsDelegate
methods.
@objc public protocol LSDVideoCaptureStatisticsDelegate: class {
@objc optional func getVideoStats(inferenceTime: Int, executionTime: Int, fps: Int, droppedFrames : Int)
@objc optional func output(sampleBuffer: CMSampleBuffer,videoOutput:AVCaptureVideoDataOutput)
@objc optional func displayFrameCounter(_ frame: Int)
@objc optional func sendObservationInformation(_ aObservationInfo: String, aConfidence: Float)
@objc optional func sendObservation(infoDictionary aDict: NSDictionary)
@objc optional func alertReceived(alerts : [LSDAlert])
}
Note
Use the following recognition session lifecycle methods in your view controller's lifecycle methods - Method 1 and Method 2.
override func viewDidAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(willBecomeActive), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func willResignActive() {
videoCaptureView.pauseRecognition()
}
@objc func willBecomeActive() {
videoCaptureView.startRecognition()
}