Skip to content

Instantly share code, notes, and snippets.

@jknthn jknthn/vision.swift
Last active Jul 4, 2017

Embed
What would you like to do?
extension LiveCameraViewController: LiveCameraDelegate {
private let ageModel = try! VNCoreMLModel(for: AgeNet().model)
private let genderModel = try! VNCoreMLModel(for: GenderNet().model)
func createAgeRequest() -> VNCoreMLRequest {
return VNCoreMLRequest(model: ageModel, completionHandler: onClassification)
}
func createGenderRequest() -> VNCoreMLRequest {
return VNCoreMLRequest(model: genderModel, completionHandler: onClassification)
}
func onClassification(request: VNRequest, error: Error?) {
guard
let results = request.results as? [VNClassificationObservation],
let result = results.first
else {
return
}
DispatchQueue.main.async { [weak self] in
if results.count == 2 { // Gender
self?.updateGender(label: result.identifier, confidence: result.confidence)
} else { // Age
self?.updateAge(label: result.identifier, confidence: result.confidence)
}
}
}
func didUpdateBuffer(_ buffer: CMSampleBuffer, on camera: LiveCamera) {
let handler = VNImageRequestHandler(cvPixelBuffer: CMSampleBufferGetImageBuffer(buffer)!, options: [:])
try? handler.perform([createAgeRequest(), createGenderRequest()])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.