Solving a Mystery with OpenCV

Claudia
4 min readJul 21, 2021

--

Photo by Nicolas Ladino Silva on Unsplash

The scene: In the summer of 2018, I went to a friend’s wedding in Maine. While the weekend in Maine was lovely and the wedding was fun, there was an even that happened that has haunted my boyfriend and I for the past 3 years.

The problem: While on our way to get some primo lobster rolls from a little crab shack, we walked over a very quaint and weathered wooden bridge. I thought it was a perfect time to snap a pic of us while we were just hanging out in between wedding events.

We took a few selfies, yay! When a person who was walking across the bridge stopped and asked if we would like for them to take a photo of us. I guess we looked like we were having a tough time getting the right pic.

I said “yes of course, thank you!” and handed over my phone. However, I did not flip the camera over from the front camera to the back camera. So, this lovely person while laughing and going “I have no idea what I am doing!”, begins to take several selfies of themself! They eventually figured out how to flip the camera and got some pretty adorable photos of us laughing and being oh-so-cute. We say thank you and move on with our lives.

Years go by. Time has its way with us. Seasons change.

When either of us recall the event, we each had a different recollection of what the person looked like. I thought they were a young woman, about our age, mid 20s. My boyfriend remembers them being much younger and thought they were a teenage girl. We show a group of friends the photo of the friendly stranger and they can’t seem to agree on a solid age either.

Do some data science about it!

The Plan: pass the image through an age and gender classifier.

Model is trained by Gil Levi and Tal Hassner, using the Adience dataset, which is made up of real world pictures uploaded to Flickr.

CNN Model:

  • Convolutional layer 1 : The first convolutional layer has 96 filters/nodes of kernel size 7.
  • Convolutional layer 2 : The second convolutional layer has 256 nodes with kernel size 5.
  • Convolutional layer 3 : The third convolutional layer has 384 nodes with kernel size 3.
  • The two fully connected layers have 512 nodes each.

The prediction is based on the class with max probability for the image.

The biggest age category for both male and female is 25–32.

Adience dataset: 26k images of 2,284 subjects in real world conditions and quality.

This is not a very big dataset to use in terms of training a model.

Everything is either a 0 or a 1 to a computer. Any more than that and their fans really start blowing. For now gender predictions will have to stay binary.

# gender labels
genders = ['Male', 'Female']
# age labels
ages = [‘(0–2)’, ‘(4–6)’, ‘(8–12)’, ‘(15–20)’, ‘(25–32)’,
‘(38–43)’, ‘(48–53)’, ‘(60–100)’]

Results on test images:

Center crop of images at 250x250 and 400x400
I mean… kinda?

I did change the padding around the images. Originally the network is fed a cropped 277x277 image around the face center.

I increased the crop size and generally retrieved similar results.

The gender classifier preformed pretty good but the age classifier was a bit wonky. For my specific problem I really needed a good age classifier.

This is the selfie in question.

Our mystery person is classified as Female between the ages of 8–12.

If we go by Price is Right rules, my boyfriend wins this one. He predicted (guessed) that the person was a young girl.

In conclusion, the CNN was pretty OK at predicting both gender and age. And really, how good are any of us at doing that?

Using a much larger dataset is one good experiment to try to boost the model. However, the idea of putting people into little boxes doesn’t really sit right with me. For academic purposes, its fine.

--

--