Smile detection using OpenCV + Designing a ROS node

An implementation of smile detection using OpenCV, cv_bridge, ROS and Python is available in full at the bottom of this post.

I’ve been developing in ROS for awhile now, and I’ve settled into a pattern for creating nodes that I find works really well for me. It has three components:

  1. Create a single class that encapsulates the entirety of what the node is supposed to do
  2. All subscriber callbacks are restricted to saving the content of the message to a member variable to hold until ready to be processed
  3. Have a run function in the code that is the main ROS loop, and calls the update functions to process data

Functionally, it looks like this:
(I’m using FaceFinder as an example)

[Read more…]

How Far I’ll Go

Moana was a great movie, and I liked how it had a really compelling hero’s journey with a main female character that didn’t fall in love with anyone! Also the music is amazing, so here’s a small tribute to my favorite song in it.

On reflection, the message in the song is a pretty positive one. During my work at Amazon Robotics, we’re always told to “Think Big” and have a “Bias for Action”. In other words, staying in the shallows where results are always known will also cause you to develop code/products that are squarely inside the box. Sometimes it takes a big failure in order to see possible paths for improvement in ways one couldn’t have otherwise known. Well, of course impulsively jumping into the ocean like Moana on her first voyage is a bad idea–but we’re all smarter than that. 😉

Image Steganography in Python

Second Title: Hiding Secret Messages in Cute Pictures of Dogs

What is this pupper hiding???

I developed this activity to as an interesting way to introduce students taking the foundation level software class at Olin to image manipulation and binary math. Since I spent quite a bit of time working on it, I wanted to publish the entire thing as a blog post in case other people on the web are interested in steganography. This exercise was modified from a similar one found at Interactive Python, though this version encodes an image into another image instead of ASCII text.

Because I wrote this as a learning exercise, there’s a neat little repo that holds starter code and solution code. You can clone/fork it here. The starter code is in and has some missing functions for you to fill out, while the full solution is in There are two main functions to this code. The first is a decoding function that can extract secret information from an image file, while the second is a function that can encode secret messages into images.

This code uses the Python Pillow library. You can install it using pip if the package is missing from your computer by typing sudo pip install pillow into your terminal window.

If you don’t wanna learn about how to do steganography and just want a neat script that will hide messages in images for you, you can use the code in It is also embedded at the end of this post.

What is steganography?
In a nutshell, the main goal of steganography is to hide information within data that doesn’t appear to be secret at a glance. For example, this sentence:

Since everyone can read, encoding text in neutral sentences is definitely effective

turns into

Secret inside

if you take the first letter of every word. Steganography is really handy to use, because people won’t even suspect that they’re looking at a secret message–making it less likely that they’ll want to try to crack your code. In the past, you may have tried to accomplish this kind of subterfuge using invisible inks or using special keywords with your friends. However, as fearless coders we have access to fancier ways to sneak data around. *evil laughter here*
[Read more…]

He is the sad frog trapped in a pot.

Okay, this one is more a “People” post directed towards some thoughts that have been rolling around in my head lately rather than a specific person.

Kind of heavy for a public blog post, but hey! These are the kinds of things I think about, for better or for worse. Next post will have more levity, promise. 😀

“Forgive me,” He whispers to me in low tones, “I don’t mean to be happy.”
How can he, when the one he hates is himself? He says that its easier this way, because it grants him an excuse. He looks for brighter avenues because that’s what people say he should do, but consoles his wholly failed half-measures with the reprieve of being hated. To him, his daily defeats are wounds only bearable because he knows he has already failed. He sits in a little hole dug by his hatred, face buried in his arms so the rocks tumbling into his pit hurt less.

The one he hates is himself.
A comforting sureness of who he is, in one pair of eyes at least. He hides behind this ugly excuse, crouching lower so as to let the hatred smother him faster.

“Forgive me.” He says to the world
“I don’t mean to be happy.” He cries into his arms.
But he’s forgotten that the world doesn’t give a shit. He won’t be glossed over. He won’t be forgotten, forgiven, have nothing expected from him just because he is hated. His pit is a pothole, and he will naturally be run over if he insists on sitting in the middle of an avalanche.

I want to shake him from his dark reverie, tell him to take one step–just one step!–out of that hole. But I know that the walls of his prison stretch high, and this yawning cavern only echoes back his ironic fear of the world outside. He insists on staying.

It is not my place to tell him what he needs to do, I know that. It is also not my place to assume that I know what is best for him. But I do so want to see his face, the original–unhidden by tearful creases and shadowed hatred.

Algorithm for solving Knights Tour with Python(revisited)

The original Algorithm For Knights Tour was a pretty popular post, so I decided to revisit it thanks to an interesting email I got. This implementation has a couple changes:

  • You can define a ruleset for locations the knight must be at any given move
  • You can define whether the path needs to be closed or not (Knight returns to starting position)
  • There’s a visualizer now! (You’ll need to have pygame installed for it to work though)
  • No more ugly exiting using sys.exit(), we catch custom exceptions this time around

Here’s what the visualizer looks like:
(pink because pink is the best color obvs)

The tour code is mainly the same as the original post, but with the added custom exception class and booleans. Lines 137-146 deal with obeying to a custom tour ruleset 
[Read more…]

The Holographic Universe

I’m currently reading “The Holographic Universe” by Michael Talbot. I expected quite a bit more science than is actually present in the book,  but it’s an interesting read nonetheless. (Though the book definitely explains science through a super new-age lens.) Only a quarter of the way through so far, but chapter three bought something up that blew my mind.

First, holograms 101:

The reason we can see stuff is because light bounces off of everything. Traditional images taken by cameras record incident light intensities as the photons hit the elements in a CCD or whatever sensor the camera is using. A hologram also records the phase of this light as well, meaning that playback of the recorded image retains all of the information during the recording phase.

Recording a holographic image. [Source: Wikipedia]

To record an image, a coherent light beam (aka laser) is sent through a beam splitter so we have two identical beams of light. One beam is reflected off the object to be recorded while the other beam serves as the reference of the recording laser. The two beams (object & laser) form interference patterns when they intersect, and this is recorded on the photographic plate.

Reconstructing the image. [Source: Wikipedia]

When the image is to be read/reconstructed, the original recording beam (or laser of same wavelength) is shone through the plate. Interference allows the original beam to be recovered, restoring the recording of the object to the identical state it was in when the image was recorded. Because we literally recorded the light bouncing off the image, the holographic result is actually identical.

In this way, a mirror recorded as a hologram will also reflect light. And a holographic recording of a magnifying glass will actually magnify objects behind it. The recording plate can also be cut into as many pieces as desired, while the full original image can still be restored albeit being a little more blurry because of the cutting. SO COOL. TOO COOL.

[Read more…]

Hatred isn’t the opposite of love.

Cowardice is.

Just needed to get that off my chest. (The aftermath of the election sucks.)
It comes from a place of fear, of needing to pull your treasures close for fear that they might get lost in the sea that is our world so broken and mended that it’s more tape than substance. There’s nothing wrong with fear in itself–wasn’t Jesus himself fearful that night before the crucifixion? But cowardice, when we are so paralyzed by fear that we can’t look hardship in the eyes, is what destroys us.

Cowardice is what says that a man is less for the color of his skin, the tiles on his house, or the quality of his soul because we’re fearful of the implications of embracing a difference. We convince ourselves that we are righteous, and in so doing carefully, artfully water those vines of hatred that tear us apart from the inside out.

Cowardice is what persuades us to set a man on fire for fear of our own treasures getting too cold, but love gives us the strength to burn so that another can sleep more comfortably. Theres no need for me to rattle on about love because everyone talks about it, but I want to make just one point: Can’t we just love one another and stop being jerks? That’s literally the bible in a nutshell. Is it really that hard??


Oh, wait, I’m not actually done. 😛
Here’s the bible verse that our pastor talked about last week that got me thinking about what love means in context of all the toxic sludge that’s been slung around recently.

Therefore, as the elect of God, holy and beloved, clothe yourselves with compassion, kindness, humility, gentleness, and patience. Bear with each other and forgive any complaint you may have against one another. Forgive as the Lord forgave you. And over all these virtues put on love, which is the bond of perfect unity.

Colossians 3:12-14

And here’s the song that goes along with it. It’s called “Brother” by The Brilliance. 

This post is a bit melodramatic. Election week 2016 and 2016 itself has been a rough time. But hey, my God is not so small that a bad year is enough to overshadow him… so I suppose that’s a consolation.


Sync iTunes playlists to Spotify

iTunes is dumb and has a super restrictive API. Spotify is awesome, and has an API for all my things, like my PS4 and Amazon Echo. But I like the iTunes UI, and it plays better with my local library than Spotify. As a result, I subscribe to both services, but it’s such a pain managing playlists between the two.

I present to you, the super not janky at all sync script/setup that I wrote up in a 3 hour burst of inspiration last night. The main flow of the idea is this:

  1. CRON job runs AppleScript periodically
  2. Applescript queries iTunes for playlist information and saves each playlist and its songs to a .txt file
  3. Python script compares .txt files to Spotify playlists – information retreived via API calls
  4. Python script updates Spotify playlists with differences found between the two services

AppleScript is a very interesting programming language, where the code is incredibly human readable at the cost of ease of use when coding. Here’s what I wrote for extracting playlist information out of iTunes:

This extracts playlist information and saves each playlist as a .txt file to a specified directory. The Python component does most of the legwork in terms of finding which tracks needed to be added to Spotify. You’ll need to create a new application at the Spotify Developers page to get a client_id and client_secret_key.
[Read more…]

Teaching cars to drive with genetic algorithms

“What the heck Sophie?!” You say, “Why are you writing a debrief for a project you did 3 years ago???”

Hahaha, please bear with me dear reader. If I don’t, I’ll forget about the cool thing I did as a freshman in college still stumblin’ through Python. And how cool and easy genetic algorithms are. 😀

Anyhow, carrying on….

For our software design final project, we wrote a simulation program that would allow you to draw a track, then generate cars that would eventually learn how to drive around said track. It was a pretty fun project, and we got a pretty cool result.

Here’s our website!
Here’s the code!

Demo video:

What’s happening is that the cars are starting with random parameters for their wheels relative to its distance from the wall. As the the cars progress through the generations, these parameters are selected using our parameters, such that the car that make it around the track is the only one that survives.

[Read more…]

Installing and setting up ROS in 5 mins

[*disclaimer: not really 5 mins. Downloading ROS will probably take longer than that lol]

ROS is a really great library for robotics research. It might even be good for not robotic applications, I don’t know, but my general feel is that I like ROS about as much as I like nutella. (read: a lot). The ROS install tutorial is really good at explaining the process, but it’s spread over too many pages and requires too much scrolling. I’ve consolidated the information here in one page for ease of use. Our robotics lab only uses ROS Indigo, so I’m writing install instructions with that in mind. Also, ROS only works well with Ubuntu, so I’m assuming you’re also using the correct OS.

Everything that follows, unless explicitly stated, should be typed in a terminal window. Okay, so the steps:

1. Set up sources.list
We need to add the ROS package sources to our sources list so we can actually find the ROS packages using the apt package manager. Open up a terminal window and paste the following:
sudo sh -c ‘echo “deb $(lsb_release -sc) main” > /etc/apt/sources.list.d/ros-latest.list’

2. Set up your keys
Next we need to set up keys so Ubuntu will actually allow us to download + install the packages.
sudo apt-key adv –keyserver hkp:// –recv-key 0xB01FA116
If you get a gpg: keyserver timed out error, use this instead:
sudo apt-key adv –keyserver hkp:// –recv-key 0xB01FA116

3. Install ROS via apt-get
Make sure the apt package index is up to date from when we added those new sources at step 1.
sudo apt-get update
Then install ROS!
sudo apt-get install ros-indigo-desktop-full
There are a couple of flavors of ROS that you can install, but for most applications, you might as well do the full install to make sure you get all the packages you’ll ever need.
[Read more…]