Ultrafast switching between OpenCV versions

Posted on Oct 2 2014
[ Copyright Adi Shavit, BSD license ]

OpenCV 3.0.0-alpha came out recently with exciting new functionalities. I was keen to give a try in particular to the new 3D reconstruction functions, so I downloaded, built and installed OpenCV 3.0.0-alpha from source code. Everything was great, until I tried to keep on working on one of my client's projects the day after, which was being developed with OpenCV 2.4.9. In this project, I use a custom makefile that calls pkg-config to know about OpenCV's headers and libraries. The compiler spat a ton of errors because pkg-config was now pointing to the the new OpenCV version and it is not backwards compatible. Hey, they changed the major version number, so I should have expected it... Shame on me. Fortunately, I had a copy of the old OpenCV build and fixed the mess quickly. That same day after work, I had some extra time again to spend on my experiments with version 3. So, I installed back OpenCV 3.0.0-alpha, which took me a minute because I still had the build in my drive. This is not too much time if you do it seldom, but a waste of time if you want to switch between versions frequently. On the other hand, each OpenCV build was taking up some precious gigabytes of disk space. This is why I decided to write CVswitch, a simple bash script to automate the switching task quickly while respecting disk space.

CVswitch allows you to take snapshots of OpenCV installations and switch between them at light speed. It is open-source and I am releasing it under the GPL 3 license on GitHub. It works as follows. Once an OpenCV version is installed, you can take a snapshot of it. This will store all (and only) the necessary headers, libraries and pkg-config files for the current version in a private directory for later recovery. It copies files by creating hard links to them, so it is fast and does not duplicate data. The installation files are associated to the OpenCV version number, so you can later recover any previously saved version, which means that pkg-config will return that version's information and all files for that version will be restored.

Some people might think that the correct way of working on my experiment without affecting my client's project would be installing version 3 in its own directory and making my experiment point there. Well, very often, I use custom makefiles that query pkg-config to know about OpenCV's headers and files (for certain projects, I prefer this method over cmake). Pkg-config only supports one package version at a time and, therefore, I would still have to manually change pkg-config's configuration to switch between OpenCV versions. This is what CVswitch does automatically. Be it orthodox or not, I still find very comfortable throwing OpenCV installations anywhere —even at the same path— and deciding later, with just a single command, which one pkg-config will report about. Another good example of use case for this tool is keeping OpenCV versions that were installed through a package manager that does not let you change the installation directory. Since CVswitch also backs up OpenCV files, a version overwritten by the package manager can be brought back to life without reinstalling it. In fact, CVswitch can even restore accidentally removed OpenCV versions.

Commands

These are the things that CVswitch can do for you:

Show the current OpenCV versioncvswitch current

Save the current OpenCV versioncvswitch save

Show the list of saved versionscvswitchorcvswitch list

Switch to a saved versioncvswitch _saved_version_If the current version was not already saved, it does so beforehand. You may use shortcuts for saved_version, i.e. you can type 2.4 instead of 2.4.9.0 if there is no other 2.4 variant, or just type 2 if there is a single version 2 variant. If CVswitch is confused by what you typed, it will let you know about the alternatives.

Show helpcvswitch helporcvswitch --help

Before using CVswitch

Please check the project's README to know whether CVswitch was tested for your specific system. It is in a very beta stage. If you finally happen to try it, please let me know how it went in the comments below. I would appreciate that you specify the OpenCV versions you use it with. It is a good idea to keep your OpenCV builds until you are sure that CVswitch is saving/restoring them correctly. You can file any bugs through the GitHub project's page, or try to solve them on your own and make a pull request. Suggestions from Linux gurus will be very welcome and rewarded with deep respect.

comments powered by Disqus