Monday, September 12, 2016

[Haar Classifier] Train your own OpenCV Haar classifier

I just keep a record for myself because there are a lot of documents teaching how to train your haar classifier and almost of them seem to don't work well. The following 2 items are clear and easy to understand.

The Data Image Source (cars) I use.

1. Train your own OpenCV Haar classifier

find ./positive_images -iname "*.pgm" > positives.txt
find ./negative_images -iname "*.pgm" > negatives.txt

perl bin/ positives.txt negatives.txt samples 550\
  "opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1\
  -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 48 -h 24"

python ./tools/ -v samples/ -o samples.vec

opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt\
  -numStages 10 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000\
  -numNeg 600 -w 48 -h 24 -mode ALL -precalcValBufSize 1024\
  -precalcIdxBufSize 1024

2. OpenCV Tutorial: Training your own detector |
find pos/ -name '*.pgm' -exec echo \{\} 1 0 0 100 40 \; >
find neg/ -name '*.pgm' > bg.txt

opencv_createsamples -info -num 550 -w 48 -h 24 -vec cars.vec
opencv_createsamples -w 48 -h 24 -vec cars.vec

opencv_traincascade -data data -vec cars.vec -bg bg.txt \
 -numPos 500 -numNeg 500 -numStages 10 -w 48 -h 24 -featureType LBP

P.S: Which one is best? I don't know...

[Image] How to resize, convert & modify images from the Linux

$ sudo apt-get install imagemagick

Converting Between Formats
$ convert howtogeek.png howtogeek.jpg

You can also specify a compression level for JPEG images:
$ convert howtogeek.png -quality 95 howtogeek.jpg

Resizing Images
$ convert example.png -resize 200×100 example.png
  - to force the image to become a specific size – even if it messes up the aspect ratio
$ convert example.png -resize 200×100! example.png

$ convert example.png -resize 200 example.png
$ convert example.png -resize x100 example.png

Rotating an Image
convert howtogeek.jpg -rotate 90 howtogeek-rotated.jpg

Applying Effects
ImageMagick can apply a variety of effects to an image.
  - For example,   the following command applies the “charcoal” effect to an image:
$ convert howtogeek.jpg -charcoal 2 howtogeek-charcoal.jpg
  - the “Implode” effect with a strength of 1:
# convert howtogeek.jpg -implode 1 howtogeek-imploded.jpg

Batch Processing
for file in *.png; do convert $file -rotate 90 rotated-$file; done


Thursday, September 8, 2016

[TensorFlow] My case to install TensorFlow with GPU enabled

My Operation System is Ubuntu 14.04 LTS 5 and GPU card is GeForce GTX 750Ti

1. Go to and download the driver (
2. For Nvidia to find linux header files (*):
$ sudo apt-get install build-essential linux-headers-$(uname -r)

3. To enable full screen text mode (nomodeset):
$ sudo gedit /etc/default/grub
>> Edit GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomodeset"
Save it and reboot
$ sudo update-grub
$ sudo reboot

4. Log into with Ctl +Alt + F1

5. Stop the X Server service
$ sudo service lightdm stop

6. Install nVidia driver
$ sudo ./

7. Install CUDA (GPUs on Linux)
Download and install Cuda Toolkit
sudo dpkg -i cuda-repo-ubuntu1404-8-0-local_8.0.44-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda

8. Download and install cuDNN
tar xvzf cudnn-8.0-linux-x64-v5.1.tgz
cd cuda
sudo cp include/cudnn.h /usr/local/cuda-8.0/include
sudo cp lib64/* /usr/local/cuda-8.0/lib64
sudo chmod a+r /usr/local/cuda-8.0/lib64/libcudnn*

9. You also need to set the LD_LIBRARY_PATH and CUDA_HOME environment variables. Consider adding the commands below to your ~/.bash_profile. These assume your CUDA installation is in /usr/local/cuda:
$ vim ~/.bashrc
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda-8.0/lib64"
export CUDA_HOME=/usr/local/cuda-8.0
export PATH="$CUDA_HOME/bin:$PATH"
export PATH="$PATH:$HOME/bin"

10. To install TensorFlow for Ubuntu/Linux 64-bit, GPU enabled:
$ sudo pip install --upgrade
To find out which device is used, you can enable log device placement like this:
$ python
>>>> import tensorflow as tf
>>>> sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))