Bad OpenCV Weekend

Late last week, I took the plunge and upgraded my work desktop–which, to the Irony Gods’ great delight, is also a production server–from Ubuntu 11.10 to the Precise 12.04 known as Pangolin.

Cue every IT professional ever:

With two tiny exceptions, though, it’s been a remarkably smooth transition. The first (and most trivial) of the two exceptions was that MySQL was, without explanation or warning, uninstalled entirely from my system, leaving me to reinstall it manually. Once that happened, all my databases reappeared as if it was all some big Hide and Seek game.

The second is proving less of a tiny exception and more of a “WHY WON’T THIS WORK”: getting OpenCV to behave once more.

Background

I’ve been using OpenCV’s Python bindings in order to do some small on-the-fly video manipulation with the data we’ve been gathering for my research. We still largely use Matlab for operations like optical flow (since we’re not terribly confident in what, exactly, is happening in OpenCV for those calculations), but for things like extracting a single frame from the video, converting it to a PNG image, and throwing it up on a website, it works extremely well.

I’d been using the old version of OpenCV, the one that comes in the Ubuntu package manager; I believe it’s 2.1, when the newest version is 2.4 and features some radical changes to the API. It was easiest to use (nothing beats a simple apt-get install) and worked just fine.

Problem! When I upgraded from 11.10 to 12.04, it suddenly stopped working. The frames we extracted using the same exact code suddenly looked either like this or were completely black.

Present Day

Over the past weekend (in addition to the Pittsburgh Marathon, which I’ll write about later), I posted this question on StackOverflow to try and diagnose the problem. It’s since spun almost out of control into a step-by-step-by-step ordeal where each subsequent action solved the previous problem while simultaneously introducing a new one.

  1. I started here, uninstalling all the packages from Ubuntu for the critical components (OpenCV, ffmpeg, libvpx, x264), and then downloading the source code for each one and following the compilation and installation instructions listed.
  2. On the last step–recompiling OpenCV–it kept crashing with a very cryptic error message: relocation R_X86_64_32S against `av_destruct_packet' can not be used when making a shared object; recompile with -fPIC. Wish I knew what that meant!
  3. My Googling around led me here–someone with the same problem! I tried adding the apparently-magical --enable-shared flag to the ffmpeg configuration, and…nothing. Actually, worse than nothing: now ffmpeg started throwing the same cryptic error!
  4. AGJWOIEJQJ*WJOWEIDQAWAKJDFSRAAAAAAGE QUIT
  5. I went back a step, and rebuilt and reinstalled libvpx with the same magical flags (--enable-pic and --enable-shared), then tried it again on ffmpeg…and holy crap it worked!
  6. Tried recompiling OpenCV once more…AND IT WORKED! Successfully built and installed with no problems. Would this finally perform the simple action of pulling one frame from a video and saving it as a PNG?
  7. NO! It couldn’t even find the ffmpeg executable! Something about missing shared libraries! OH GOD MAKE IT STOP.
  8. More Googling around turned up this gem, which ended up being exactly what I needed. NOW let’s test OpenCV and see if it works?
  9. …no. Of course not.

You can follow this progression on the SO question itself, but needless to say I am once again stuck. There’s not a whole lot I can do until this is fixed, and the problem is likely some menial little codec somewhere that got removed in the Ubuntu upgrade but which OpenCV won’t admit that it needs.

I suppose I still have plenty of other things to keep me busy.

EDIT: Yep, I finally figured it out. Yes, all the steps beforehand were required to even attempt the final solution, but [as usual] I blocked myself from reaching the ultimate destination by leaving out one. crucial. line of code. from the script that went something like this:

if video.grab():

Apparently the grab() method is more than just a check (mentally I made it analogous to Java’s Stack’s isEmpty(), for example) and actually bootstraps the whole process. Without it my program couldn’t do anything. Mystery solved.

Advertisements

About Shannon Quinn

Oh hai!
This entry was posted in Academics, Programming, Technology and tagged , , , , , , , , , . Bookmark the permalink.

4 Responses to Bad OpenCV Weekend

  1. infantilo says:

    may i ask, how you’ve managed openCV python wrappers to work?
    for me opencv2.4.2 in ubunto 12.04 the createEigenFaceRecognizer createFisherFaceRecognizer and some other functions are not available anymore. Tried to build it from scratch a few times, but always the same reults. it seems as the python wrapper got broken. now i’m searching for any clue.

    • magsol says:

      This may actually be the result of deprecation on the part of OpenCV. Similarly, some of their optical flow methods disappeared between OpenCV 2.1 and OpenCV 2.4 (the latter is considered “cv2”, whereas the former is just “cv”). It may not be that the wrappers are broken; the functionality might have been removed entirely. I checked with the community on the optical flow stuff, and this was indeed the case. My recommendation here would be the same: ask the OpenCV folks.

  2. Luke says:

    I feel your pain. I spent the entire weekend trying to setup OpenCV with Python on windows and then with C++ on windows. It worked on Python a little bit but so far my experience has not been the best with both because the documentation is extremely outdated.

    Even more, when I attempted to install OpenCV on windows with C++, it didn’t work because I had the “opencv2/core/core_c.h: No such file or directory” error. At this point, I’m going to wait until OpenCV is more developed before I use it, or use linux.

    I have checked many other blogs and from what I can tell, if you use OpenCV on linux or on a mac, it has a better chance of things working out
    http://stackoverflow.com/questions/8354342/opencv-and-computer-vision-where-do-we-stand-now

    • magsol says:

      In a way, I feel like the code itself is ahead of the documentation. Overall, 2.4.x is a huge improvement over 2.1, but the documentation for it is almost nonexistent. So when one [invariably] runs into problems, there’s not really anywhere to look for a solution; the only option is to bang away at it yourself and pray it eventually works.

      My primary workstation is, thankfully, Ubuntu. Still, as is clear from this entry, it certainly isn’t without issues. But I certainly sympathize with your plight, and wish you the very best of luck in getting it running on Windows. You are a braver individual than I!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s