analitics

Pages

Saturday, September 22, 2012

Setting up Vim to work with Python - part 1

You need to use mercurial.

Mercurial is a free, distributed source control management tool.

It is mainly implemented using the Python programming language and C.

To use it , just see the next command.

# hg clone https://vim.googlecode.com/hg/ vim 
requesting all changes
adding changesets
adding manifests
adding file changes
added 3831 changesets with 24526 changes to 2566 files (+2 heads)
updating working directory
2385 files updated, 0 files merged, 0 files removed, 0 files unresolved

Go to vim folder.

# cd vim/src/

Use configure to builds a Makefile file.

src # ./configure --enable-pythoninterp --with-features=huge --prefix=$HOME/opt/vim
configure: creating cache auto/config.cache
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking whether the C compiler works... yes
...

The next commands make builds the program and make install to install the program.

src # make && make install
mkdir objects
...

Now let's try to make working well.

src # mkdir -p $HOME/bin
src # cd $HOME/bin
bin # ls
vim
bin # ln -s $HOME/opt/vim/bin/vim
bin # ls
vim
bin # which vim
/usr/bin/vim
bin # vim --version 
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Sep 21 2009 11:22:49)
Included patches: 1-245

If you read all the output , will see something like this:

+profile +python +quickfix 

The main goal is to use vimrc.

For example you cand do this.

" Wrapping and tabs spaces.
set tw=78 ts=4 sw=4 sta et sts=4 ai

" Update syntax highlighting.
let python_highlight_all = 1

" Create smart indenting
set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class

" Auto completion using ctrl-space
set omnifunc=pythoncomplete#Complete
inoremap <nul> <C-x><C-o>

With just few lines can change and improve your work.

Tuesday, September 18, 2012

How to compile your python script .

There're situations when we want to compile the python script.

In this case , we have a python script named your_script.py.

The next script code will make one new your_script.pyc file.

import py_compile
py_compile.compile('your_script.py')

Note: this will hide the python code, but some strings can be view.

For example , if you use this in your_script.py.

print 'This is a string'

The string This is a string can be show in pyc file.

Thursday, September 13, 2012

Simple python script - Voronoi diagram

Today I show you how to make Voronoi diagram using python.

I use this to make textures for underwater.

This is just one example. But you can improve to control all cells of voronoi diagram.

The theory say:

In mathematics, a Voronoi diagram is a special kind of decomposition of a metric space, determined by distances to a specified family of objects (subsets) in the space. These objects are usually called the sites or the generators...Source : wikipedia.

I used the euclidean distance to make the Voronoi diagram because it's the most familiar case.

About wikipedia - Euclidean_distance: In mathematics, the Euclidean distance or Euclidean metric is the "ordinary" distance between two points that one would measure with a ruler, and is given by the Pythagorean formula...

My python script use the next python modules:

PIL - this allow me to use image functions.

random - this module give me... random numbers.

math - some math functions.

Let's see the source code :


from PIL import Image
import random
import math

Now I make the function named gen_voronoi.

This take the height and width of the output image and the number of cells.

The function has some random variables for red , green , blue - nr,ng,nb.

The function hypot is not accessible directly so we need to import math module and using math static object.

The return value is the Euclidean norm : sqrt(x*x + y*y).


def gen_voronoi(w, h, cells):
 image = Image.new("RGB", (w, h))
 putpixel = image.putpixel
 img_x, img_y = image.size
 nx = []
 ny = []
 nr = []
 ng = []
 nb = []
 for i in range(cells):
  nx.append(random.randrange(img_x))
  ny.append(random.randrange(img_y))
  nr.append(random.randrange(256))
  ng.append(random.randrange(256))
  nb.append(random.randrange(256))
 for y in range(img_y):
  for x in range(img_x):
   dmin = math.hypot(img_x-1, img_y-1)
   j = -1
   for i in range(cells):
    d = math.hypot(nx[i]-x, ny[i]-y)
    if d < dmin:
     dmin = d
     j = i
   putpixel((x, y), (nr[j], ng[j], nb[j]))
 image.save("output.png", "PNG")
 image.show()

Use the function to make the output.png image.


gen_voronoi(200, 200, 55)

The result is :

Thursday, August 30, 2012

Python script using OpenCV to detect / recognition faces on photos

This is old tutorial make long time ago by me to detect faces on photos.

If you know more about OpenCV module , then is easy to understand source code.

First I load the modules:

import opencv.cv as cv
import opencv.highgui as gui
import opencv

Next I set the variables and data blocks processed some particular features of the modules loaded.

hc = cv.cvLoad("haarcascade_frontalface_default.xml")
img = gui.cvLoadImage("me.jpg",cv.CV_BGR2RGB)
storage = cv.cvCreateMemStorage(0)
cascade = cv.cvLoadHaarClassifierCascade('haarcascade_frontalface_alt.xml',cv.cvSize(1, 1))
grayscale = cv.cvCreateImage(cv.cvSize(img.width, img.height), 8, 1)
cv.cvCvtColor(img, grayscale, cv.CV_BGR2GRAY)

This is part where is detect faces and save the output like a jpeg image.

faces = cv.cvHaarDetectObjects(grayscale, cascade,\ 
storage,1.2,2,cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(5, 5))

if faces:
 for i in faces:
  cv.cvRectangle(img, cv.cvPoint( int(i.x), int(i.y)),cv.cvPoint(int(i.x + i.width), int(i.y + i.height)),cv.CV_RGB(0, 255, 0), 3, 8, 0)
gui.cvSaveImage("faces_detected.jpg", img)

The haarcascade_frontalface_default.xml file it's from internet, but you can create one if you want.

Maybe in the next tutorial I will show how.

Let's see the result. The input image file is:

... and the result is:

Saturday, August 25, 2012

The new tutorial about pstats python module.

Today I make a new tutorial about pstats python module.

The pstats module is a statistics browser for reading and examining profiler program.

This is provided in the modules cProfile, profile and pstats.

Because it's a long tutorial with long row , I put this tutorial on my tutorials website.

You can find it here on Python section.

Friday, August 24, 2012

Python 3.2 : Start with Django 1.4.

Although most of us prefer the python version 2.6, today I tried to install the latest version of django and python 2.3.2 .
Make a new folder , named test-dj .
$mkdir test-dj
$cd test-dj/
On the official site, I got the two archives:
django-django-1.4-919-ge57338f.zip
Python-3.2.3.tar.bz2
I will start with the installation of python. We unzip the archive:
$tar xvjf Python-3.2.3.tar.bz2 
We execute the following commands to install python:
$cd Python-3.2.3
$./configure
$make all
$sudo make altinstall
# python3.2 setup.py install 
Let's see what we have.
$ whereis  python3
python3: /usr/lib/python3.0 /usr/local/bin/python3.2m-config
/usr/local/bin/python3.2 /usr/local/bin/python3.2m 
/usr/local/lib/python3.2
As you see it's ...
python3.2
python3.2m
python3.2m-config
In accordance with the PEP-3149 we can got this:
Python implementations MAY include additional flags in the file name tag as appropriate. For example, on POSIX systems these flags will also contribute to the file name:

        * --with-pydebug (flag: d)
        * --with-pymalloc (flag: m)
        * --with-wide-unicode (flag: u)

Now we need to install django.
$unzip django-django-1.4-919-ge57338f.zip
Go to the django folder:
$cd django-django-e57338f/
# python3.2 setup.py install 
Now , we can test it:
# python3.2
Python 3.2.3 (default, Aug 24 2012, 19:24:21) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django 
>>> print(django.get_version())
1.5
>>> 
I will make another tutorial about how to configure the django to have one website.