Skip to content

Instantly share code, notes, and snippets.

@giln
Created March 4, 2019 14:39
Show Gist options
  • Save giln/0b9ead2d6255f985d6d9dfe053004bdd to your computer and use it in GitHub Desktop.
Save giln/0b9ead2d6255f985d6d9dfe053004bdd to your computer and use it in GitHub Desktop.
VNImageRequestHandler
let visionQueue = DispatchQueue(label: "com.viseo.ARML.visionqueue")
private func startDetection() {
// To avoid force unwrap in VNImageRequestHandler
guard let buffer = currentBuffer else { return }
// Right orientation because the pixel data for image captured by an iOS device is encoded in the camera sensor's native landscape orientation
let requestHandler = VNImageRequestHandler(cvPixelBuffer: buffer, orientation: .right)
// We perform our CoreML Requests asynchronously.
visionQueue.async {
// Run our CoreML Request
try? requestHandler.perform([self.predictionRequest])
guard let observation = self.predictionRequest.results?.first as? VNPixelBufferObservation else {
fatalError("Unexpected result type from VNCoreMLRequest")
}
// The resulting image (mask) is available as observation.pixelBuffer
// Release currentBuffer when finished to allow processing next frame
self.currentBuffer = nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment