Skip to content


Face detection using torch3vision library in Ubuntu

Torch3vision is a nice machine vision library. It implements some features and algorithms that are not present in other libraries (especially the ones based on illumination). In this post I will show you how to install it and how to detect faces with it. A nice thing about this library is that is it released under the BSD license, which makes it usable for commercial projects.

OK, let’s install this library. These instructions should work on any version of Ubuntu, but I tested them under Ubuntu 10.04.

Execute the following commands in the Terminal:

sudo apt-get install build-essential
cd
mkdir faceDetectors
cd faceDetectors
wget https://torch3vision.idiap.ch/sources/vision2.1/Torch3vision2.1.tgz
tar -xvzf Torch3vision2.1.tgz
rm Torch3vision2.1.tgz
cd Torch3vision2.1
cp Linux_i686.cfg.vision2.1 Linux_i686.cfg
cp Makefile_options_Linux.vision2.1 Makefile_options_Linux
cp vision2.1/Makefile_vision_Linux.example.basic vision2.1/Makefile_vision_Linux
make clean; make depend; make

That’s it, we can use the library now. Let’s build and run some examples:

Note: Ignore the “Try to compile Torch” messages when compiling the examples; everything is fine.

First, let’s apply a retinex algorithm. The idea here is to remove extreme shadows and highlights so that the objects in the scene appear under a more uniform illumination than the original.

