OpenCV with video support in recent Linux distros

There were days when we had to painstakingly go through the manual install process for getting OpenCV running on our systems. Those days are long gone, and for the good. Current day distros bundle both OpenCV and ffmpeg in their repositories, making life extremely easy for enthusiasts and geeks alike. The following 4 steps provide a fully functional OpenCV install on Fedora:

  • sudo yum groupinstall “Development Tools” “Development Libraries”
  • sudo yum install eigen2-devel CTL CTL-devel OpenEXR_CTL-libs tbb yasm yasm-devel tbb-devel OpenEXR CTL-devel¬† gstreamer-plugins-base-devel libsigc++20-devel glibmm24-devel libxml++-devel gstreamermm xine-lib libunicapgtk-devel xine-lib-devel gstreamermm-devel python-devel sip-macros sip vamp-plugin-sdk audacity sip-devel xorg-x11-proto-devel libXau-devel libX11-devel automake libogg-devel libtheora-devel libvorbis-devel libdc1394-devel x264-devel faac-devel xvidcore-devel dirac-devel gsm-devel zlib-devel faad2-devel speex-devel lame-devel orc-compiler orc-devel libvdpau cppunit libvdpau-devel schroedinger-devel dirac x264 lame faad2 amrwb-devel opencore-amr-devel amrnb amrnb-devel
  • sudo yum install ffmpeg ffmpeg-devel opencv opencv-devel opencv-python
  • sudo updatedb

That’s it! Dont believe me, well run a sample python or c code and see for yourself! ūüôā You can download this PDF file containing a sample code written in both python and C. Provide the video file appropriately and enjoy seeing your OpenCV installation playing a video file.

P.S : For C based codes, run gcc as:

gcc -L/usr/include/opencv -lopencv_core -lopencv_highgui -lm -o a.out fileName.c

For python based codes:

python fileName.py

Video in OpenCV

Video files in OpenCV

Recording from Kinect for offline Kinect-less development

Fakenect, allows for hacking and playing with the libfreenect library without the need for having an actual Kinect device. Assuming you have a pre-“recorded” data, all that needs to be done is:

fakenect <path_to_data_folder> <your_application> <parameters>

For e.g., if we need to run freenect-glcview, that comes bundled with libfreeenct library, we need to execute it as:

fakenect <path_to_data_folder> freenect-glcview

and we’ll get access to the pre-recorded data as though an actual Kinect device was connected! Now comes the interesting part.. How to record such a data? We could do it using freenect-record that comes bundled with libfreenect, but it doesnt let the user know what is being captured due to absence of any GUI.

kinect_record is a nice little code meant to record and show what is being recorded simultaneously. However, compiling this little gem needs some tweaking to the Makefile.linux that comes bundled along:

search for the location where freenect.h is located in your system:

  • locate libfreenect.h (for my system, its something like: /usr/include/libfreenect/libfreenect.h)

copy the path till libfreenect/ (for my system, it will be: /usr/include/libfreenect/). Open Makefile.linux and edit the CFLAGS variable to look like:

  • CFLAGS=-g -Wall -I/usr/include/libfreenect -I/usr/include

This should be enough in most of the cases. I hit an additional error message: undefined reference to symbol ‘pthread_key_delete GLIBC_2.2.5’. This was resolved using this suggestion and the corresponding edit to LDFLAGS variable makes it look like:

  • LDFLAGS = -lfreenect -lGL -lGLU -lm -lglut -L /lib64 -l pthread

That should be it! Just type make and the executable should be ready. To execute it, type:

  • ./kinect-record <path_to_folder>

and enjoy hacking using libfreenect without needing the actual Kinect device!

Using Skeltrack with Kinect on Fedora 18

Skeltrack is a skeleton tracking library developed by Igalia’s Joaquim Rocha. Developed as a FOSS library, it is very well integrated with Gnome technologies, an example of which is the capability to control Gnome desktop using Skeltrack.

Here, the steps needed to get Skeltrack up and running on your fresh F18 system is detailed. First, install the dependencies using yum:

sudo yum install libfreenect* autoconf automake gobject-introspection-devel gtk-doc */cairo.h

Now we are ready to begin the actual installation. Since, libfreenect lacks the motor control capabilities, GFreenect was developed at Igalia. It is a dependency for Skeltrack, so we need to install it first using the following steps:

  • sudo updatedb
  • export LD_LIBRARY_PATH=/usr/local/lib/
  • export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
  • git clone https://github.com/elima/GFreenect.git
  • cd GFreenect && libtoolize -v –copy –install
  • aclocal
  • autoconf
  • ./autogen.sh
  • ./configure
  • make
  • sudo make install

