Skip to content

Inesa09/NormalSmartDevice

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Normal Smart Device

Technical white paper

Authors: Normal Team - Dmytro Koziy, Inesa Hermaniuk, Nazar Mamchur

Published: May 2018

Applies to: Normal Smart Device

Summary: This paper provides an overview of how Normal Smart Device users can set up the person identification smart system based on a Raspberry Pi 3, using this API.

Getting Started with Normal Smart Device API

  1. Creating RESTful client on the Raspberry
  2. Face detection
  1. Creating DataSet
  2. Face Recognition
  3. How to run the server?

Sequence diagram and diagram of classes in this system

sequenceDiagram

diagramOfClasses

Getting Started with Raspberry Pi

This section provides an information to help you succesfully set up Raspberry Pi to get a powerful portable camera.

Prerequisites:

  1. Raspberry Pi 3
  2. MicroSD card with cappacity 8GB or more
  3. USB webcam
  • Set up your Raspberry Pi 3 - install OS by following this guide
  • Install Python 3.6
$ sudo apt-get install python-dev
$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
  • Install numpy
$ pip install numpy 
  • Install OpenCV by following this guide

  • Install subversion

$ sudo apt-get install subversion

Creating RESTful client on the Raspberry

  1. Download Device folder with code from Github repository.
$ cd 
$ svn checkout https://github.com/Dmytruto/NormalSmartDevice/trunk/Device
  1. Make program run at a startup:
  • Open rc.local file:
$ sudo nano /etc/rc.local
  • Add the following line there
sudo python /home/Device/detection.py &

Face detection

Import detection.py

You need to import module in a file you want to use it in

import Device.detection as FDR

Image preprocessing

FDR.detect(img)

Parameter: img - image, where you want to detect a face and crop it out.

Return type - numpy.array

The function detect(img) detects face on an image and returns a croped and grayScaled image of a face if it was detected, or a numpy.array([0]) if not.

Take photos

FDR.process()

Parameter: void
Return type - void

The function process() contains a while True loop which takes 2 photos on webcam per second, converts an image to the base64 String.

Make request to the server

FDR.request(str)

Parameter: str - String, you want to send to the server

The function request(str) sends request with the str String to the server and returns its response.

The server with recognition algorithm

This section provides an information to help you succesfully set up a server, which can recognize a human face and determine if this person is in the data base.

Prerequisites

  1. Install python 3.6.

  2. Install, create and activate your virtual environment in the Recognition folder by following this guide:

  3. Install the required modules:

$ pip install django==2.0.5 
$ pip install djangorestframework==3.8.2
$ pip install opencv-python==3.4.0.12 
$ pip install opencv-contrib-python==3.4.0.12
$ sudo apt-get install subversion

Clone the Repository

  1. Download Recognition folder with code from Github repository.
$ cd path/to/your/dir
$ svn checkout https://github.com/Dmytruto/NormalSmartDevice/trunk/Recognition

Creating DataSet

Import CreateTrainingDataAndTrainModel.py

First of all you need to import our module in file where you want to use it.

import Recognition.FacialRecognition.CreateTrainingDataAndTrainModel as CTD

Next step

This function creates a directory named faces which will be contain a sub folders with photo set. In input this function takes the directory name and creates the subfolder with inputted name.

  CTD.create_directory(sub_directory_name)

Exstracting face from the image

In input this function has the image. if this image contains faces, function will returns grayscaling cropped face from this image.else it will return emty list.

CTD.face_extractor(img)

Take a photos

This function reads images from your web cam and if face is on image it saves the image in subfolder that you have already created. In input this function takes number of photo which you want to be in your data set.

CTD.start_creating_data_set(quantity)

Train model

This function create histograms from all images in subfolder and save this histogram in json file. In input this method has name of trained model. In order to program work successfully file_name must be the same as sub_directory_name.

CTD.trainModel(file_name)

!To create data set and trained model on this data you have to execute this functions in the same order that It had been written on this article!

Face Recognition

Import FaceRecognition.py

Import reconizer which have already trained on the Data Set.

import Recognition.FacialRecognition.FaceRecognition as FR

Run Recognizer

In input this function takes human face image and compare it with faces which had been used to create DataSet. If face on the input image is similar to one of the faces in data set, function will return true, else false.

  FR.face_recognizer(face_image)

In order to use face recognizer firstly you need create data set and train model

How to run the server?

There are two possible ways how to run server in your computer:
  • Using terminal
  1. Activate your virtual environment.
  2. Move to the server folder and run the following command:
  $ python manage.py runserver
  • Using PyCharm
  1. Open project, that you cloned.
  2. Go to File - Settings - Project - Project Interpreter and add local interpretener, choosing path to Scripts/python.exe in the folder with your virtual environment.
  3. Go to Run - Edit Configurations and add new configuration:
  Name: Django Run Server 
        (If there is already configuration with name 'Django Run Server', edit only 'Python interpreter')
  Script path: \your\path\to\Recognition\server\manage.py
  Parameters: runserver
  Python interpreter: *choose that, what you have created*
  1. Run 'Django Run Server'.

Valid request

Server is mapping a POST request with a text file, in which there is an encoded photo, by the method post.

def photo(request):
   if request.method == 'POST' and request.FILES['file']:

Request shoud be sent to http://{host}:{port}/mapping/photo. If server is run on the local machine, this url looks like http://localhost:8000/mapping/photo.

Response

Server sends back one of the String responses, which are provided by the recognition algorithm:

*name* allowed
Not allowed

Example request and response

response

Uploaded image

The image, which was got during the last request mapping, is saved to the uploaded_image folder.

!Check if everything is installed in the correct folders and all paths are proper!

About

Django web service with face recognition algorithm

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%