Search code examples
iosobjective-ciphoneaffdex-sdk

Affdex AFDXDetector delegate functions are never called when using the camera?


I’m having some trouble getting the Affdex iOS SDK to work with streaming input from the onboard camera. I’m using XCode 7.1.1 and an iPhone 5S. Here’s my initialization code:

let detector = AFDXDetector.init(delegate: self, usingCamera: AFDX_CAMERA_FRONT, maximumFaces: 1)
detector.setDetectAllEmotions(true)
detector.setDetectAllExpressions(true)
detector.maxProcessRate = 5.0
detector.licensePath = NSBundle.mainBundle().pathForResource("[email protected]", ofType: "license”)

if let error = detector.start() {
    log.warning("\(error)")
}

No error is produced by detector.start() and the app requests access to the camera the first time it is called, as expected. However, none of the delegate functions are ever called. I have tested with both AFDX_CAMERA_FRONT and AFDX_CAMERA_BACK.

I am able to process single images captured by the onboard camera as expected using the following:

let detector = AFDXDetector(delegate: self, discreteImages: true, maximumFaces: 1)
detector.setDetectAllEmotions(true)
detector.setDetectAllExpressions(true)
detector.licensePath = NSBundle.mainBundle().pathForResource("[email protected]", ofType: "license")

if let error = detector.start() {
    log.warning("\(error)")
}

detector.processImage(image)

Am I missing something obvious?


Solution

  • The issue appears to be the declaration of the detector variable. The lifetime of that variable is only scoped for the function if you declare it inside of the function — it is deallocated when the function exits.

    Make the variable an instance variable in the class; this guarantees its lifetime is for the life of the object that it is instantiated in, and the delegate functions should also be called.