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:
- Create a single class that encapsulates the entirety of what the node is supposed to do
- All subscriber callbacks are restricted to saving the content of the message to a member variable to hold until ready to be processed
- 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
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. 😛