With this, GFreenect installation should be done. You can test the installation by plugging in Kinect and executing:

  • cd <PATH_TO_GFreenect_FOLDER>/examples
  • python2.7 test*.py

You should get something like this:

GFreenect Demo

Now to install Skeltrack, similar steps are needed:

  • sudo updatedb
  • export LD_LIBRARY_PATH=/usr/local/lib/
  • export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
  • git clone https://github.com/joaquimrocha/Skeltrack.git
  • cd Skeltrack && libtoolize -v –copy –install
  • aclocal
  • autoconf
  • ./autogen.sh
  • ./configure
  • make
  • sudo make install

Thats it! Skeltrack would be up and running on your system :). To test, run:

  • cd <PATH_TO_Skeltrack_FOLDER>/examples
  • ./test-kinect.o

These steps should be enough to get us ready to use the amazing Skeltrack library.. In case you’re even more eager to play around with Kinect and want to show off your gesture skills, install and use Skeltrack-Desktop-Control :

And we’re done. To run the software:

  • cd <PATH_TO_Skeltrack_FOLDER>/src
  • ./skeltrack-desktop-control

Happy Kinect hacking! ūüôā

Set up samba share on Fedora 18

With the advent of samba4, seems a few stuff have changed (havent really gone through the changes list). Till now used to copy previous Fedora installation’s smb.conf file. However, doing the same didnt really help creating the share.

Reverting back to the original smb.conf file (ALWAYS, ALWAYS back up conf files before making any changes!), added the hostname restriction as had mentioned in one of the previous posts. Once done, added in the “Share Definitions” section shared folder information:

[Share_Name]
comment = Shared Folder
path = <location>
read only = no
; browseable = yes
guest ok = yes

This step can also be achieved using a gui tool : system-config-samba (sudo yum / dnf install system-config-samba).

Next comes SELinux policy settings. Either using the gui policycoreutils-gui (sudo yum / dnf install policycoreutils-gui) or through terminal set the following Booleans:

  • su
  • setsebool -P allow_smbd_anon_write=1

For full read / write access :

  • setsebool -P samba_export_all_rw on

For only read access :

  • setsebool -P samba_export_all_ro on

and Finally relabeling for the shared folder:

  • chcon -t samba_share_t <path_to_shared_folder>/

And we are ALMOST done. Just install samba-client and set samba password for the user:

  • smbpasswd -a <username>

Finally, restart / enable smb.service and nmb.service to ensure samba server starts at boot from next time:

  • systemctl enable smb.service
  • systemctl enable nmb.service
  • systemctl restart smb.service
  • systemctl restart nmb.service

Thats it! Enjoy samba shares on Linux and Windows from a fresh, moowing F18 system..

P.S : (1) also check if firewall allows samba connections, i.e., Firewall > Persistent Configuration > check on samba.

(2) change the permissions of the folder you want to share, i.e., chmod -R a+rwx folder_location/ .

Nice features in TeXLive 2012

Tried TeXLive 2012 in Fedora 18 Beta today. Till now, whenever needed to create / edit tex files, used to install TeXMaker, and it pulled in most of the TeXLive dependencies. Unfortunately, that didnt happen this time around.

dvips, psfrags.sty and cmr10.tfm didnt get installed leading to errors like : Error could not start command, dvips: couldnt find header file : psfrags.pro and dvips: i can’t find cmr10.tfm respectively, when trying to compile a tex file. It was then that i found a really convenient feature introduced in TeXLive 2012.

To install any of the above missing TeXLive packages (or for that matter, any missing TeXLive package), all that we need to do is :

sudo yum install ‘tex(dvips)’

sudo yum install ‘tex(psfrag.sty)’

sudo yum install ‘tex(cmr10.tfm)’

(Please substitute ‘yum install’ with ‘apt-get install’ if you’re using a Debian-derivative)

And we are done! Easy and so convenient.. helps avoid frantically searching for the missing packages.. Kudos to the TeXLive developers. But why the packages were missing in the first place, is something that am trying to figure out.

MATLAB: Search a vector in a matrix

I often encounter situations that require doing a vector search in a matrix. Finding discrete matrix elements is quite simple using the find command, but what about finding a vector?

