Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. // Classification method.
  2. func classify(_ image: CGImage, completion: @escaping ([VNClassificationObservation]) -> Void) {
  3. DispatchQueue.global(qos: .background).async {
  4. // Initialize the coreML vision model, you can also use VGG16().model, or any other model that takes an image.
  5. guard let vnCoreModel = try? VNCoreMLModel(for: Inceptionv3().model) else { return }
  6.  
  7. // Build the coreML vision request.
  8. let request = VNCoreMLRequest(model: vnCoreModel) { (request, error) in
  9. // We get get an array of VNClassificationObservations back
  10. // This has the fields "confidence", which is the score
  11. // and "identifier" which is the recognized class
  12. guard var results = request.results as? [VNClassificationObservation] else { fatalError("Failure") }
  13.  
  14. // Filter out low scoring results.
  15. results = results.filter({ $0.confidence > 0.01 })
  16.  
  17. DispatchQueue.main.async {
  18. completion(results)
  19. }
  20. }
  21.  
  22. // Initialize the coreML vision request handler.
  23. let handler = VNImageRequestHandler(cgImage: image)
  24.  
  25. // Perform the coreML vision request.
  26. do {
  27. try handler.perform([request])
  28. } catch {
  29. print("Error: \(error)")
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement