Face Template Utility
Utility for converting and comparing raw Ver-ID face templates
Obtaining a raw face template
- Follow the instructions to install Ver-ID SDK for iOS.
Example function to detect a face and get its raw template:
import VerIDCore func rawFaceTemplateFromImage(url: URL) -> [Float]? { let verid = VerIDFactory().createVerIDSync() guard let image = VerIDImage(url: url) else { // Failed to create image from URL return nil } guard let faces = try? verid.faceDetection.detectFacesInImage(image, limit: 1), !faces.isEmpty else { // No face found in image return nil } guard let faceRecognition = verid.faceRecognition as? VerIDFaceRecognition else { // Using face recognition implementation that does not support extracting raw face templates return nil } guard let recognizableFace = (try? faceRecognition.createRecognizableFacesFromFaces(faces, inImage: image))?.first else { // Failed to create recognizable face return nil } guard let template = try? faceRecognition.rawFaceTemplate(fromFace: recognizableFace) else { // Failed to get raw template return nil } // Convert from [NSNumber] to [Float] return template.map({ $0.floatValue }) }
Converting face templates to strings
If you’re going to be storing raw face templates you may want to convert them to strings.
Convert face template to string:
import FaceTemplateUtility let faceTemplate: [Float] // Raw face template let string: String = try String(from: faceTemplate)
Convert string to face template:
import FaceTemplateUtility let string: String // Face template converted to string let faceTemplate: [Float] = try string.faceTemplate()
Custom face template types
You can encode and decode face templates to your own type by conforming to the FaceTemplateConvertible
protocol.
The framework already contains extensions to String
and Data
that conform to the FaceTemplateConvertible
protocol.
Create a class that conforms to the
FaceTemplateConvertible
protocol. In this example the template will be encoded in JSON:import FaceTemplateUtility class MyFaceTemplateClass: FaceTemplateConvertible { let encodedTemplate: Data public init(encodedTemplate: Data) { self.encodedTemplate = encodedTemplate } // MARK: - FaceTemplateConvertible required public init(from faceTemplate: [Float]) throws { self.encodedTemplate = try JSONEncoder().encode(faceTemplate) } public func faceTemplate() throws -> [Float] { return try JSONDecoder().decode([Float].self, from: self.encodedTemplate) } }
Use an instance of the class when comparing face templates:
let templateJSON1: Data // Template 1 let templateJSON2: Data // Template 2 let template1 = MyFaceTemplateClass(encodedTemplate: templateJSON1) let template2 = MyFaceTemplateClass(encodedTemplate: templateJSON2) let score = try FaceTemplateUtility.default.compareFaceTemplate(template1, to: template2)
Comparing face templates
import FaceTemplateUtility
func isFaceTemplate(_ template1: [Float], similarTo template2: [Float]) -> Bool {
let threshold: Float = 4.0
let score: Float = FaceTemplateUtility.default.compareFaceTemplate(template1, to: template2)
return score > threshold
}