OpenCV Face Recognition Deployment In Flask Web Framework

Dharmaraj
4 min readMay 23, 2023

--

Introduction

If you want to learn OpenCV basics please go through this blog. In this blog, we are going to cover face recognition with Flask API Deployment. The short introduction for OpenCV is a Python library that is designed to solve computer vision problems. It is used in various applications such as face detection, video capturing, tracking moving objects, and object disclosure.

Flask API

Flask is a widely used micro web framework for creating APIs in Python. It is a simple yet powerful web framework that is designed to get started quickly and easily, with the ability to scale up to complex applications.

What is Haar-cascade

It is an Object Detection Algorithm used to identify faces in an image or a real-time video. The algorithm uses edge or line detection features.

Now let’s get into the implementation.

Project Structure

index.html

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<title>Dharmaraj - Face Recognition</title>
<style>
h2
{
padding-bottom:20px;
font-weight: 600;
font-size: 3.2em
}
</style>
<body>
<div class="container"><center><h2>Face Recognition</h2></center>
<div class="col-lg-offset-2 col-lg-8">
<center><form class="form-inline" action = "/stop" method = "post" enctype="multipart/form-data">
<input type = "submit" class="btn btn-danger btn-md btn-block" value="Stop">
</form></center>
<center><form class="form-inline" action = "/start" method = "post" enctype="multipart/form-data">
<input type = "submit" class="btn btn-success btn-md btn-block" value="Start">
</form></center><br></div>
<div class="col-lg-offset-2 col-lg-8">
<img src="{{ url_for('video_capture') }}" width="100%">
</div></div>
</body>
</html>

stop.html

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<title>Dharmaraj - Face Recognition</title>
<style>
h2
{
padding-bottom:20px;
font-weight: 600;
font-size: 3.2em
}
</style>
<body>
<div class="container">
<center><h2>Face Recognition</h2></center>
<div class="col-lg-offset-2 col-lg-8">
<center><form class="form-inline" action = "/stop" method = "post" enctype="multipart/form-data">
<input type = "submit" class="btn btn-danger btn-md btn-block" value="Stop">
</form></center>
<center><form class="form-inline" action = "/start" method = "post" enctype="multipart/form-data">
<input type = "submit" class="btn btn-success btn-md btn-block" value="Start">
</form></center><br>
</div></div>
</body>
</html>

datacollection.py

This file is used to collect data from the camera. Run this code and enter person id example 1 after that camera window will be open and take 100 photos of your face and store in the dataset folder.

import cv2
import os
cam = cv2.VideoCapture(0)
face_detector = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
face_id = input('enter user id end press <return> ==> ')
print("Initializing face capture. Look the camera and wait ...")
count = 0
while(True):
ret, img = cam.read()
img = cv2.flip(img, 1) # 1 stright, 0 reverse
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
count += 1
cv2.imwrite("datasets/User." + str(face_id) + '.' +
str(count) + ".jpg", gray[y:y+h,x:x+w])
cv2.imshow('image', img)
k = cv2.waitKey(100) & 0xff
if k == 27:
break
elif count >= 100: # Take 100 face sample and stop video
break
cam.release()
cv2.destroyAllWindows()

training.py

This file is used to train your face dataset and weight will be stored in ‘trainer.yaml’ file.

import cv2
import numpy as np
from PIL import Image
import os
path = 'datasets'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("Cascades/haarcascade_frontalface_default.xml");
# function to get the images and label data
def getImagesAndLabels(path):
imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
face_Samples=[]
ids = []
for imagePath in imagePaths:
PIL_img = Image.open(imagePath).convert('L') # grayscale
img_numpy = np.array(PIL_img,'uint8')
id = int(os.path.split(imagePath)[1].split(".")[1])
faces = detector.detectMultiScale(img_numpy)
for (x,y,w,h) in faces:
face_Samples.append(img_numpy[y:y+h,x:x+w])
ids.append(id)
return face_Samples,ids

print ("Training faces. It will take a few seconds. Wait ......")
faces,ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
recognizer.write('trainer.yml')
print("{0} faces trained.".format(len(np.unique(ids))))

app.py

from flask import Flask, render_template, Response
import cv2
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer.yml')
cascadePath = "Cascades/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
font = cv2.FONT_HERSHEY_SIMPLEX
id = 0
names = ['None','Dharmaraj','Vikram'] #
app=Flask(__name__)

def capture_by_frames():
global cam
cam = cv2.VideoCapture(0)
while True:
ret, img =cam.read()
img = cv2.flip(img, 1) # 1 Stright 0 Reverse
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
detector=cv2.CascadeClassifier(cascadePath)
faces=detector.detectMultiScale(img,1.2,6)
for(x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
if (confidence < 100):
id = names[id]
confidence = " {0}%".format(round(100 - confidence))
else:
id = "Unknown"
confidence = " {0}%".format(round(100 - confidence))
cv2.putText(img,str(id),(x+5,y-5),font,1,(255,255,255),2)
#cv2.putText(img,str(confidence),(x+5,y+h),font,1,(255,255,0),1)
ret1, buffer = cv2.imencode('.jpg', img)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route('/')
def index():
return render_template('index.html')

@app.route('/start',methods=['POST'])
def start():
return render_template('index.html')

@app.route('/stop',methods=['POST'])
def stop():
if cam.isOpened():
cam.release()
return render_template('stop.html')

@app.route('/video_capture')
def video_capture():
return Response(capture_by_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__=='__main__':
app.run(debug=True,use_reloader=False, port=8000)

Result

Once you run this code you will get the below screen.

Copy this URL http://127.0.0.1:8000/ and past it in your browser to get the result.

Full source code in GitHub: https://github.com/DharmarajPi/OpenCV-Face-Recognition-Deployment-In-Flask-Web-Framework

Have doubts? Need help? Contact me!

LinkedIn: https://www.linkedin.com/in/dharmaraj-d-1b707898

GitHub: https://github.com/DharmarajPi

--

--

Dharmaraj

I have worked on projects that involved Machine Learning, Deep Learning, Computer Vision, and AWS. https://www.linkedin.com/in/dharmaraj-d-1b707898/