Home
> Uncategorized > #AI with # and #Swift

#AI Image Recognition with #CoreML and #Swift

1ad4da1c-dbcc-4720-baea-3c9c00a9a443

Being able to recognise a object from an image is a super-easy thing to do, for humans, but for machines, it’s really difficult. But with Apple’s new CoreML framework it’s now possible to do this on-device, even when offline. The trick is to download InceptionV3 from Apple’s machine learning website, and import this file into your . With this pre-trained neural network, it can recognise thousands of everyday objects from a photo.

This code is adapted from the London App Brewery’s excellent course on Swift, from Udemy, and the complete source code is available on Github here ; https://github.com/infiniteloopltd/SeaFood

Here’s the code

import UIKit
import CoreML
import Vision

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!
    
    let imagePicker = UIImagePickerController()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self
        imagePicker.sourceType = .camera
        imagePicker.allowsEditing = false
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
        let userPickedimage = info[UIImagePickerControllerOriginalImage] as? UIImage
        imageView.image = userPickedimage
        guard let ciImage = CIImage(image: userPickedimage!) else
        {
            fatalError("failed to create ciImage")
        }
        
        imagePicker.dismiss(animated: true) {
            self.detect(image: ciImage)
        }
    }
    
    func detect(image : CIImage)
    {
        guard let model = try? VNCoreMLModel(for: Inceptionv3().model) else
        {
            fatalError("Failed to covert ML model")
        }
        
        let  = VNCoreMLRequest(model: model) { (, error) in
            guard let results = .results as? [VNClassificationObservation] else
            {
                fatalError("Failed to cast to VNClassificationObservation")
            }
            
            print(results)
            
           
            self.ShowMessage(title: "I see a...", message: results[0].identifier, controller: self)
        }
        
        let handler = VNImageRequestHandler(ciImage: image)
        do
        {
            try handler.perform([])
        }
        catch
        {
            print("(error)")
        }
        
    }
    
    func ShowMessage(title: String, message : String, controller : UIViewController)
    {
        let cancelText = NSLocalizedString("Cancel", comment: "")
        
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        
        let cancelAction = UIAlertAction(title: cancelText, style: .cancel, handler: nil)
        
        alertController.addAction(cancelAction)
        
        controller.present(alertController, animated: true, completion: nil)
    }

    @IBAction func cameraTapped(_ sender: UIBarButtonItem) {
        self.present(imagePicker, animated: true, completion: nil)
    }
    
}



Source link
thanks you RSS link
( https://.dotnetframework.org//04/15/ai-image-recognition-with-coreml-and-swift/)

LEAVE A REPLY

Please enter your comment!
Please enter your name here