iOS Build Cat Vs Dog Image Classifier Using Vision In 5 minutes
I had created a Cat vs Dog Image Classifier by converting a small Keras Model into a Core ML model. The link to the source code is available here.
With iOS 13, Vision is even more powerful.
VNImageRequest now has
VNRecognizeAnimalRequest to identify cats and dogs in images. There's no need for creating our own Core ML model as the built-in pet classifier model in Vision is pretty accurate.
Currently, the Vision Animal Detector can only detect cats and dogs.
You can now build a cat vs dog image classifier application in less than 5 minutes
In the following section, we’ll be jumping straight into the code and build an exciting Cat vs Dog Image Classifier iOS Application. Let’s get started with a new XCode project.
Here’s the code from the ViewController that builds a cat vs dog image classifier for you.
So we did a few things in the above code:
- Set up an ImagePickerController to get images from the photos library.
- Convert the image into a
CGImageand pass it to the
VNImageRequestencapsulates the conversion of CGImage to
CVPixelBuffer. It also takes care of resizing the image as per the model input sizes.
CVPixelBufferis eventually passed to the model.
- Run the
VNRecognizeAnimalsRequestcontains a built-in Cat vs Dog Classifier model. It returns the identifier string and the confidence as the output in the
- The prediction runs inside an asynchronous
In the Vision Image Request Handler, you can customise the image by setting the image options to scale/crop..
That’s it! We created a
VNRecognizeAnimalsRequest which classifies the image passed to
VNImageRequestHandler as either a cat or a dog.
We ran the above iOS application over a few randomly selected images from Google and here’s the output we got:
That was pretty quick to do! Here’s the full source code:
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…
Refer my article iOS 13 Checklist For Developers for the other important features that came out with iOS 13.
Originally published at https://www.iowncode.com on September 28, 2019.