Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Classification method.
- func classify(_ image: CGImage, completion: @escaping ([VNClassificationObservation]) -> Void) {
- DispatchQueue.global(qos: .background).async {
- // Initialize the coreML vision model, you can also use VGG16().model, or any other model that takes an image.
- guard let vnCoreModel = try? VNCoreMLModel(for: Inceptionv3().model) else { return }
- // Build the coreML vision request.
- let request = VNCoreMLRequest(model: vnCoreModel) { (request, error) in
- // We get get an array of VNClassificationObservations back
- // This has the fields "confidence", which is the score
- // and "identifier" which is the recognized class
- guard var results = request.results as? [VNClassificationObservation] else { fatalError("Failure") }
- // Filter out low scoring results.
- results = results.filter({ $0.confidence > 0.01 })
- DispatchQueue.main.async {
- completion(results)
- }
- }
- // Initialize the coreML vision request handler.
- let handler = VNImageRequestHandler(cgImage: image)
- // Perform the coreML vision request.
- do {
- try handler.perform([request])
- } catch {
- print("Error: \(error)")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement