Saturday, June 27, 2009

Python: Simple Singleton for Mysql Access

I was trying to come up with a simple implementation of Singleton pattern in python, its a bit different from other language constructs. Here is the code implementing a single point database access using MySQLdb.

#! /usr/bin/python

import MySQLdb
import sys

class Conn(object):
__instance = None
__conn = None


def __init__(self):
try:
if Conn.__instance == None:

Conn.__instance = self
Conn.__conn = MySQLdb.connect (host="localhost", user="user", passwd="pass", db="dbname")

except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])

sys.exit(1)


def get_connection(self):
return Conn.__conn


if __name__ == "__main__":
conn = (Conn()).get_connection()

''' Do your thing '''
conn.close()


It's not perfect I guess, but right now it serves the purpose. You are welcome to put down any valuable advice into this.

Friday, June 26, 2009

Mysql: Dumping only stored procedures

I had to find a way to dump ONLY the stored procedures from a mysql database. Here is how I did it.
mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt "databasename" > stored_procedure.sql



PS. This is the shortest post that I ever made. :)

Thursday, June 25, 2009

Python: C++ style cin, cout in Python

Ready to bring some flavor of C++ into python? If you like cout, cin in C++ and also a python programmer, you'd definitely like this snippet.
import sys
class ostream:
def __init__(self, file):
self.file = file

def __lshift__(self, obj):
self.file.write(str(obj));
return self
cout = ostream(sys.stdout)
cerr = ostream(sys.stderr)
endl = '\n'

x, y = 'Printing', 'like C++'
cout << x << " " << y << endl
Pretty cool, huh. I found this and a lot of other cool stuffs from Peter Norvig's blog. Its such a nice blog, just subscribed to it.

Sunday, June 21, 2009

Computer Vision: Working with OpenCV and beyond

For my software Engineering project, I had chosen to work on something different other than database and information management system that we typically take in this course. I've had enough of it in my last term. So this time we three group members choose to work on Computer Vision System. At first we where quite confused because we knew nothing about this field and given our inexperience, it was really difficult. Even our project requirement was a bit hazy, so to speak :)

We tried to learn about the basic concepts of computer vision, image analysis and had to go through an extensive set of frameworks to evaluate which fits our needs. Initially our project requirement was to detect/recognize human facial structure but this was later retrofitted with object detection and feature extraction. Through the course of time we tried to study a lot of research papers (most of them seemed to me like Egyptian Hieroglyphics, :(( ...).

The first framework we came across was Torch3Vision. This framework is built on top of Torch3, which is gives the user a lot of image processing algorithms. Torch3Vision is extensively built for Facial detection. While it was really a good framework, it proved a bit sturdy to customize for our general need as the project requirement started to change.

We later started to study about Content Based Image Retrieval (CBIR) techniques, two of the framework we took into account was GIFT (also known as GNUIFT, to avoid name confusion with another open source tool) and Lire. Lire is written in java and uses Lucene api in the backend, while GIFT is written in C/C++. These implementations enable the user to search images by 'Query by Example', not what we exactly wanted to do. So yeah, another dead end what it seemed ...

The last one, and which of course we choose to work on was OpenCV. OpenCV is an open source library initially built and later sponsored by Intel. It is used extensively in various fields of computer vision, so its in active development and well documented. It also comes with a comprehensive e-book that can help you a lot in crunching the heavy duty libs for the first time.

We also took help from another project that uses OpenCV library to detect objects using 'Boosted Histograms' method, its called objectdet. Here I'm going to go through the basic steps of running OpenCV library in Linux as well as compiling objectdet in Linux. Though OpenCV comes with all major linux distros, I recommend direcetly downloading and compiling from the svn. They have a very extensive wiki, this
page has the detailed instructions for installing in all OS. One thing I'd like to mention here, is the CMake based build is more straightforward than Autotools as it sometimes (on rare occasions) breaks.

After you have OpenCV set up and running, you can try to install 'objectdet' on your system (I'm running it in a Ubuntu 9.04). It hasn't been on active development for a while and running the old code with new libs can surely have its moments(!!!).


sudo apt-get install build-essential libxml++-dev libboost-filesystem-dev
svn checkout http://objectdet.googlecode.com/svn/trunk/ objectdet-read-only
cd objectdet-read-only/objectdet
chmod a+X autogen.sh
./autogen.sh
mv Makefile.am makefile.am
sudo ln -s /usr/lib/libboost_filesystem.a /usr/lib//libboost_filesystem-gcc.a
sudo ln -s /usr/lib/libboost_filesystem.so /usr/lib//libboost_filesystem-gcc.so
./configure --with-opencv-headers=/usr/local/include/opencv/
cd src
make
sudo make install
cd ..

Run the demo

src/objectdet -i images/000537.png -c classifiers/cabmodel_interm_nst40_VOC06car01train5_trainval_orienthistmod.xml

A simple window with a car being detected should appear. Here the xml file contains the trained data for the 'car' object. How to create this trained xml? Well, that's the harder part, I'll try to write that in my next post when I get some time from my GSoC project.

If you are interested in working in Computer Vision and searching for a good set of library, apart from the above mentioned ones, also take a look at these.

1. Gandalf (yeah Gandalf, I really liked the name)
2. This site hosts a lot of computed vision projects
3. LTI-LIB


Wednesday, June 17, 2009

Python: Working in Unicode

For my ongoing Google Summer of Code project, I need to write a lot of scripts that run analysis on Bengali words. So far I had been doing away with shell scripts and a lot of php-cli scripts. But writing long object oriented code in php seems a bit cumbersome to me (that's my personal opinion, no offense to the die hard php-lovers :)), so I decided to move to python. Most of the scripts that my mentor provided me was also in python, so it made really good sense.

While working on unicode based characters in python, you'll often come across this type error message (this cost me a while to fix).


UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)


This will happen if you do not set your character encoding in your python file to UTF-8. First you need to make sure the first few lines of your programm looks like this.


#!/usr/bin/python
# coding=utf-8
# -*- encoding: utf-8 -*-


This enables you to write unicode characters in your source code. But this does not enable you to print them in console and you'll still keep getting the same error I previously mentioned.

To solve this you need to add the following code in your /usr/lib/python2.5/sitecustomize.py file (This might change depending your installed python version)


import sys;
sys.setdefaultencoding('utf-8')


I first tried to did this in the source code but it didn't work. I kept getting this error

AttributeError: 'module' object has no attribute 'setdefaultencoding'


I'm no python expert, but maybe python's default behavior is not to allow changing of the encoding in runtime just for safety (an increasing amount of system tools are written in python these days and they run all the time in Gnome and KDE). That'd make more sense.

Back to Blogging

After a failed attempt last year to get back to blogging, I'm trying it again this year. I really wanted to get back, but got busy will...