cd vision2.1/examples/illumination
make
mv Linux_DBG_FLOAT/* .
./multiscaleRetinex ../data/1002.pgm ../data/result.pgm

The previous example runs multiscale retinex on the first image (../data/1002.pgm) and stores the result in the second (../data/result.pgm). You can see the results of this retinex algorithm in the following image:

Now, let’s detect some faces:

cd ../facedetect
make
mv Linux_DBG_FLOAT/* .
./mlpcascadescan ../data/9001_f_wm_s01_9001_en_1.ppm -minWsize 79 -draw -savepos

In this example, a cascade of multilayer perceptrons (MLP)  is used. The library also provides a face detector based on a cascade of Haar-like wavelets (similar to the ones in the OpenCV face detector). An image is created in the same directory if at least one face is detected. You can see an example of this procedure in the following image:

As you can see, the face was correctly detected. This library also provides many other examples for face detection and other features. Take a look at the other examples provided, it is definitely a nice library to have around.

Posted in Computer Vision, Open Source, Programming.


How to install Microsoft Windows 7 and Ubuntu 10.04 using a pendrive

Microsoft Windows 7 and Ubuntu 10.04 are both excellent operating systems. Each one of them has their own pros and cons. That’s why I like to install both of them in my laptop on a dual boot mode. That way I can choose which one I want to use every time I restart my computer.

The current trend in laptops, and netbooks, is that they do not include a disk drive (CD or DVD Drive). Although this is great for having a lighter laptop, it means that the installation CD for any OS is not an option. Well, you could use an external CD Drive, but what’s the fun of it anyway. I prefer using a pendrive. Most of you already have one laying around anyway. The good thing about using a pendrive is that there are no moving parts, and it produces a faster installation. Also, if you don’t use CDs, you will have less noise and no more coasters.

Requirements:

  • Pendrive with at least 3GB of memory.
  • ISO of Microsoft Windows 7. You can buy the ISO for Windows 7 from the Microsoft Store.
  • ISO of Ubuntu 10.04. You can download for free the ISO for Ubuntu 10.04 from their site.
  • CD/DVD Download Tool. This is for transferring Windows 7 to your pendrive. Download it from here.
  • Universal USB Installer. This is for transferring Ubuntu 10.04 to your pendrive. Download it from pendrivelinux.com.

OK, now we are ready. First, you need to download the ISO for Windows 7 and the CD/DVD Download tool. Then, put your pendrive on your laptop (make sure you back up the data on your laptop and pendrive since everything will be lost), open the CD/DVD Download Tool from Microsoft and use it to transfer the ISO of Windows 7 to your pendrive.

This will take some time. Once it is finished, you need to restart your computer and boot from the USB drive. You may need to change the boot order from the system BIOS. From now on, you can just follow the installation process the same as with a normal disk.

Since we want a dual boot, under the Windows 7 installation, delete all existing partitions on the disk and create a new one with about half of the disk space, or as much space as desired. This will be the allocated space for Windows 7. Leave the rest of the drive as unallocated or free space. We will use that for Ubuntu 10.04 later.

Once you finish installing Windows 7, download the Universal USB Installer and the Ubuntu 10.04 ISO. Run the Universal USB Installer to copy the Ubuntu 10.04 ISO to the pendrive. Make sure the box for formatting the pendrive is checked. Click on Create and wait.

Once the Ubuntu 10.04 ISO is copied into the pendrive, restart the laptop and boot from the USB. The Ubuntu installer will show up. When asked about where to install Ubuntu, choose the option “Use Largest Continuous Free Space“. This will install Ubuntu on the free space we left before. Proceed as a regular Ubuntu installation.

Once it is finished installing Ubuntu, you will be able to choose which OS you want to use every time you restart your laptop.

Posted in Programming.


Reducing the size of your pdf file

Sometimes you may find problems when trying to upload a pdf file to a web site. It may be larger than the maximum allowed size. This is very annoying when you already reduced the contents of the file to a minimum or when you just can’t change its contents.

Fortunately, you can reduce the size of the pdf by just running a single line with the help of the program Ghostscript. First, let’s install it. Run the following code in the command line:

sudo apt-get install gs

Now, let’s reduce the file size of our pdf. For simplicity I used input.pdf and output.pdf for the filenames. You can change them to anything you want. In this case, obviously, input.pdf is the file that you have and output.pdf is the name of the generated pdf with a smaller size. If you just copy and paste the code remember to rename your pdf to input.pdf. Now run the following command.

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Now look at output.pdf, or whichever name you used. It should be much smaller than the original. In my test case, I used an original pdf file with a file size of 3,023,501 bytes (around 3MB) and the output file had a file size of just 466,689 bytes (around 500 kB). That is six times smaller!. The compression is obtained by slightly reducing the quality of the images in the pdf and other optimizations.

You can also use this technique for saving storage space. Just run this command on your collection of pdf files to reduce their file size. That way you will preserve the contents of the pdf but you will be using much smaller file sizes. Also, when sending pdfs for revisions, you can save time by sending smaller files!.

Posted in Open Source, Programming.


Empathy script to show contact window

Empathy is an instant messaging application that is shipped with Ubuntu 10.04 and 9.10. It works very good, although there are some small details that I do not like about it like the fact that the contact window is not shown by default.

For showing the contact window when empathy starts, you need to create a custom script. I will show you how to do that now.

Open the terminal (Applications->Accesories->Terminal) and write the following:

gedit empathy.sh

Now gedit will open with a blank screen. Just copy and paste the following text:

#!/bin/bash
gconftool-2 -t boolean -s /apps/empathy/ui/main_window_hidden false
empathy

Save the file and exit. You can do this by selecting File->Save and then File->Exit.
Now you should be again in the terminal. You are going to give the script execution access. You can do that from the command line by executing the following command:

chmod +x empathy.sh

Done. So now every time you want to start empathy just run (double click) the empathy.sh file which is your home folder.

If you want empathy to automatically start every time you login, you can add this script to the start-up by adding it in System-> Preferences-> Startup Applications.

Posted in Programming.


How to use your cellphone as a remote control for your music

In this post I will show you how to control your music from your cellphone. A nice thing about using your cellphone as a remote control is that you can change the music from another room, because of Bluetooth.

First, make sure that you can play mp3 and other restricted files by executing the following code.

sudo apt-get install ubuntu-restricted-extras

Now, let’s install a proper music player: audacious. It is a Winamp clone and it is one of the best music players available in Ubuntu. You can install it by executing the following code in the command line:

sudo apt-get install audacious

Now, you need to get the server software. I will be using a great program called anyremote. Install it by executing the following code:

sudo apt-get install anyremote

Now, let’s configure anyremote so that it can talk with audacious. Under the /usr/share/anyremote/cfg-data/Server-mode/ folder, there are many configuration files, one for each supported program. In this case, I will be using audacious.cfg since that is the program I want to control. For enabling the server to work with audacious, run the following code:

anyremote -f /usr/share/anyremote/cfg-data/Server-mode/audacious.cfg

Now the server is ready for you to send commands, although you have to start it manually every time. You can press Control + C for stopping the server. For automatically loading this server every time you log in, just go to the System menu, Preferences and finally Startup Applications. In there, create a new entry and put the previous command in the Command section. Name and Comment can be anything you want.

After saving this new entry, restart the computer. The server is now up and running. Now I will show you how to install the client in your cellphone.

First, you need to make sure that you have Bluetooth enabled in your computer. Some computers already come with Bluetooth integrated. If you do not have Bluetooth in your PC, do not worry. You can easily add Bluetooth capability to any computer by just buying a usb Bluetooth dongle. After connecting your dongle you will see a Bluetooth icon in your task bar.

Now you need to download the anyremote Java ME client for your cellphone. Go to https://anyremote.sourceforge.net/dload.html and under the Java client for WAP download section download any of the jar files depending on the screen size of your cellphone. For this post, I will be using the with 64×64 icon sets and 64×64 title icon one. You can download it to your Desktop by executing this code:

cd ~/Desktop
wget https://anyremote.sourceforge.net/anyRemote-48b.jar

Now, you need to transfer the file to your phone. You can do that by clicking on the Bluetooth icon and selecting Set up new device and follow the instructions. Once your phone is set up, you click again in the Bluetooth icon, and under Devices your phone should be listed. Go there and click on Send Files and select the jar file you just downloaded. Once the program is in your phone, just run the application, search for servers and connect to your computer. Make sure that your PC is visible so that your phone can find it. You can set your computer visible by going into preferences when you click on the Bluetooth icon.

When you run the application in your phone, accept any confirmation dialog that appears and finally you will have a screen with several buttons such as play, next, previous, etc. Click on them and you will be in control of your music!.

Posted in IoT, Open Source, Programming.


OpenCV input output example

OpenCV has a nice simple cross platform GUI. You can display images, read from keyboard and mouse, input some number using sliders, write on screen, etc. In this post I will show you an example code that I developed that achieves all these things. Because I used cmake, this code can be compiled and run in either Windows or Ubuntu. Feel free to use this code for your next projects. I commented it thoroughly to make it easy to understand.
Remember that you need to have OpenCV and cmake already installed in your machine. If you are using Ubuntu, you can read the installation instructions from here.
OK, the first step is to download and extract the example that I made. You can download it from here.
Go to the command line, navigate to where the code is and execute the following code:

cmake .
make

This will generate the executable. Now run it:

./ioSample

I created a small area on the lower part of the display where I write the actual values of three different inputs that you can use in OpenCV. If you move the mouse around the image, its position will be updated. Also, if you change the slider, the new value will appear. You can also press any key and it will report which was the last key that has been pressed.

I also implemented an inverse function for showing pixel level access to the input image. This can be achieved by just pressing the ‘i’ key.

Finally, if you click on any part of the image, the background color will change to the color of the clicked pixel.

Posted in Open Source, OpenCV, Programming.


12 Great Ubuntu Applications

Ubuntu comes packed with some great applications but still there are some other applications that are extremelly useful. These are my personal favorite Ubuntu applications that do not come pre installed, at least with Ubuntu 8.10, with no particular order

1. Checkgmail

Do you miss the email notifications from gtalk in Windows?, Checkgmail is even better!. This program adds a small icon to your taskbar and constantly checks for incoming email. When email arrives, you see a popup with the emails and some options. It is fast since you do not need to open a web browser to read the emails. Also, it has a very convenient option that allows you to mark all your emails as read in just one click.

You can install it by running the following code in the command line:

sudo apt-get install checkgmail

2. Filezilla

Filezilla is a complete FTP solution. It has a very simple to use interface, it works really good and it is free. If you need to transfer files into your server, webhost, etc, this software can help you. It has many other features as well, such as SFTP.

You can install Filezilla by just running this code:

sudo apt-get install filezilla

3. Netbeans

If you are a programmer, you may need an IDE. Maybe you program in many different languages, so you may need a lot of different IDEs. With Netbeans, you can program for Java, Java ME, C/C++, Javascript, PHP, Python, Ruby, among others. Just install the basic Netbeans installation and then add the plug ins for your language of choice.

sudo apt-get install netbeans

4. Inkscape

This software allows you to create and edit vectorial images. This type of images have the main advantage that they can be resized to any size without loosing their details. It is mostly used for creating diagrams, line art, cartoons, etc.

You can get it by running:

sudo apt-get install inkscape

5. Scribus

This software allows you to make professional looking documents such as brochures, magazines, etc.

You can get it by running:

sudo apt-get install scribus

6. Avidemux

This software allows you to edit video files. You can append video files, cut some sections, process frame by frame, etc. It is a very complete video processing solution and it is also easy to use.

Install it by running the following command:

sudo apt-get install avidemux

7. VLC

For video playback, VLC is one of the best. It has a great support for many different codecs. You only need to install this and enjoy watching any type of videos with it.

Install it by running this:

sudo apt-get install vlc

8. CD Audio Extractor

This software allows you to extract the music from a CD to your computer. It has a minimalistic interface for just doing that.

sudo apt-get install sound-juicer

9. RAR

This program allows you to compress and decompress rar files using the built in Ubuntu graphical interface.

Install it with:

sudo apt-get install rar unrar

10. k3b

This is a CD/DVD burning application. It allows you to do everything you need for creating discs. Also, it allows you to burn gapless music cds (no 2 seconds pause between songs).

sudo apt-get install k3b

11. LyX

This is a document processor based on LaTeX. It allows you to write beautiful documents easier than writing plain LaTeX code.
Install it by running this:
sudo apt-get install lyx

12. Ubuntu Pocket Guide

Well, this is not an application but a free ebook that teaches the Ubuntu essentials. It is very useful for novices and it can be used as reference for more experienced users. It is free, so you can just give it a try!.

Posted in Open Source, Programming.


Installing OpenCV 2.1 in Ubuntu

UPDATE: You can also install OpenCV 3.2.0 in Ubuntu 16.04LTS.

OpenCV is an excellent library for Computer Vision. I have been using it for years and it helped me a lot during my master thesis.

OpenCV 1.0 can be easily installed in Ubuntu via the repositories. You can install OpenCV 2.0 by following one of my previous posts https://www.samontab.com/web/2010/03/installing-opencv-2-0-in-ubuntu/

Unfortunately, the newer version of OpenCV, 2.1, which was released on April has a slightly different installation procedure. Since it contains many bug fixes and some nice new additions, I will show you how to install it.

UPDATE: Install OpenCV 2.2 in Ubuntu 11.04 with Python and TBB support here.

Here are the steps that I used to successfully install OpenCV 2.1 in Ubuntu 9.10. I have used this procedure for previous versions of Ubuntu as well with minor modifications (if any).

First, you need to install many dependencies, such as support for reading and writing jpg files, movies, etc… This step is very easy, you only need to write the following command in the Terminal


sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev

The next step is to get the OpenCV 2.1 code:

cd ~
wget https://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.1/OpenCV-2.1.0.tar.bz2/download
tar -xvf OpenCV-2.1.0.tar.bz2
cd OpenCV-2.1.0/

In this version of OpenCV, the configure utility has been removed. Therefore you need to use Cmake to generate the makefile. Just execute the following line at the console. Note that there is a dot at the end of the line, it is an argument for the cmake program and it means current directory.

cmake .

Check that the above command produces no error and that in particular it reports FFMPEG as 1. If this is not the case you will not be able to read or write videos.

configuring opencv2.1

Now, you are ready to compile and install OpenCV 2.1:

make
sudo make install

Now you have to configure the library. First, open the opencv.conf file with the following code:

sudo gedit /etc/ld.so.conf.d/opencv.conf

Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:

/usr/local/lib

Run the following code to configure the library:

sudo ldconfig

Now you have to open another file:

sudo gedit /etc/bash.bashrc

Add these two lines at the end of the file and save it:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

Finally, open a new console, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.

Now you have OpenCV 2.1 installed in your computer.

Let’s check some demos included in OpenCV:

cd ~
mkdir openCV_samples
cp OpenCV-2.1.0/samples/c/* openCV_samples
cd openCV_samples/
chmod +x build_all.sh
./build_all.sh

Some of the training data for object detection is stored in /usr/local/share/opencv/haarcascades. You need to tell OpenCV which training data to use. I will use one of the frontal face detectors available. Let’s find a face:

./facedetect --cascade="/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg

Note the scale parameter. It allows you to increase or decrease the size of the smallest object found in the image (faces in this case). Smaller numbers allows OpenCV to find smaller faces, which may lead to increasing the number of false detections. Also, the computation time needed gets larger when searching for smaller objects.

In OpenCV 2.1, the grabcut algorithm is provided in the samples. This is a very nice segmentation algorithm that needs very little user input to segment  the objects in the image. For using the demo, you need to select a rectangle of the area you want to segment. Then, hold the Control key and left click to select the background (in Blue). After that, hold the Shift key and left click to select the foreground (in Red). Then press the n key to generate the segmentation. You can press n again to continue to the next iteration of the algorithm.

./grabcut lena.jpg

This image shows the initial rectangle for defining the object that I want to segment.

Now I roughly set the foreground (red) and background (blue).

When you are ready, press the n key to run the grabcut algorithm. This image shows the result of the first iteration of the algorithm.

Now let’s see some background subtraction from a video. The original video shows a hand moving in front of some trees. OpenCV allows you to separate the foreground (hand) from the background (trees).

./bgfg_segm tree.avi

There are many other samples that you can try.

Posted in Computer Vision, Open Source, OpenCV.


Compiling Java Code

Java is a very popular language. Here is an introduction on how to compile and run Java programs. The first step is to download and install the latest Java Development Kit (JDK), which allows you to compile and run java code. Note that there is a Java Runtime Environment (JRE) which only allows you to run applications made in Java, you have to download the JDK which contains the development and runtime environments. You can download it from here.

The next step is to write a simple Java program. Using any plain text editor, write the following code:

class mainClass
{
    public static void main(String args[])
    {
    System.out.println("Hello there...");
    }
}

The filename has to be the same as the name of the class defined in the code. In this case, the class that I created is called mainClass. Also, the filename needs to have a .java extension. Therefore, the file has to be saved under the name of mainClass.java. Now you can compile the code using javac which is the Java compiler.

javac *.java

If you need to include some other folders with pre-compiled .class files or other libraries in .jar format, you need to use the -classpath argument. Note that for each .jar you need to specify the complete path. Each inclusion is separated with a semicolon (;). Remember to include the current directory by adding a single dot (.).

javac -classpath .;"c:\path\to\classFiles";"c:\path\to\lib1.jar" *.java

Now the code is ready to be run. Just call java and the name of the class that contains the Main function, which in this case is mainClass.

java mainClass

You can also pack your application into a Jar file, which basically is a compressed file containing the machine code you already compiled and a manifest file. The manifest file tells extra information about the application, in particular which one is the main class to execute. Let’s create this file. Open a text editor and write the following:


Main-Class: mainClass
Manifest-Version: 1.0

As you can see, you have to define which is the Main-Class that is going to be executed when the Jar file is run. Save this file as Manifest.txt. Although the second line itself is optional, it is important to add it (or even just a blank line) below the Main-Class definition because the Main-Class line needs to contain an end of line character in order to work properly. If you do not add an end of line character at the end of the Main-Class line you will receive the following error when you try to run your Jar file:

Failed to load Main-Class manifest attribute from main.jar

Jar is the application that allows you to pack your application. The c argument means that you are creating a Jar file. The f argument allows you to give a filename instead of sending the output to the console. The m argument means that you are telling the program where the manifest file is. Then, you need to provide the jar filename and the manifest filename. In this case, I used the main.jar filename but you can use any name as long as it has a .jar extension. The manifest filename can be anything you want, in this case I used Manifest.txt which was created previously. The last argument tells the program which files to include in the Jar package, in this case I simply used all the files that end in .class which is general enough for this introduction.

jar cfm main.jar Manifest.txt *.class

Now you have your application packed in a Jar file. If you want to run it, you only need to write this:

java -jar main.jar

Now you are able to compile and run Java programs.

class myfirstjavaprog
{
        public static void main(String args[])
        {
           System.out.println("Hello World!");
        }
}

Posted in Programming.


Installing OpenCV 2.0 in Ubuntu

OpenCV is an excellent library for Computer Vision. I have been using it for years and it helped me a lot during my master thesis.

OpenCV 1.0 can be easily installed in Ubuntu via the repositories. Unfortunately, the newer version of OpenCV, 2.0, which is full of new features is not yet available in the repositories of Ubuntu.

Here are the steps that I used to successfully install OpenCV 2.0 in Ubuntu 9.10. I have used this procedure for previous versions of Ubuntu as well with minor modifications (if any).

First, you need to install many dependencies, such as support for reading and writing jpg files, movies, etc… This step is very easy, you only need to write the following command in the Terminal


sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev

The next step is to get the OpenCV 2.0 code:


cd ~
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.0/OpenCV-2.0.0.tar.bz2/download
tar -xvf OpenCV-2.0.0.tar.bz2
cd OpenCV-2.0.0

Now, you need to configure how you want to compile OpenCV 2.0. In my case I used /usr/local as the installation directory and I also activated the video support for reading and writing videos using OpenCV (via ffmpeg).


./configure --prefix=/usr/local --enable-apps --enable-shared --with-ffmpeg --with-gnu-ld --with-x --without-quicktime CXXFLAGS=-fno-strict-aliasing

Check that the above command produces no error and that in particular it reports ffmpeg as yes. If this is not the case you will not be able to read or write videos.

Now, you are ready to compile and install OpenCV 2.0:


make
sudo make install

Now you have to configure the library. First, open the opencv.conf file with the following code:


sudo gedit /etc/ld.so.conf.d/opencv.conf

Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:


/usr/local/lib

Run the following code to configure the library:


sudo ldconfig

Now you have to open another file:


sudo gedit /etc/bash.bashrc

Add these two lines at the end of the file and save it:


PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

Finally, open a new console, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.

Now you have OpenCV 2.0 installed in your computer.

Let’s check some demos included in OpenCV:

cd ~
mkdir openCV_samples
cp /usr/local/share/opencv/samples/c/* openCV_samples
cd openCV_samples/
chmod +x build_all.sh
./build_all.sh

All the training data for object detection is stored in /usr/local/share/opencv/haarcascades. You need to tell OpenCV which training data to use. I will use one of the frontal face detectors available. Let’s find a face:

./facedetect --cascade="/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg

Note the scale parameter. It allows you to increase or decrease the size of the smallest object found in the image (faces in this case). Smaller numbers allows OpenCV to find smaller faces, which may lead to increasing the number of false detections. Also, the computation time needed gets larger when searching for smaller objects.

Now let’s see some background subtraction from a video. The original video shows a hand moving in front of some trees. OpenCV allows you to separate the foreground (hand) from the background (trees).

./bgfg_segm tree.avi

There are many other samples that you can try.

Posted in Computer Vision, Open Source, OpenCV.