Sunday, 27 January 2013

Help, frustration, Python and OpenCV on OS-X

One of the most frustrating moments is when I'm almost sure I cannot do something. And in general I know I can solve anything, really. Just not now. I have to wait til I learn a certain material. Or I need to wait for help. Or I just need to realize what's the problem. Yeah, I often ask the wrong question.

I realized that moment when I had problems with web servers. I never learned proper Unix or networks. And with the internet it's not too hard to solve anything when you know what you want to solve. Please give me this Apache mod name. Please tell me how to install a mail server. Please show me how to install an SVN server. But then you have questions like - I've seen that cool server architecture my colleague is using, and he's pretty happy with that, who can I install it? Haha, slap, *ts*.

This story is about video input. I have an idea that I would like to make and I need to use the video input. Now the easiest would be to use Flash. But that's proprietary (about that another time). There is the free Adobe Flex SDK - but I'm pretty angry at them to charge for Flash Builder. Anyways. I can use Objective C and Mac libraries - meh, me no gusta. I need something more dynamic. Google didn't give me any HTML5 solution either. Then I checked Python and it said he has a library: ctypes opencv. Brilliant, I know 2 commands in Python so I will figure out the rest somehow. And then shit hit the fan.

I managed to install the library (*clap*^2), run the first demo and crash:

Traceback (most recent call last):
  File "", line 13, in <module>
    from ctypes_opencv import *
  File "/Library/Python/2.7/site-packages/ctypes_opencv/", line 18, in <module>
    from ctypes_opencv.cxcore import *
  File "/Library/Python/2.7/site-packages/ctypes_opencv/", line 80, in <module>
    _cxDLL, _cvDLL, _hgDLL = detect_opencv()    
  File "/Library/Python/2.7/site-packages/ctypes_opencv/", line 58, in detect_opencv
    cxDLL = cdll.LoadLibrary(find_lib('cxcore'))
  File "/Library/Python/2.7/site-packages/ctypes_opencv/", line 54, in find_lib
    raise ImportError("OpenCV's shared library '%s' is not found. Make sure you have its path included in your PATH variable." % name)
ImportError: OpenCV's shared library 'cxcore' is not found. Make sure you have its path included in your PATH variable.

Splendid, thank you, really. Got a hat and a whip and opened google. Everybody was suggesting to install macports. I'm not saying it's bad, I just don't feel comfortable using it. I tried once and it collided with other binaries - and I'm using hombrew anyway (which do not install opencv).

Then I've found that I need to add the local variables to the shell profile - which I did in all the possible permutation and variation. No luck. As many times (often on *.so hunt) I was looking for silly professors' websites who uploaded their libraries (dylib or so). There was some - which partially solved my problem - generating new ones. (Btw I'm sure a proper infosec admin would have died by seeing my negligence.)

Then you go to the source and try to act as ones in the "hacker" movies. Seemed it requires some files in the library source - which was interesting. Some existed, some didn't. I pulled up a Lubuntu and did the same install. Same issues.

That was several days of desperate self torture when I joined to the #python IRC room. A guy their mentioned he had to change distribution in order to make cv work. Marvelous. Then another guy (@Brend) was extremely helpful and took a look at the code and mentioned it's really not likely I can make it work.

That's the point when you close your eyes and kick a last one. And ... I've met OpenCVWiki. You know the moment after dozen of circles in the dark forest you spot a new tree. I downloaded their package. It uses cmake, so I checked out what is that. Compile, copy the libs to /usr/lib. Nothing works. import cv says no module found. On my way out I tried one of their demo - and Jesus Christ it was running with my camera led turned on. Tada.wav.

What actually happened is that cv seems to be old-ish. And the latter I installed is using cv2. Which works just fine.


What is the lesson? I don't know, honestly. I aged 10 years thanks to my lack of knowledge. How could you get the answer the quickest? What would be your path to success in that case?


1 comment:

  1. The approach which seems to be the most useful/successful for solving problems for me is trying to avoid solve the problem in any specific way, but try to solve the problem itself. If you're having problem with the tool you're trying to solve the original problem, then make sure you're using the best tool for solving the original problem. If it's not the best tool, try to ditch it as soon as possible. Only waste time solving problems of the tool if it's the best tool for the original problem.


Note: only a member of this blog may post a comment.