Category: Programming

Naming things and "ImportError: No module named msg"

I’m using ROS at school for a project. Part of the project is to detect someone’s hand with a camera, so I’m just looking for a patch of “skin colored*” pixels. ROS organizes software as packages, with nodes in them, and messages that the nodes use to communicate with each other.

For my system, I had a package called “hand_detector” with a source file called “hand_detector.py” and a message type called “hand”. ROS generates the messages, which I then import into my python code with the line:

from hand_detector.msg import *

This gets me the error message: “ImportError: No module named msg”

The reason for this is that python searches the same directory as the executing script for imports before it goes looking anywhere else. Since the file hand_detector.py is the executing script, and is naturally in the directory with itself, python finds it there, imports it into itself, and then tries to find a module called “msg” within hand_detector.py. There’s no .msg in there, so I get the error.

The moral of the story here is don’t name your package and the script in it the same thing. Once I converted the script to just “detector.py”, the problem went away.

*I’m somewhat concerned that I wrote a “white people detector”, as it’s really just thresholding the H part of the HSV color space and counting pixels. Other color spaces may be better for this, but this doesn’t have to be perfect. I just don’t want the robot to be a dick to black people.

That's Not Helping, Python.

[ERROR] [WallTime: 1384556730.822164] bad callback: >
Traceback (most recent call last):
File "/opt/ros/groovy/lib/python2.7/dist-packages/rospy/topics.py", line 681, in _invoke_callback
cb(msg)
File "./board_finder.py", line 81, in callback
self.finder.getBoard(data)
File "./board_finder.py", line 68, in getBoard
newImg = cv2.warpPerspective(cvImg, perMat)
TypeError: Required argument 'dsize' (pos 3) not found

[ERROR] [WallTime: 1384556763.964408] bad callback: >
Traceback (most recent call last):
File "/opt/ros/groovy/lib/python2.7/dist-packages/rospy/topics.py", line 681, in _invoke_callback
cb(msg)
File "./board_finder.py", line 81, in callback
self.finder.getBoard(data)
File "./board_finder.py", line 68, in getBoard
newImg = cv2.warpPerspective(cvImg, perMat, newImg.shape)
TypeError: function takes exactly 2 arguments (3 given)

Well which is it? Exactly two arguments, or the third positional argument is required?

The real problem is that the shape of newImg is a 3-tuple, and warpPerspective expects a 2-tuple. This StackExchange post hipped me to the bug.

A Framework for Flow-based Coding

Normally, I think visual presentations of programming languages, such as Labview, are more a problem than a solution, but there is one case where I don’t think that’s the case: when the program is operating on a stream of data, and the program should be able to be changed without stopping the stream. The canonical use case for this, in my opinion, is realtime operation on a video stream. In this case, you can watch the video output change in realtime as operations are added, removed, and modified.

What I was hoping to do, at some point, is to write a set of operations for video that are wrappers for e.g. GStreamer, and by putting them in a visual programming framework. That would give me a set of VJing operations that can be played with in realtime to do things like chromakeying a live video stream on human skin colors or dropping swirling masks on all the faces detected in the video stream.

Pyqtgraph has a lot of promise as a framework for this. My main desire for the framework is that I don’t have to deal with things like handling mouse clicks, and can just get a description of the pipeline and shove data through it. It may be someone already did this, but firtree.org is dead, so maybe it doesn’t matter if they did.

Microcontroller fun

The Arduino has a huge hobbyist-level codebase and lots of libraries for talking to various devices.

The 8051 is a venerable old processor that still gets used in lots of stuff because it’s cheap, and has a low gate count.

It’s probably possible to port a lot of the Arduino stuff (everything that doesn’t use specific on-chip features) to the 8051, thus allowing people to use the software environment they are comfortable with on a new chip. The same is likely true of PICs, and other chips.

The general case, then, is to create a translation system that automates, as much as possible, the process of porting the Arduino libraries and environment from one chip to another. This is, at a high level, possible because anything a computer with a turing-complete instruction set can do, any other computer with a turing-complete instruction set can also do. The hang-up would be on limitations of real hardware (there’s a lot of cool stuff in there, but no infinite data/instruction tape).

Pebbles and antlers

I have a bunch of ideas for little applications.

One is a game-like interactive entertainment where you contemplate a pebble. You can turn it around, zoom into its surface, etc. That’s it, just examining a pebble. The trick is, this is actually technically somewhat complex. Pebbles have structure ranging from overall shape to near-microscopic scratches and dents. To faithfully render the pebble, all of that has to show up as you look around. To save processor time and storage space, I plan to generate all of that on the fly. I’ve been advised that I should either use the Unity game engine, or write my own engine to do this.

Pebble is a proof of concept, really. The original idea was to write a game where you examine a tree. You can fly all around it, zoom into the cracks in the bark until they tower over you like canyons, zoom out until the tree is a green spot below you. There is no way to sit down and hand-make all of that data, so it pretty much has to be generated. Pebble seems like something I can do to determine that the concept is possible.

