Face Detection, Video Streaming with Python, OpenCV, Django – Part 1

Django, Python

(Note:- The post was migrated from the previous blog written on 26th August 2017 web.arvhive.org). That was a lossy migration and the images were not be able to recover using webarchive. See what happend to the previous blog

Hi folks. I got to dive into a totally different Library last week. It is non other than OpenCV . While I was working as an intern at interblocks one of my tech leads said he never tried to stream a video between two machines, Actually he wanted to transfer the stream only when someone is in front of the computer. It is not a work relate one, but just a random idea. So I wanted to give it a try. Here is the basic diagram.

As the Face Detector and End User are talking using REST API I thought to use Django as the web frame work and openCV as the face detection library. I’ve never used both of them before. So I decided to go with those as a learning task.

Django is a high-level Python Web framework. This is the basic setup for a app in Django. I followed the official documentation . Here is a highlight of it.

Install Python.

Install a Python Virtual Environment.

To install the package manager

Download get-pip.py

Go to folder containing get-pip.py and run

python get-pip.py

Installing the virtual environment

pip install virtualen

What virtual environment do is it create a virtual system which is almost like the normal system and it prevents any harm that could be caused because of the programme development The virtual environment. Here you can find an explanation about the importance of the virtual environment very clearly, just have a look at it.

Creating the Project Folder

mkdir env

Go to the folder and create a new project

cd env
Virtualenv mysite

Active the newly created environment

cd env\mysite\scripts\activate

This will change the command line as follows.

(env)C:\Users\username\Documents\mysite>

Now the environment is up and running we can develop ord project on top of that

Install Django.

This will install the latest version of django and you can specify the version by,

pip install django==x.y.z

You can cross check the installation by running the python interpreter here and checking the python version here.

>>>Python
>>>Import Django dJ
>>>DJ.get_version()

Back to the topic. Now we have to starting a new Project. A project can hold multiple apps FYI 😀

django-admin startproject mysite

This will automatically create a new folder with the following structure.

mysite/
  manage.py 
  mysite/
    __init__.py 
    settings.py 
    urls.py 
    wsgi.py

As Django automatically creates some applications by defaults like Admin Programme , User Management , Authentication we have to set up a database in order to work with them.

Direct to mysite. And run

python manage.py migrate

again to make sure that you are in the newly created project folder. In “mysite” you can easily use CD command (in case you are confused with the running environment).

All set!!! Now you can run the server you just created.

python manage.py runserver

If you want to change the server,

python manage.py runserver 8080

If you have done everything right it will look like

Creating your first app.

Go to the project directory where your manage.py is and run the following command.

python manage.py startapp myfirstapp

Testing the app

As the django Doc says,

In views.py

from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

And in url.py

from django.conf.urls import url

Then go back to the the project (mysite ) in this case, /mysite/urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^myfirstapp/', include('myfirstapp.urls')),
    url(r'^admin/', admin.site.urls),
]

This is the very basic set up of a Django app. I will share the web app for the solution using Django in the next post.

Avatar
Sudeepa Nadeeshan
Research Assistant

My research interests include Intelligent Transport Systems, Machine Learning.