> For the complete documentation index, see [llms.txt](https://docs.recognito.vision/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.recognito.vision/face-recognition-sdk/integration-guide/ios/sample-application.md).

# Sample Application

1:N Face Identification and Liveness Detection SDK iOS Demo

{% embed url="<https://www.youtube.com/watch?v=9HM70PFa4lQ>" %}
NIST FRVT #1 Face Recognition, Liveness Detection Mobile SDK Demo
{% endembed %}

***

## Build Project

### - Download and Open Project

* Download [**Recognito\_Face\_iOS.zip \[21.8M\]**](https://www.dropbox.com/scl/fi/2f9kmzk4pvt1a1835c04t/Recognito_Face_iOS.zip?rlkey=xqny98ifgsgoglgwfc6edvsvb\&st=w8h1uv4b\&dl=0)
* Open the `FaceDemo` project in Xcode.

### - Setting Up SDK License Key

* Add your license to `license.txt` file:

{% code title="iOS-FaceRecognition-FaceLivenessDetection/license.txt" lineNumbers="true" %}

```
 jwIUC3mm7P9uJDPxx/gRUttdS3bDg5n3QFnyySRB/E776MSPQAMubdyFTuzFb7BCdOx4EuoHmcsO 
 rpeGfI+Z371p/7cInVsnxLZU7PcSJh45Dd7c6maTg0QPwNLDHqdyNzRLFwKXXOX/IVuQNh6Dsen1 
 mwk6RGQZfReSUU6nLvWzC5sPgYhBVemExbbIa3UdDbBC+Bm4qNeXQ2i/08s9GFrhhbuvdYyI5TGl 
 6aVFjSGoHQhm/ENI1916+ck7BiguXMA1KFRTlchSWKhyb9CllHOGTomkoBUD3ykLlMGkcuZE8wT6 
 qpaHrtnmz1TgkP3tjK7L61BaEpWNib3uH29hWA== 
```

{% endcode %}

* Build Project.

### - Integration Guide

* Activate and Initialize FaceSDK

```swift
override func viewDidLoad() {
    super.viewDidLoad()
    var ret = SDK_LICENSE_KEY_ERROR.rawValue
    if let filePath = Bundle.main.path(forResource: "license", ofType: "txt") {
        do {
            let license = try String(contentsOfFile: filePath, encoding: .utf8)
            ret = FaceSDK.setActivation(license)
        } catch {
            print("Error reading file: \(error)")
        }
    } else {
        print("File not found")
    }
    
    if(ret == SDK_SUCCESS.rawValue) {
        ret = FaceSDK.initSDK()
    }
```

* Detect Face from Camera Frame

```swift
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {    
    guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
    
    CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.readOnly)
    let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
    
    let context = CIContext()
    let cgImage = context.createCGImage(ciImage, from: ciImage.extent)
    let image = UIImage(cgImage: cgImage!)
    CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.readOnly)

    // Rotate and flip the image
    var capturedImage = image.rotate(radians: .pi/2)
    if(cameraLens_val == .front) {
        capturedImage = capturedImage.flipHorizontally()
    }
    
    let faceBoxes = FaceSDK.faceDetection(capturedImage)
```

* Extract Face Template

```swift
let faceBox = faceBoxes[0] as! FaceBox 
if(faceBox.liveness > livenessThreshold) {
    let templates = FaceSDK.templateExtraction(capturedImage, faceBox: faceBox)
    ...
```

* Calculate Similarity

```swift
let similarity = FaceSDK.similarityCalculation(templates, templates2: personTemplates)                    
```

***

## Application UI

<div><figure><img src="https://3902005046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxiEW2bRNeDz3JZIL41gD%2Fuploads%2FsDcnLFPu42H6uK3zUQTJ%2F1.png?alt=media&amp;token=83486b95-9f77-4bc8-9627-34b93fe9f632" alt=""><figcaption></figcaption></figure> <figure><img src="https://3902005046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxiEW2bRNeDz3JZIL41gD%2Fuploads%2FkFebdsyp7BfncXZOYjZ7%2F2.png?alt=media&amp;token=9c5a5781-146b-4db7-961f-73d31b8af6f0" alt=""><figcaption></figcaption></figure> <figure><img src="https://3902005046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxiEW2bRNeDz3JZIL41gD%2Fuploads%2FyFsWbKU9Czqjxtfz5HRe%2F3.png?alt=media&amp;token=05748803-46ed-416f-a3e2-32967e94e952" alt=""><figcaption></figcaption></figure> <figure><img src="https://3902005046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxiEW2bRNeDz3JZIL41gD%2Fuploads%2FBHW2lYoHHPXT7Isb4zAZ%2F4.png?alt=media&amp;token=711cd109-8a58-4ae9-9ff4-8ec72bb36fef" alt=""><figcaption></figcaption></figure> <figure><img src="https://3902005046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxiEW2bRNeDz3JZIL41gD%2Fuploads%2F5guhoNH6euD9tOAtJFo6%2F5.png?alt=media&amp;token=68af3035-3277-4aa8-91f2-53fd5a8d4aa4" alt=""><figcaption></figcaption></figure> <figure><img src="https://3902005046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxiEW2bRNeDz3JZIL41gD%2Fuploads%2Fd8MXTPHd2CNpoF9BDNRL%2F6.png?alt=media&amp;token=b73ba001-ca88-4bc6-a266-46051b94ee57" alt=""><figcaption></figcaption></figure></div>
