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.



import face_recognition as f
import numpy as np
import cv2 as c
import os
import pickle as p
path = 'Images1'
image_dir = 'Images1'
ImageList = os.listdir(path)
Images = []
ImageName=[]
FullImageName = []
print('Getting Images')
print('Getting All the Encodes....')
print('Please Wait...')
encodeList = []
for root, dirs, files in os.walk(image_dir):
for dir in dirs:
a = os.listdir(image_dir+'/'+dir)
for folder_name in a:
b = os.listdir(image_dir + '/' + dir+'/'+folder_name)
for image in b:
FullImageName.append(image)
path = image_dir + '/' + dir+'/'+folder_name
currentImg = c.imread(f'{path}/{image}')
currentImg = c.resize(currentImg, (0, 0), None, 0.25, 0.25)
currentImg = c.cvtColor(currentImg, c.COLOR_BGR2RGB)
encode = f.face_encodings(currentImg, model='large')[0]
if len(encode)>0:
encodeList.append(encode)
Images.append(currentImg)
NameOfImg = os.path.splitext(image)[0]
if (NameOfImg.__contains__('_')):
NameOfImg = NameOfImg.split('_')[0]
ImageName.append(NameOfImg)
else:
ad = os.path.splitext(image)[0]
ImageName.append(NameOfImg)
else:
print('removing...', image)
os.remove(path + '/' + image)
print('Removed', image)
encode = []
break
# print("All Images are imported")
# s= []
# print(ImageName)
# def findEncoding(Images):
# print(ImageName)
# # remove = []
# i = 0
# for img in Images:
# try:
#
# encode = f.face_encodings(img,model='large')[0]
# encodeList.append(encode)
# except:
# # remove.append(i)
# print('removing...',image)
# os.remove(path + '/' + image)
# print('Removed',image)
#
# #
# # print("There is an error in a file at index->" + i.__str__())
# # print(ImageName[i])
#
# # i = i+1
# # remove.reverse()
# # for i in remove:
# # print(i)
# # ImageName.pop(i)
# return encodeList
encodeListForKnownFaces = encodeList
# print(ImageName)
# print('All Encodes are obtained')
with open('dataset_faces.dat', 'wb') as filedata:
p.dump(encodeListForKnownFaces, filedata)
with open('imageName.dat', 'wb') as filedata:
p.dump(ImageName, filedata)
import face_recognition as f
import numpy as np
import cv2 as c
import os
import pickle as p
import keyboard
from RationCardFiles import RationShopIntegrations as ration
from tkinter import messagebox
from tkinter import *
import matplotlib.pyplot as plt
path = 'Images1'
image_dir = 'Images1'
ImageList = os.listdir(path)
Images = []
ImageName=[]
encodeListForKnownFaces = []
import json
print('Enter q to stop and exit')
with open('dataset_faces.dat', 'rb') as filedata:
encodeListForKnownFaces = p.load(filedata)
with open('imageName.dat', 'rb') as filedata:
ImageName.append(p.load(filedata))
a = ImageName
ImageName = ImageName[0]
a = ImageName
s= []
cap = c.VideoCapture(0)
# cap = c.VideoCapture('http://192.168.43.1:4747/mjpegfeed?640x480')
cap.set(c.CAP_PROP_FRAME_WIDTH, 1240)
cap.set(c.CAP_PROP_FRAME_HEIGHT, 980)
NameIndexList = []
def showRationDetails(name,id,dirn,alreadyBouhtL):
ration.main(name,id,dirn,alreadyBouhtL)
bought = []
id_Name = []
nameFol = []
dirN = ""
ration_materials = []
totalList = []
# with open('rationID.json', 'r') as file:
# id_Name = json.load(file)
with open('ration_materials.json', 'r') as file:
ration_materials = json.load(file)['materilas']
def findID(name):
nameFol = []
for root, dirs, files in os.walk('Images1'):
for dir in dirs:
a = os.listdir('Images1/' + dir)
for NameFolder in a:
if(NameFolder.upper()==name):
dirN = dir
for Nf in a:
nameFol.append(Nf)
return nameFol,dirN
break
while True:
if keyboard.is_pressed('q'):
break
NameIndexList = []
success,image = cap.read()
in1 = image
image = c.flip(image, 1)
in1 = c.flip(in1, 1)
imgSmall = c.resize(image, (0, 0), None, 0.25, 0.25)
imgSmall = c.cvtColor(imgSmall, c.COLOR_BGR2RGB)
facesCurrentImage = f.face_locations(imgSmall)
encodeCurrentFrame = f.face_encodings(imgSmall, facesCurrentImage,model='large')
landmarks = f.face_landmarks(imgSmall,facesCurrentImage)
landmarks = landmarks[0]
for part in landmarks:
parts = landmarks[part]
for p in parts:
x, y = p
x = x * 4
y = y * 4
c.circle(in1, (x, y), 4, (255, 0, 0), -1)
# c.imshow("image", image)
if landmarks is not None:
c.imwrite('Landmarks.png',in1)
# print(landmarks.part(0).x)
c.imshow("image", image)
for encodefac,faceloc in zip(encodeCurrentFrame,facesCurrentImage):
borrowed_items = []
NameIndexList = []
matches = f.compare_faces(encodeListForKnownFaces,encodefac)
faceDis = f.face_distance(encodeListForKnownFaces ,encodefac)
match = np.argmin(faceDis)
currentName = ImageName[match].upper()
for i in range (0,len(ImageName)):
name = ImageName[i]
if(name.upper()==currentName):
NameIndexList.append(i)
with open('rationID.json', 'r') as file:
id_Name = json.load(file)
if(len(NameIndexList)>1):
count = 0
NameIndexListLength = len(NameIndexList)
# if (NameIndexListLength - count == 0 or NameIndexListLength - count < 2):
for i in range(0, len(NameIndexList)):
if (matches[NameIndexList[i]] == True):
count = count + 1
with open('bought.json', 'r') as file:
bought = json.load(file)['bought']['names']
if (NameIndexListLength - count == 0 or (NameIndexListLength/2) < count):
if matches[match]:
name = ImageName[match].upper()
family_members,dirN = findID(name)
i = 0
totalList = []
for na in family_members:
for n1 in bought:
if n1.upper() == na.upper():
i =1
borrowed_items = id_Name['ration id'][dirN]
totalList = []
for k in ration_materials:
if ration_materials[k]["available"]=="1":
totalList.append(k)
if(len(totalList) == len(borrowed_items) or len(borrowed_items)>len(totalList)):
root = Tk()
# root = Toplevel()
# root.withdraw()
messagebox.showinfo("Error", "This ID is already purchased")
print('Already Bought!!')
root.destroy()
# exit()
else:
showRationDetails(name, id_Name, dirN,borrowed_items)
break
if i == 0:
showRationDetails(name,id_Name,dirN,borrowed_items)
i = 0
y1, x2, y2, x1 = faceloc
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
c.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
c.rectangle(image,(x1,y2-35),(x2,y2),(0,255,0),c.FILLED)
# c.rectangle(image, (x1 - 10, y2 - 30), (x2 + 10, y2 + 30), (0, 255, 0), c.FILLED)
c.putText(image, name, (x1 -6, y2 -6), c.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
print("Found a Face of " + name)
else:
if matches[match]:
name = ImageName[match].upper()
family_members, dirN = findID(name)
i = 0
# for n1 in bought:
# if(name ==n1.upper()):
# i = i+1
# for na in family_members:
# if name == na.upper():
# i=i+1
totalList = []
for na in family_members:
for n1 in bought:
if n1.upper() == na.upper():
i = 1
borrowed_items = id_Name['ration id'][dirN]
for k in ration_materials:
if ration_materials[k]["available"] == "1":
totalList.append(k)
if (len(totalList) == len(borrowed_items) or len(borrowed_items)>len(totalList)):
root = Tk()
# root = Toplevel()
messagebox.showinfo("Error", "This ID is already purchased")
print('Already Bought!!')
root.destroy()
# exit()
else:
showRationDetails(name, id_Name, dirN, borrowed_items)
break
if i == 0:
showRationDetails(name, id_Name, dirN, borrowed_items)
i = 0
y1, x2, y2, x1 = faceloc
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
c.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
c.rectangle(image,(x1,y2-35),(x2,y2),(0,255,0),c.FILLED)
# c.rectangle(image, (x1 - 10, y2 - 30), (x2 + 10, y2 + 30), (0, 255, 0), c.FILLED)
c.putText(image, name, (x1-6, y2-6), c.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
print("Found a Face of " + name)
borrowed_items = []
# print(name)
c.imshow("image",image)
c.waitKey(1)
key = c.waitKey(1)
if (key == ord('1')):
break
cap.release()
import random
import os
import face_recognition as f
import cv2 as c
sampleNum = 0
ration_unique_id = (input("Enter the ration id "))
image_dir = 'Images1'
def checkforid(ration_unique_id):
for root, dirs, files in os.walk(image_dir):
for dir in dirs:
if dir == ration_unique_id:
return True
return False
while True:
if not ration_unique_id == "":
ImagePath = 'Images1/' + ration_unique_id
break
for i in range(0, 30):
n = random.randint(0, 9)
ration_unique_id = ration_unique_id + (str(n))
if not checkforid(ration_unique_id):
ImagePath = 'Images1/' + ration_unique_id
os.mkdir(ImagePath)
break
else:
ration_unique_id =""
# if ration_unique_id=="":
# ration_unique_id = createIDFolder()
# isalreadypresent = checkforid(ration_unique_id)
#
# if not isalreadypresent:
# ImagePath = 'Images1/' + ration_unique_id
# os.mkdir(ImagePath)
# else:
# createIDFolder()
# else:
# ImagePath = 'Images1/' + ration_unique_id
name = (input("Enter the name"))
ImagePath = ImagePath+'/'+name
isImg = os.path.isdir(ImagePath)
if not isImg:
os.mkdir(ImagePath)
else:
print('The data is already present')
capture = c.VideoCapture(0)
capture.set(c.CAP_PROP_FRAME_WIDTH, 1240)
capture.set(c.CAP_PROP_FRAME_HEIGHT, 980)
def createDataset():
global sampleNum, image
while True:
if(sampleNum==20):
break
success, image = capture.read()
image = c.flip(image, 1)
imgSmall = c.resize(image, (0, 0), None, 0.25, 0.25)
imgSmall = c.cvtColor(imgSmall, c.COLOR_BGR2RGB)
facesCurrentImage = f.face_locations(imgSmall)
encodeCurrentFrame = f.face_encodings(imgSmall, facesCurrentImage)
if encodeCurrentFrame.__len__()>0 and len(facesCurrentImage)>0:
c.imwrite(ImagePath + "/" + name + "_"+sampleNum.__str__()+".jpg", image)
sampleNum = sampleNum + 1
c.imshow("image", image)
c.waitKey(1)
print('Data Created for '+name)
createDataset()
view raw randomfile.py hosted with ❤ by GitHub
import tkinter as tk
from tkinter import *
from PIL import Image,ImageTk
import os
import json
import matplotlib.pyplot as plt
image_dir ='Images1'
current_name = ""
totalprice = 0
# id = []
ration_ = ''
listItems = []
borr_items = []
directory_ID = ""
# with open('rationID.json', 'r') as file:
# id = json.load(file)
def get_image(name):
for root, dirs, files in os.walk('Images1'):
for dir in dirs:
a = os.listdir('Images1/' + dir)
for image in a:
if (image.lower().__eq__(name.lower())):
b = os.listdir('Images1/' + dir+'/'+image)
print(b)
for c in b:
print(c)
return ('Images1/' + dir + '/' + image + '/' + c)
break
break
# def main(name):
# current_name = name
#
# image = get_image(name)
# # print(image)
# #
# # root = Tk()
# #
# # root.title('Ration Card Details')
# # # img = 'C:\\Users\Marnish Prabhu\PycharmProjects\FaceRecogDummy\FullSetupTest\palani.jpg'
# # image = Image.open(image)
# # # image = image.resize((100, 100), Image.ANTIALIAS)
# # im = ImageTk.PhotoImage(image)
# #
# # image = Label(root, image=im, height=500, width=500, padx=60)
# # image.grid(row=1, column=1)
# #
# # img = 'C:\FaceRecognition\FaceRecognition\\rationcard\palani.jpg'
# image = Image.open(image)
# image = image.resize((200, 200), Image.ANTIALIAS)
# im = ImageTk.PhotoImage(image)
#
# image = Label(root, image=im)
# image.grid(row=8, column=4)
#
# root.mainloop()
def main(name,json_ration,directory_id,items):
ration_ = json_ration
directory_ID = directory_id
borr_items = items
# if len (borr_items)>0:
# for item in borr_items:
totalPrice = 0
current_name = name
# root = Tk()
root = Toplevel()
select_all_var = IntVar()
rice_var = IntVar()
wheat_var = IntVar()
oil_var = IntVar()
sugar_var = IntVar()
rr_var = IntVar()
tp_var = IntVar()
# totalPrice = StringVar()
image12 = get_image(name)
image13 = Image.open(image12)
image = image13.resize((200, 200), Image.ANTIALIAS)
im = ImageTk.PhotoImage(image)
image1 = Label(root, image=im)
image1.grid(row=8, column=4)
with open('ration_materials.json', 'r') as file:
materials = json.load(file)['materilas']
def borrow(name):
for item in borr_items:
if name == item:
return True
return False
def select():
e = 'normal'
d = 'disabled'
_var = select_all_var.get()
rice = rice_cb.cget("state")
wheat = wheat_cb.cget("state")
oil = oil_cb.cget("state")
sugar = sugar_cb.cget("state")
rr_c = rr_cb.cget("state")
thuvaram_paruppu = tp_cb.cget("state")
if _var == 1:
if rice == e:
rice_cb.select()
if wheat == e:
wheat_cb.select()
if oil == e:
oil_cb.select()
if sugar == e:
sugar_cb.select()
if rr_c == e:
rr_cb.select()
if thuvaram_paruppu == e:
tp_cb.select()
else:
wheat_cb.deselect()
sugar_cb.deselect()
oil_cb.deselect()
rice_cb.deselect()
rr_cb.deselect()
tp_cb.deselect()
def saveFile():
bought = []
with open('bought.json', 'r') as file:
bought = json.load(file)
bought['bought']['names'].append(current_name)
with open('bought.json', 'w') as file:
json.dump(bought, file)
def buy():
total = 0
listItems = []
# a = ["ration id"]
#
# a['id'] = "123"
rv = rice_var.get()
wv = wheat_var.get()
ov = oil_var.get()
sv = sugar_var.get()
tpv = tp_var.get()
rrv = rr_var.get()
if (rv == 1):
r = int(rp)
total = total + r
listItems.append("Rice")
if (wv == 1):
if (wp == 'FREE'):
listItems.append("Wheat")
pass
else:
w = int(wp)
total = total + w
listItems.append("Wheat")
if (ov == 1):
o = int(op)
total = total + o
listItems.append("Oil")
if (sv == 1):
s = int(sp)
total = total + s
listItems.append("Sugar")
if rrv == 1:
if rrp == 'FREE':
listItems.append("Raw Rice")
pass
else:
wi = int(rrp)
total = total + wi
listItems.append("Raw Rice")
if tpv == 1:
if tpp == 'FREE':
listItems.append("Thuvaram Paruppu")
pass
else:
tppr = int(tpp)
total = total + tppr
listItems.append("Thuvaram Paruppu")
# val = ration_["ration id"]
# print(directory_id)
# print(len(listItems))
before_val = ''
try:
before_val = ration_["ration id"][directory_ID]
for k in before_val:
if not listItems.__contains__(k):
listItems.append(k)
ration_["ration id"][directory_ID] = listItems
except:
print('in the except block')
ration_["ration id"][directory_ID] = listItems
with open('rationID.json', 'w') as file:
json.dump(ration_,file)
totalP.config(text=total)
saveFile()
root.title('Ration Card Details')
name_label = Label(root, text='Photo', font=('Verdana', 20))
name_label.grid(row=7, column=4)
ration_card_name = Label(root, text='Ration Materials', font=('Verdana', 20))
ration_card_name.grid(row=0, column=4)
selectall_cb = Checkbutton(root, variable=select_all_var, text='Select All Available'
, font=('Verdana', 20), command=select, pady=50)
selectall_cb.grid(row=2, column=3)
state = materials['rice']['available']
isAlreadyBorrowed = borrow('Rice')
print(state)
if state == '1' and not isAlreadyBorrowed:
rice_cb = Checkbutton(root, text='Rice', font=('Verdana', 20), variable=rice_var)
else:
rice_cb = Checkbutton(root, text='Rice', font=('Verdana', 20), state=DISABLED)
rice_cb.grid(row=2, column=4)
state = materials['wheat']['available']
print(state)
isAlreadyBorrowed = borrow('Wheat')
if state == '1' and not isAlreadyBorrowed:
wheat_cb = Checkbutton(root, text='Wheat', font=('Verdana', 20), variable=wheat_var)
else:
wheat_cb = Checkbutton(root, text='Wheat', font=('Verdana', 20), state=DISABLED)
wheat_cb.grid(row=2, column=5)
state = materials['sugar']['available']
print(state)
isAlreadyBorrowed = borrow('Sugar')
if state == '1' and not isAlreadyBorrowed:
sugar_cb = Checkbutton(root, text='Sugar', font=('Verdana', 20), variable=sugar_var)
else:
sugar_cb = Checkbutton(root, text='Sugar', font=('Verdana', 20), state=DISABLED)
sugar_cb.grid(row=2, column=6)
state = materials['oil']['available']
print(state)
isAlreadyBorrowed = borrow('Oil')
if state == '1' and not isAlreadyBorrowed:
print('aaa')
oil_cb = Checkbutton(root, text='Oil', font=('Verdana', 20), variable=oil_var)
else:
print('bbb')
oil_cb = Checkbutton(root, text='Oil', font=('Verdana', 20), state=DISABLED)
oil_cb.grid(row=2, column=7)
state = materials['raw rice']['available']
isAlreadyBorrowed = borrow('Raw Rice')
if state == '1' and not isAlreadyBorrowed:
rr_cb = Checkbutton(root, text='Raw Rice', font=('Verdana', 20), variable=rr_var)
else:
rr_cb = Checkbutton(root, text='Raw Rice', font=('Verdana', 20), state=DISABLED)
rr_cb.grid(row=2, column=8)
buy_button = Button(root, text='Buy Materials', command=buy, font=('Verdana', 20)
, bg='blue', fg='white')
buy_button.grid(row=8, column=3)
state = materials['Thuvaram Paruppu']['available']
isAlreadyBorrowed = borrow('Thuvaram Paruppu')
if state == '1' and not isAlreadyBorrowed:
tp_cb = Checkbutton(root, text='Thuvaram Paruppu', font=('Verdana', 20), variable=tp_var)
else:
tp_cb = Checkbutton(root, text='Thuvaram Paruppu', font=('Verdana', 20), state=DISABLED)
tp_cb.grid(row=2, column=9)
#### row 3
# RiceP = StringVar()
# WheatP = StringVar()
# OilP = StringVar()
# SugarP = StringVar()
# DhalP = StringVar()
sp = materials['sugar']['price']
op = materials['oil']['price']
rp = materials['rice']['price']
wp = materials['wheat']['price']
rrp = materials['raw rice']['price']
tpp = materials['Thuvaram Paruppu']['price']
riceLa = Label(root, text='Rice', font=('Verdana', 20), pady=15)
riceLa.grid(row=3, column=2)
ricePr = Label(root, text=rp, font=('Verdana', 20))
ricePr.grid(row=3, column=3)
riceLa = Label(root, text='WheaT', font=('Verdana', 20), pady=15)
riceLa.grid(row=4, column=2)
ricePr = Label(root, text=wp, font=('Verdana', 20))
ricePr.grid(row=4, column=3)
riceLa = Label(root, text='Oil', font=('Verdana', 20), pady=15)
riceLa.grid(row=5, column=2)
ricePr = Label(root, text=op, font=('Verdana', 20))
ricePr.grid(row=5, column=3)
riceLa = Label(root, text='Sugar', font=('Verdana', 20), pady=15)
riceLa.grid(row=6, column=2)
ricePr = Label(root, text=sp, font=('Verdana', 20))
ricePr.grid(row=6, column=3)
riceLa = Label(root, text='Raw Rice', font=('Verdana', 20), pady=15)
riceLa.grid(row=7, column=2)
ricePr = Label(root, text=rrp, font=('Verdana', 20))
ricePr.grid(row=7, column=3)
riceLa = Label(root, text='Thuvaram Paruppu', font=('Verdana', 20), pady=15)
riceLa.grid(row=4, column=4)
ricePr = Label(root, text=tpp, font=('Verdana', 20))
ricePr.grid(row=4, column=5)
totalL = Label(root, text='Total', font=('Verdana', 20), pady=15)
totalL.grid(row=5, column=4)
totalP = Label(root, text=totalPrice, font=('Verdana', 20))
totalP.grid(row=5, column=5)
root.mainloop()