analitics

Pages

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.

Monday, June 25, 2012

Access system version information using python platform module.

The platform module includes the tools to take some infos about operating system, and hardware platform where a program is running.

import platform
dir(platform)

Show all about this module.

Also you can use the help.

help(platform)

Let's try another

print platform.win32_ver()
('', '', '', '')

So is not Windosw OS.

print platform.system()
Linux

Can be 'Linux', 'Windows' or 'Java' ...

print platform.version()

Show you the system's release version ( can be Debian , Ubuntu , Fedora ).

print platform.architecture()
('32bit', 'ELF')
print platform.uname()

Show the infos like uname linux command.

print platform.release()

Show the kernel use by system.

print platform.machine()
i686
print platform.node()

Show the computer's network name.

print platform.linux_distribution()

Show you the linux distribution.

Monday, April 30, 2012

Create tile image for your game using python script

What is tile image?
Tile image is a method of storing a sequence of images placed in a single image file.
These images are then processed according to user needs.
Here's an example below:
How we can create these images?
We can use graphics editing software to create them separately.
I used Blender 3D to create separate images.
A tutorial how to do this can be found here on section Blender 3D.
After I rendered images separately and named: 0000.png , 0001.png , 0002.png , 0003.png
I created a python script to put in an tile image, see below:
import os
import PIL
from PIL import Image
from PIL import ImageDraw
o=Image.new("RGBA",(192,48))
d= ImageDraw.Draw(o)
for pic in range(0,4):
        strpic=str(pic)
        filnam="000"+strpic+".png"
        x=pic*48
        img=Image.open(filnam)
        o.paste(img,(0+x,0))
        o.save("out.png")
The script reads the image files of size 48 pixels and puts them into one image called out.png

Resize screenshot with PIL python module .

The script that I've created is made ​​to shrink images. Some screenshots are large and should be resized to be used later on the Internet. It is a simple example that uses PIL module. This script reads the image name that I want to resize and filename that will be saved image. I use python PIL functions how to create a new image.
"""
This python script read the name of image and will create a new image with the given width and height.

$ python imgresz.py 
filename input image:test.png
test.png
filename output image:test-out.jpg
->width:500
->height:400
"""
import os 
import sys
from PIL import Image 
from PIL import ImageDraw
filnaminp=raw_input("filename input image:")
filnamout=raw_input("filename output image:")
w=input("->width:")
h=input("->height:")
imgi=Image.open(str(filnaminp))
imgo=imgi.resize((w,h),Image.BILINEAR)
imgo.save(str(filnamout))