iOS Build Cat Vs Dog Image Classifier Using Vision In 5 minutes

Anupam Chugh
Sep 28, 2019 · 2 min read
Image for post
Image for post
From Google Images

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.

🐶 vs 🐱 Image Classifier iOS Application

Here’s the code from the ViewController that builds a cat vs dog image classifier for you.

Source code

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 CGImage and pass it to the VNImageRequestHandler.
  • The VNImageRequest encapsulates the conversion of CGImage to CVPixelBuffer. It also takes care of resizing the image as per the model input sizes. CVPixelBuffer is eventually passed to the model.
  • Run the VNRecognizeAnimalsRequest
  • VNRecognizeAnimalsRequest contains a built-in Cat vs Dog Classifier model. It returns the identifier string and the confidence as the output in the VNRecognizedObjectObservation
  • The prediction runs inside an asynchronous DispatchQueue

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:

Image for post
Image for post
A Screengrab from my iPhone.

That was pretty quick to do! Here’s the full source code:

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.

The Startup

Medium's largest active publication, followed by +719K people. Follow to join our community.

Anupam Chugh

Written by

Pouring thoughts in technology and code. Writer with over 2M views. An iOS and Android developer by the day. Sometimes funny. linkedin.com/in/anupamchugh

The Startup

Medium's largest active publication, followed by +719K people. Follow to join our community.

Anupam Chugh

Written by

Pouring thoughts in technology and code. Writer with over 2M views. An iOS and Android developer by the day. Sometimes funny. linkedin.com/in/anupamchugh

The Startup

Medium's largest active publication, followed by +719K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store