Am aware of 3 of the possibly many options, namely: ismember, strmatch and strfind. But which of these to use for optimal speed? Turns out, strfind is the fastest of the three. Try the following code and be amazed at the order of magnitude difference in its search capability!

%—–define matrix a and vector b———%
a = [1 2 3;4 5 6;7 8 9;10 11 12;13 14 15]’; b = [7 8 9];
%—–using strfind
tic;
pos = strfind((a(:))’,b);
pos = round(pos/size(a,1))+1
disp(toc);
%—–using strmatch
tic;
pos = strmatch(b,(a)’)
disp(toc);
%—-using ismember
tic;
pos = ismember(a,b);
find(sum(pos) == 3)
disp(toc);

Simulation of the above (even such a small matrix) results in average time as 1.91e-4 sec, 6.3e-4 sec and 0.0019 sec respectively! Moral of the story: avoid ismember!

Revisiting Nvidia Driver and CUDA Toolkit install in Fedoa 17/ CentOS 6

After having gone through a lengthy set of instructions for installing Nvidia binary driver and CUDA Toolkit on CentOS, I woke up one day to kernel updates and to my horror post restart, no login screen! Rebooting into to the older kernel worked fine, offering a much needed solace in the midst of thoughts of all the effort having gone down the drain.

It didnt take me much time to realize the culprit: kernel-source-path argument while executing the binary installer (step #7 in previous post). Did this imply I had to manually reinstall the driver everytime the kernel got updated!

Not wanting to waste time repeating mechanical work, I started googling, and thanks to a tuxgeek post, realized a much simpler, hassle free way of managing the Nvidia driver. The steps are as follows:

The install step above can be done in GUI by double clicking on the downloaded .rpm file(s). Now, all that is needed are the following yum commands in the terminal:

  • sudo yum update
  • (Fedora 17) sudo yum install kmod-nvidia xorg-x11-drv-nvidia-libs
  • (CentOS 6) sudo yum install kmod-nvidia nvidia-x11-drv

and one more step to blacklist the open source driver nouveau from loading:

  • su -c¬†¬†¬†¬†¬†¬†¬† # log in as root
  • echo “blacklist nouveau options nouveau modeset=0” > /etc/modprobe.d/blacklist-nouveau.conf
  • exit¬†¬†¬†¬†¬†¬†¬†¬† # exit from root environment

Thats it!! 4 lines in terminal instead of the first 9 bulleted steps in the previous post! Kudos to the maintainers of these rpms ūüôā .. But the question remains, does this handle kernel updates. Well, post the availability of a newer kernel, boot into the previous kernel till the time an update for kmod-nvidia arrives. Once its updated, your newer kernel will boot without any fuss!

Unfortunately, the CUDA toolkit steps are still to be followed as given in the previous post. But compared to the driver install, they are practically nothing..

NOTE: CentOS users may have to follow the vga=ask step as given in previous post to see plymouth at boot. Fedora’s “charge” plymouth works out of the box!

UPDATE: As pointed out by Jim, the kmod-nvidia package can be substituted with akmod-nvidia to get uninterrupted Nvidia driver usage even on an updated kernel, thus reducing the wait time to boot to the new kernel to a simple restart, rather than a day or two till the kmod-nvidia package is released in RPMFusion / ELRepo.

Restrict samba share access to certain IPs / hosts

Samba provides an easy (well.. almost) way of sharing files and folders between Linux and Windows. In certain situations, it may be required that a certain system allows both write and read permissions to some folders. Now imagine a situation where the systems are in a large subnet: an unrestricted write access gives anyone on the subnet free access to your data (maybe sensitive too). Can something be done so that only certain IPs / hosts in the subnet  get access to your system?

Well, it turns out, Samba developers have given it a good thought.  First of all, edit /etc/hosts and add aliases in the following format:

IP                                alias_name

Say you added 3 aliases for 3 different hosts (alias1, alias2, alias3). This step ensures you dont have to remember the actual IP of the hosts, and those can be referred to by their alias names. Now, edit /etc/samba/smb.conf, and add the following to the [global] section:

  • hostname lookups = yes
  • hosts allow = alias1 alias2 alias3

And its done.. Restart samba server for the changes to take effect:

  • sudo systemctl restart smb.service
  • sudo systemctl nmb.service

Now only the aliased IPs will get access to your samba share, while the rest of the subnet will be blocked out!