Does Facial Recognition Work on Dogs

OpenCV Simplified and Explained

Sats Sehgal
Towards Data Science

--

Image by Author

There is no doubt that facial recognition has certainly become a hot and now mainstream topic within the field of data science, but it also comes with its fair share of controversy given privacy concerns. However, this technology is becoming more prevalent whether it is used to unlock your phone with your face or detect people and/or images for security reasons such as your security cameras. In this article I want to take the concept a step further and that is whether you can actually train technology to recognize the face of a dog.

The applicability is very limited but my motive was more for scientific benefit and understanding than for real world application. The dilemma I was trying to solve is whether I can train a computer algorithm to recognize my dog’s face and if so, turn on a light otherwise turn it off. The image below breaks out the sequence and events along with the devices needed, most importantly my adorable dog. I used Python and the cv2 package along with a cascade XML file that was specific to dogs.

Image by Author

Now before we get into the actual results of the test, let’s take a step back to understand how computer vision works. Take for instance the image below of my dog. When a computer shows an image, it actually doesn’t know whether an image contains a dog, pot, or some other device. It only see’s a bunch of data that it uses to render the image. So then how does a computer recognize a dog? ok..let’s get it into it

Image by Author

First, the image itself is converted into a grayscale image simply because its faster than using a color image. This is largely because the ofdimensionality of pixels (more below) for grayscale vs color images. Grayscale pixels range from 0 (black) to 255 (white) while color images are multidimensional, hence the term RGB. R, G, B have their own pixel scales with the mixture of either to render a specific color. For the purpose of this project, i’m not too concerned about the color of the background or his fur, rather the traits that make up a dog which can be performed in grayscale as well. So now that we have a grayscale image, the image is broken up into smaller squares that a computer starts to analyze and process.

Image by Author

Now each of these squares have something called pixels and within each pixel is a unique combination of data that represents a specific color (in this case shade between black and white). Each pixel’s data gets stored and remembered in context to its neighboring pixels. This is how computers can determine important things such as the edge of a face or nose of a dog. Now to leverage machine learning you would need to feed the algorithm thousands if not hundreds of thousands of images of a dog and other animals so that it can learn the distinction between a dog and cat or even more specifically the difference between a dog’s nose and a cat’s nose. Once you feed it enough images with labeled data, it’s able to reference back from everything it’s learned and provide a probability of what the algorithm thinks the picture contains as depicted below.

Image by Author

In the example above, the algorithm believes there is a 75% probability that the image contains a dog, 20% it may be a cat and 5% that its a fox. It’s safe to say that at 75%, the algorithm’ s probably got it right. Now you can always go and set your minimum threshold before it classifies the image. For instance I can tell the algorithm to reject the image and not give it any classification unless its 85% sure it’s one or the other.

So how did my experience workout? Well at first it was hard to get my dog to stay still and look up and down. I had to bribe him with some cheese to get his attention. I also realized that I did not train his face on enough images so I went into my sea of puppy images and found around 100 recent pictures. Once I loaded that into the training set, the prediction was a lot better. If you want to see the code walkthrough but more importantly want to see my dog turn the light on and off with his face you can check it out here.

https://youtu.be/ofh27VwCoLE

I really had a fun time working on this project and also showing that with a bit of trial and error, you can do some really neat things. In the end I would say this project was a success for my research although I don’t expect it to be ready for mainstream quite yet. There are more sophisticated ways to perform facial recognition such as using packages like YOLO, face-recognition or even the capabilities within pytorch. I would probably benchmark against some of these libraries to determine how well the classifier is able to label the right image. Hope you enjoyed this article.

--

--

Sats is a data and analytics business executive. He enjoys teaching coding and analytics modelling. Follow him on youtube here — https://bit.ly/2IAm54F