Created
March 4, 2019 14:39
-
-
Save giln/0b9ead2d6255f985d6d9dfe053004bdd to your computer and use it in GitHub Desktop.
VNImageRequestHandler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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