Example code : Face recognition system in ration shop using python

 Face recognition is a system, that identifies the face of a person. To recognize the face of a person, we need to have their encodings. So the first step will be collecting the images of persons which are to be recognized.

               In this example, we are going to see how to use this face recognition system in the ration shops for purchasing ration materials.



STEP 1: 

Collections of data

                                 The Collections of data can be made manually or by using the webcam. In this example,  we are recognizing face to use in ration shop to buy ration materials. So we need to have a unique number of size 30 , for every family and creates a directory in the folder named images. So the family members images are saved into a unique number directory. This method is repeated for every family.


STEP 2: 

Getting the encodings of the images and labels

                                        Next, the encodings of each image is taken  along with their labels and store it in a list . The encodings are stored in a variable named encodingsList and the labels are stored in a variable named Names.

STEP 3: 

Storing the encodings of the images and labels

                                        Next , all the encodings and their labels are saved into a .dat file , which can be used for face recognition.

STEP 4: 

                    Now , we are ready for face recognition in a real time. We use opencv to open the camera of our laptop or webcam and find the locations of face and finds its encodings,  and match it with all the known face encodings and get the label name of the least face distance of the encodings and show it in the camera.

IMPLEMENTATION:

      We need four python files.
  • RandomFileStore.py
  • EncodingStore.py
  • Face Recognition.py
  • ShopGui.py
RandomFileStore.py:
  •        Import the libraries.
  •        Then ask the user for the family id. If the user knowns that id , he/she can write here and the images are added to the directory of id. if the user does not enter a number, then it would create a random number from 0 to 9 , of length 30., and checks , whether this id , which is the directory is already present , and if it it is already present , then it creates another random id, until the id is not present in the directory, and finally it creates the directory.
  •          After creating the directory, the images need to be stored in the directory. This can be done both manually or using webcam.
  •          Manual adding means, you need to get the image of the respective person and stored it into the respective ration id directory.
  •          And using webcam means, using the opencv python library, to open the camera and the read the photo from the camera and store it into the respective ration id directory, with a folder name of the person .

Now after completing this first step, we have collected all the images of the person  to recognize.So the next step is to find the encodings of the faces and store it into a .dat file. This is done by this python program encodingstore.py


EncodingStore.py:

                          This python script finds the encodings of the face and also find the label name , which is the name of the person in the image, for every images in our images folder. And after getting all the encodings and the label name, it is then stored to a  local disk, which can be used for recognising faces.

Now the data is stored in a local file and can be used for recognizing faces in a image.


Face Recognition.py

  •  In this script the actual face recognition code is written. In this example, we are using a face recognition library to make the process simple. First, we will get the encodings and labels name from the local disk, and store it into a list. 
  •  Then, we will open the webcam or camera in your laptop using opencv library and finds the locations of face in the webcam picture. If it finds a face in a picture, then it will find the encoding of the face and compare it it with all the other known face encodings, which is the list of encodings . Then we will take the least face distance value, and confirms that it is less than our threshold value,
  •  If it is less than our threshold value, then the index of the encoding in the encoding list is taken.  And then , the index value in the  label list is taken, and display it into to the camera , by drawing a rectangle.

ShopGui.py

                 This is the script , which creates a GUI , to show the available ration materials , and to buy that product for money. We use tkinter library, to create a GUI.