(I’m not sure if my interest in generative content is an indication that I’m into elegant, intelligent solutions, or just that I’m profoundly lazy.)

Another application is a piece of software that generates antlers. You run it, and it spits out an STL file that you can use to get the antlers 3D printed. You can modify things like how the antlers branch and curve, to get things like ibex horns or deer antlers.

(Oddly, thinking about these programs, I realize that I don’t know what trees, pebbles, or antlers look like. I mean, I can identify them, and even sort of describe them, but not with anything like the sort of description required to produce them. This is, I think, the difference between looking at things like an artist and looking at them like someone who just doesn’t want to bump into stuff.)

Closure(s)

About 10 years ago, 10.58 if you want to get really precise, I took the class CS2135 Programming Language Concepts. I remember being really impressed with a lot of the power and expressiveness of functional programming.

This evening, I was working on a little goofy project, and it turned out that doing it with a closure was the clean and useful way to go. I find it amusing that when I finally got around to using these techniques, it was just kind of a natural thing.

The (half done) code is all over here.

Drinking with Robots

I am building a drink-dispensing robot. It has 5 pumps internally, so I want to find the set of five liquids that will produce the largest variety of mixed drinks. To do this, I’m going to need a huge set of drink recipes. I got a bunch from a cocktail database that esquire maintains, but the biggest list I’m aware of is The Webtender. Unfortunately, that database isn’t in a form that permits me to make queries to find out what is the maximal set of drinks that I can make, given the constraint of 5 liquid ingredients. So instead of the web interface, I want the raw data.

This means I want to download every single drink recipe from The Webtender. The URLs there are of the form http://www.webtender.com/db/drink/6217, so the obvious thing to do would be to write up a little bash oneliner that just wgets each of the files in turn. It would probably look something like this:

for (( i=1; i <= 6217; i++ )); do wget http://www.webtender.com/db/drink/$i; done 

But it may be that The Webtender issues a 403 Forbidden error if you use wget, probably to prevent just this sort of hijinks. Unfortunately for them, wget can be configured to claim to be something else. These instructions provide the config file to cause wget to claim to be Mozilla on Windows NT, which The Webtender should have no problem with.

For my next trick, I'll use BeautifulSoup to turn the HTML files from the webtender and Esquire into a SQL database, and probably perform some form of normalization on the data, such as making all the measurements be in the same units.

Coloring cells based on cell value in OpenOffice

OpenOffice (probably LibreOffice too) has limited functionality for setting the background color of a cell. I wrote a little macro that colors each cell blue, with saturation depending on the range of the value. This is in OOBasic, but it demonstrates the general idea, and the proper way of setting cell background colors.

Sub Main

End Sub

sub ColorCells

dim ii
dim jj
dim r
dim g
dim b
dim doc as object
dim sheet as object
dim cell as object

doc = ThisComponent
sheet = doc.Sheets.getByName("Sheet1")

for ii = 0 to 5
	for jj = 0 to 626
		cell = sheet.getCellByPosition(ii, jj)
		r = 255 - (cell.Value * 255)65536
		g = 255 - (cell.Value * 255)65536
		b = 255
		cell.CellBackColor = RGB(r,g,b)
	next jj
next ii

end sub

CSS: Turing complete?

“The simple selector matches if all of its components match. ” according to the W3.org page on selectors. That sounds to me like an AND gate, in that something is selected if the logical AND of its components match.

Combine that with logical inversion, provided by the not selector, and it seems to me that you get NAND, which is a universal gate. Any boolean function can be composed of NAND gates, so it is in theory possible to compose an entire CPU out of NAND gates.

This makes it seem to me like you could write a processor simulation in CSS.

The main thing that I wonder about is how browsers evaluate CSS, because if it is not re-evaluated until nothing happens (e.g. nothing gets its attributes changed) then the CPU wouldn’t work because the “output” of a selection would not be able to be used as input into more selections (e.g. by changing their classes). Heck, I’m not sure you can even have CSS change the classes of an element on the fly.

But if you can…

Eclipse Unresolved Imports with ROS on Ubuntu

I use Eclipse as an IDE. I do this to get code completion and syntax highlighting, as it does those pretty well for C/C++. Recently, I was trying to write a ROS plugin, and Eclipse couldn’t find the ros.h header file, which I had included with the line #include "ros/ros.h". Because of this, it marked every line where there was any use of anything from that header as an error, and gave a warning on the include line. They were not errors, and the code still built fine, but the IDE displayed it wrong. To fix this, I tried the normal solution, which is:

Click Project -> Properties -> C/C++ General -> Paths and Symbols -> Includes -> GNU C++ and then use the Add button to add the path to the include file. After that, reindex the project by right-clicking on the project, clicking Index -> Rebuild, and the include should be resolved.

Unfortunately, it wasn’t. Restarting Eclipse didn’t pick it up.

These instructions suggested that it might be something wrong with my language settings, but that doesn’t work either.

This is the proper way of doing it, and it actually works.

Now my tools are out of the way and I can start work. At 5:00PM on Friday.