Detect and Recognize People in Photos

Use the C API to Run the Image Analyzer

Using the image analyzer is almost identical to using the object tracker in terms of configuration and creation.

// Using the C API directly to configure the image analyzer
 
let configRef = ARKImageAnalyzerConfigurationCreate()!
let envRef = ARKEnvironmentCopyNamed("com.real.PROD")
ARKImageAnalyzerConfigurationSetObject(configRef, kARKImageAnalyzerConfigurationKey_Environment, envRef)
 
let userRef = ARKUserCreate("<Username>", "<Password>")
ARKUserSetDirectory(userRef, "main")
ARKImageAnalyzerConfigurationSetObject(configRef, kARKImageAnalyzerConfigurationKey_User, userRef)
ARKObjectRelease(userRef);
ARKObjectRelease(envRef)
             
// Configure all the desired properties here
ARKImageAnalyzerConfigurationSetString(configRef, kARKImageAnalyzerConfigurationKey_SiteID, "Building 1")
ARKImageAnalyzerConfigurationSetString(configRef, kARKImageAnalyzerConfigurationKey_SourceID, "Camera 1!")
             
// Create the image analyzer
var callbacks : ARKImageAnalyzerCallbacks = ARKImageAnalyzerCallbacks()
callbacks.context = Unmanaged.passUnretained(self).toOpaque()
callbacks.didCompleteImageAnalysis = image_analyzer_did_complete_image_analysis
         
var imageAnalyzerRef = ARKImageAnalyzerCreate(configRef, &callbacks)
ARKObjectRelease(configRef)
 
// Give a CGImage create an ARKImage and pass it to the image analyzer
let image = ARKImageCreateWithCGImage(cgImage)!
             
ARKImageAnalyzerAnalyzeImage(imageAnalyzerRef, image)           
ARKObjectRelease(image)
 
 
private func image_analyzer_did_complete_image_analysis(_ analyzerRef: ARKImageAnalyzerRef, _ resultRef: ARKImageAnalysisResultRef, _ errorRef: ARKErrorRef?, _ context: UnsafeMutableRawPointer?) {
     
    let argusKitController = Unmanaged<ArgusKitController>.fromOpaque(context!).takeUnretainedValue()
    let analysisResult = AnalysisResult(analysisResultRef: resultRef)
 
  
    ARKObjectRelease(resultRef)
}

Use the Swift ArgusKit Classes to Run the Image Analyzer

The steps below highlight the integration points with the application view controller and the ArgusKit Swift classes.

  1. Create a variable that holds an instance of the ArgusKitController.

    // ArgusKit: Integration Point
    // Instance variable for the ArgusKitController
    private var argusKitController: ArgusKitController = ArgusKitController()
  2. In the method configureArgusKitController() locate the following code.

    // Load the image analyzer configuration from the preferences.  If there are no username/password credentials then ArgusKit will run in offline mode which means it will only detect faces and not recognize them.
    let imageAnalyzerConfiguration = ImageAnalyzerConfiguration.imageAnalyzerConfigurationFromAppPreferences()
    
    // Create the image analyzer with the specified configuration
    argusKitController.createImageAnalyzer(withConfiguration: imageAnalyzerConfiguration)
    
    // Set the image analyzer handler.  This will be called each time an image is analyzed.
    argusKitController.imageAnalysisResultHandler = {  [unowned self] (analysisResult: AnalysisResult) in self.handleAnalysisResult(analysisResult)
    }

    This creates an image analyzer configuration. This is read from the persistent preferences in the sample app and uses them along with some other hard coded values to create the image analyzer configuration. It then creates an image analyzer with the specified configuration. It also sets up the handler that will be called whenever the image analysis process completes. If no credentials are provided in the configuration then the image analyzer runs in offline mode and only detects faces locally. The cloud server won't be contacted for recognition in this case.

  3. The image analyzer needs to be provided with a photo image. This code below illustrates how this is done in the demo application.

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let selectedImage = info[.originalImage] as? UIImage {
    
            let orientedUpImage = selectedImage.orientUp()
    
            // ArgusKit: Integration Point
            // Get an image and send it to the image analyzer
            if let cgImage = orientedUpImage.cgImage {
                argusKitController.analyzeImage(cgImage)
            }
    
            let navigationController = PhotoViewController.photoViewControllerInNavigationController(storyboard: storyboard!, photoImage: selectedImage, argusKitController: argusKitController) {
              picker.dismiss(animated: true, completion: nil)
            }
    
            picker.present(navigationController, animated: true, completion: nil)
        }
    }
  4. Implement a completion handler to receive the image analysis results.

    // ArgusKit: Integration Point
    // Receive image analysis events from the image analyzer
    private func handleAnalysisResult(_ analysisResult: AnalysisResult) {
    
        // This is called every time a change occurs in the current tracking result.
        DispatchQueue.main.async {
    
            // The PhotoViewController is presented on top of the UIImagePickerController.  The purpose of this forwarding is to keep all of the ArgusKitController code in this class so it is easier to follow rather than
            // spreading it out in a bunch of classes.  The PhotoViewController will only ready the analysis results and display them very similar to how the object tracking code works in this class.  In a standard application
            // this would be designed differently.
            if let navigationController = self.presentedViewController?.presentedViewController as? UINavigationController, let photoViewController = navigationController.topViewController as? PhotoViewController {
                photoViewController.imageAnalysisDidComplete(imageAnalysis: analysisResult)
            }
        }
    }

For more information, look in the class CameraViewController for comments that begin with // ArgusKit: Integration Point.

See Also