Monthly Archives: February 2012

DANG on a Cloud

Thats Django, ArchLinux ,Nginx and Gunicorn on an Amazon EC2instance.Getting a server to play around with root access courtesy of Amazon AWS is pretty easy, just sign up provide your credit card details and you are almost ready to go.First things first setting up the server.Theer are plenty of ready-made AMI images floating out there so one is really spoilt for choice , personally my preference is for Archlinux having run the rolling release distro as a primary dev machine for the better part of a decade it seemed right to give it a run as a production server.Loading the instance was pretty straightforward (instructions available on AWS blogs and forums).As with all good Arch systems getting it up to the latest version is as easy as:

pacman -Syu
once the changes have been made to the system and confirmed it time to setup the environment.The deployed app will be deployed within a virtualenv , this has the advantage of keeping the python installation of the app sand-boxed from that running on the system with a simple:

pacman -S virtualenv
then executing
virtualenv /path/to/apps -p /usr/bin/python2
source /path/to/apps/bin/activate
once created install the source control system of choice (assuming the app is being managed by one) or good old sftp can work just as well with filezilla .Once unpacked into its deployment folder
installing the project requirements is a case of :
pip install requirements.txt
pip install gunicorn.
As the gunicorn server will be running embedded from within the django app its a matter of adding it to the file and running it will be:

python run_gunicorn
The final part of the puzzle is the nginx server, lighter than the more resource hungry Apache and way simpler to configure with the editing of one file the nginx.conf to server the static assets from the project .
   upstream app_server {
server localhost:8000 fail_timeout=0;

server {
listen 80 default;
client_max_body_size 4G;
server_name _;

keepalive_timeout 5;

location /static/ {
autoindex on;
alias   /home/path/to/site_media/static/;

location / {
# checks for static file, if not found proxy to app
try_files $uri @proxy_to_app;

location @proxy_to_app {
proxy_pass_header Server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

proxy_pass   http://app_server;

error_page 500 502 503 504 /500.html;


The gunicorn server will execute the python thread to a port that the nginx will listen to and server to the world your great new web-app hosted on amazon cloud.
Leave a comment

Posted by on February 11, 2012 in code, Internet & networks, Python


Tags: , , , , , ,

Human Target in less than 90 lines of Code (Python+OpenCV)

Using python and the versatile OpenCV library its possible to get a human detection system in less than 90 lines of code.This assumes a basic knowledge of python and some working knowledge of the OpenCV computer vision library.


The initialization selects whether a camera is to be used or captured image file from the disk,with the inbuilt functions as many cameras as needed can be connected starting from CAM(0) and going up incrementally.

   def __init__(self , typ , fpath):
if( typ == 1 ):
self.capture = cv.CaptureFromCAM(0)
elif( typ == 2 ):
self.capture = cv.CaptureFromFile(fpath)
cv.NamedWindow(“Target”, 1)


Before the human can be detected we have to clear up the artifacts that may reduce the efficiency of our detection algorithms.This involves Gaussian smoothing to eliminate false positives.

The capture is gray-scale converted as this color space can be worked on faster than a full color space and re-sized to a smaller size also for the same added speed in processing.

           # Smooth to get rid of false positives
cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0)

# Convert the image to grayscale.
cv.CvtColor(color_image, grey_image, cv.CV_RGB2GRAY)

# scale input image for faster processing
cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

cv.EqualizeHist(small_img, small_img)


The detection is carried out by utilizing a HaarCascade based on Viola-Jones.After training to detect a specific object of interest , the cascade file (an XML) is loaded with into the HaarDetectObjects function which stores the detected objects as a tuple which when iterated over yields our detected objects position.

cascade = cv.Load(“data/HS.xml”)

faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
haar_scale, min_neighbors, cv.CV_HAAR_DO_CANNY_PRUNING, min_size)

A central point is computed and a rectangle with a bulls-eye.The VideoWriter provides a easy way to capture an recording of the whole program run.

writer = cv.CreateVideoWriter(“output.avi”, cv.CV_FOURCC(‘M’,’J’,’P’,’G’), 15, frame_size)

cv.WriteFrame(writer, color_image)

humans found
Leave a comment

Posted by on February 7, 2012 in Computer Vision, Python


Tags: , , ,