uWSGI + Flask + NGinx configuration

uwsgi.ini:

[uwsgi]

chdir           = /home/ubuntu/nivose
virtualenv      = /home/ubuntu/nivose/venv/
module          = app:app

master          = true
processes       = 2
socket          = /tmp/nivose.sock
chmod-socket    = 666
vacuum          = true

/etc/nginx/sites-enabled/:

upstream nivose_flask {
    server unix:///tmp/nivose.sock;
}


server {
    listen 80;
    server_name nivose.5pointers.com;
    access_log /var/log/nginx/nivose.access.log;
    error_log /var/log/nginx/nivose.error.log;


    location /static/ { # STATIC_URL
        alias /home/ubuntu/nivose/app/static/; # STATIC_ROOT
        expires 30d;
    }

    location /media/ { # MEDIA_URL
        alias /home/ubuntu/nivose/app/media/; # MEDIA_ROOT
        expires 30d;
    }

    location / {
        uwsgi_pass  nivose_flask;
        uwsgi_param  QUERY_STRING       $query_string;
        uwsgi_param  REQUEST_METHOD     $request_method;
        uwsgi_param  CONTENT_TYPE       $content_type;
        uwsgi_param  CONTENT_LENGTH     $content_length;

        uwsgi_param  REQUEST_URI        $request_uri;
        uwsgi_param  PATH_INFO          $document_uri;
        uwsgi_param  DOCUMENT_ROOT      $document_root;
        uwsgi_param  SERVER_PROTOCOL    $server_protocol;
        uwsgi_param  REQUEST_SCHEME     $scheme;
        uwsgi_param  HTTPS              $https if_not_empty;

        uwsgi_param  REMOTE_ADDR        $remote_addr;
        uwsgi_param  REMOTE_PORT        $remote_port;
        uwsgi_param  SERVER_PORT        $server_port;
        uwsgi_param  SERVER_NAME        $server_name;
    }
}

To start the project use command=uwsgi --ini /home/ubuntu/nivose/uwsgi.ini (I'm using supervisor to manage processes).

Github blogging UI

I've finally built a simple UI for managing blogs hosted on Github.com. Here's the link: Nivose

Vagrantfile for OpenCV and Python

I use the following script to create an Ubuntu 15.04 Vagrant box with OpenCV with Python bindings support:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/vivid64"
  #config.vm.network "forwarded_port", guest: 8000, host: 8000

  config.vm.provision "shell", inline: <<-SHELL

    version="$(wget -q -O - http://sourceforge.net/projects/opencvlibrary/files/opencv-unix | egrep -m1 -o '\"[0-9](\.[0-9])+' | cut -c2-)"
    echo "Installing OpenCV" $version
    mkdir OpenCV
    cd OpenCV
    echo "Removing any pre-installed ffmpeg and x264"
    sudo apt-get -qq remove ffmpeg x264 libx264-dev
    sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu vivid main multiverse"
    sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu precise main universe"
    sudo apt-get update
    echo "Installing Dependencies"
    sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg libxvidcore4


    wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download
    echo "Installing OpenCV" $version
    unzip OpenCV-$version.zip
    cd opencv-$version
    mkdir build
    cd build
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
    make -j2
    sudo checkinstall
    sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
    sudo ldconfig

    echo "OpenCV" $version "ready to be used"

    sudo apt-get install -y python-opencv
    sudo ln /dev/null /dev/raw1394

  SHELL
   
end

Extracting background from a video using OpenCV and Python

Input video example:

Code:

import cv2
import numpy as np


def main(filename):
    capture = cv2.VideoCapture(filename)
    _, first_frame = capture.read()
     
    average = np.float32(first_frame)
    
    while 1:
        ret, frame = capture.read()
        cv2.accumulateWeighted(frame, average, 0.01)
        bg = cv2.convertScaleAbs(average)
        cv2.imwrite('bg.png', bg)
        
    capture.release()



if __name__ == "__main__":
    main(sys.argv[1])