Last active
August 24, 2022 09:07
-
-
Save hietalajulius/27ba8fcedaf8f070aaf59bf3333a34a2 to your computer and use it in GitHub Desktop.
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
func setupVision() throws { | |
guard let modelURL = Bundle.main.url(forResource: "yolov5n", withExtension: "mlmodelc") else { | |
throw NSError(domain: "ViewController", code: -1, userInfo: [NSLocalizedDescriptionKey: "Model file is missing"]) | |
} | |
do { | |
let visionModel = try VNCoreMLModel(for: MLModel(contentsOf: modelURL)) | |
let objectRecognition = VNCoreMLRequest(model: visionModel, completionHandler: { (request, error) in | |
DispatchQueue.main.async(execute: { | |
if let results = request.results { | |
self.drawResults(results) | |
} | |
}) | |
}) | |
self.requests = [objectRecognition] | |
} catch let error as NSError { | |
print("Model loading went wrong: \(error)") | |
} | |
} | |
func drawResults(_ results: [Any]) { | |
CATransaction.begin() | |
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions) | |
detectionLayer.sublayers = nil // Clear previous detections from detectionLayer | |
inferenceTimeLayer.sublayers = nil | |
for observation in results where observation is VNRecognizedObjectObservation { | |
guard let objectObservation = observation as? VNRecognizedObjectObservation else { | |
continue | |
} | |
let topLabelObservation = objectObservation.labels[0] | |
let boundingBox = CGRect(origin: CGPoint(x:1.0-objectObservation.boundingBox.origin.y-objectObservation.boundingBox.size.height, y:objectObservation.boundingBox.origin.x), size: CGSize(width:objectObservation.boundingBox.size.height,height:objectObservation.boundingBox.size.width)) | |
let objectBounds = VNImageRectForNormalizedRect(boundingBox, Int(bufferSize.width), Int(bufferSize.height)) | |
let shapeLayer = createRectLayer(objectBounds, colors[topLabelObservation.identifier]!) | |
let formattedString = NSMutableAttributedString(string: String(format: "\(topLabelObservation.identifier)\n %.1f%% ", topLabelObservation.confidence*100).capitalized) | |
let textLayer = createDetectionTextLayer(objectBounds, formattedString) | |
shapeLayer.addSublayer(textLayer) | |
detectionLayer.addSublayer(shapeLayer) | |
} | |
let formattedInferenceTimeString = NSMutableAttributedString(string: String(format: "Inference time: %.1f ms ", inferenceTime*1000)) | |
let inferenceTimeTextLayer = createInferenceTimeTextLayer(inferenceTimeBounds, formattedInferenceTimeString) | |
inferenceTimeLayer.addSublayer(inferenceTimeTextLayer) | |
CATransaction.commit() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment