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)

Note that the image callback doesn’t actually do anything with the data. It just converts it to a numpy array, and saves it to self.frame. The advantage of this method is that it gives you more granular control over how the ROS node runs by breaking out the functionality of rospy.Spin() into a separate run function that manually calls updates.

Additionally, subscriber callbacks in ROS should almost always be simple functions that set flags or store data — not a complex long running function. Those are better suited for a more controlled environment because callbacks are… well, called numerous times based on the frequency of the messages received. As such, they can’t really afford to take their sweet time doing calculations.

Anyhow, adding smile detection functionality is a matter of loading the correct haar cascades and hooking up cv_bridge to do the conversion between sensor_msgs/Image to the OpenCV numpy arrays. As a word of warning — I haven’t actually tested this code live… However, I’m like 99% sure that it will work given the fact that I’ve recreated this exact setup many, many times already over all my different robotics projects. 😛

– Sophie

Leave a Reply

Your email address will not be published. Required fields are marked *