Overview of Deploying Machine Learning Models

Read it in 5 Mins

Last updated on
30th Apr, 2021
Published
30th Apr, 2021
Views
3,335
Overview of Deploying Machine Learning Models

Machine Learning is no longer just the latest buzzword. In fact, it has permeated every facet of our everyday lives. Most of the applications across the world are built using Machine Learning and their applications extend further when they are combined with other cutting-edge technologies like Deep Learning and Artificial Intelligence. These latest technologies are a boon to mankind, as they simplify tasks, helping to complete work in lesser time. They boost the growth and profitability of industries and organizations across sectors, which in turn helps in the growth of the economy and generates jobs.

What are the fields that machine learning extends into?

Machine Learning now finds applications across sectors and industries including fields like Healthcare, defense, insurance, government sectors, automobile, manufacturing, retail and more. ML gives great insights to businesses in gaining and retaining customer loyalty, enhances efficiency, minimizes the time consumption, optimizes resource allocation and decreases the cost of labor for a specific task.

What is Model Deployment?

It’s well established that ML has a lot of applications in the real world. But how exactly do these models work to solve our problems? And how can it be made available for a large user base? The answer is that we have to deploy the trained machine learning model into the web, so that it can be available for many users.

When a model is deployed, it is fully equipped with training and it knows what are the inputs to be taken by the model and what are the outputs given out in return. This strategy is used to advantage in real world applications. Deployment is a tricky task and is the last stage of our ML project.

Generally, the deployment will take place on a web server or a cloud for further use, and we can modify the content based on the user requirements and update the model. Deployment makes it easier to interact with the applications and share the benefits to the applications with others.

With the process of Model Deployment, we can overcome problems like Portability, which means shifting of software from one machine to the other and Scalability, which is the capacity to be changed on a scale and the ability of the computation process to be used in a wide range of capabilities.

Installing Flask on your Machine

Flask is a web application framework in Python. It is a lightweight Web Server Gateway Interface (WSGI) web framework. It consists of many modules, and contains different types of tools and libraries which helps a web developer to write and implement many useful web applications.

Installing Flask on our machine is simple. But before that, please ensure you have installed Python in your system because Flask runs using Python.

In Windows: Open command prompt and write the following code:

a) Initially make the virtual environment using pip -- pip install virtualenv And then write mkvirtualenv HelloWorld

b) Connect to the project – Create a folder dev, then mkdir Helloworld for creating a directory; then type in cd HelloWorld to go the file location.
c) Set Project Directory – Use setprojectdir in order to connect our virtual environment to the current working directory. Now further when we activate the environment, we will directly move into this directory.

d) Deactivate – On using the command called deactivate, the virtual environment of hello world present in parenthesis will disappear, and we can activate our process directly in later steps.

e) Workon – When we have some work to do with the project, we write the command  “workon HelloWorld” to activate the virtual environment directly in the command prompt.

The above is the set of Virtual Environment commands for running our programs in Flask. This virtual environment helps and makes the work easier as it doesn’t disturb the normal environment of the system. The actions we perform will reside in the created virtual environment and facilitate the users with better features.

f) Flask Installation – Now you install flask on the virtual environment using command pip install flask

Understanding the Problem Statement

For example, let us try a Face Recognition problem using opencv. Here, we work on haarcascades dataset. Our goal is to detect the eyes and face using opencv. We have an xml file that contains the values of face and eyes that were stored. This xml file will help us to identify the face and eyes when we look into the camera.

The xml data for face recognition is available online, and we can try this project on our own after reading this blog. For every problem that we solve using Machine Learning, we require a dataset, which is the basic building block for the Model development in ML. 

You can generate interesting outcomes at the end like detecting the face and eyes with a bounding rectangular box. Machine learning beginners can use these examples and create a mini project which will help them to know much about the core of ML and other technologies associated with it.

Workflow of the Project

  • Model Building: We build a Machine Learning model to detect the face of the human present in front of the camera. We use the technology of Opencv to perform this action which is the library of Computer Vision.
    Here our focus is to understand how the model is working and how it is deployed on server using Flask. Accuracy is not the main objective, but we will learn how the developed ML model is deployed.
  • Face app: We will create a face app that detects your face and implements the model application. This establishes the connection between Python script and the webpage template.
  • Camera.py: This is the Python script file where we import the necessary libraries and datasets required for our model and we write the actual logic for the model to exhibit its functionality.
  • Webpage Template: Here, we will design a user interface where the user can experience live detection of his face and eyes in the camera. We provide a button on a webpage, to experience the results.
  • Getting the output screen: when the user clicks the button, the camera will open directly and we can get the result of the machine learning model deployed on the server. In the output screen you can see your face. Storage: This section is totally optional for users, and it is based on the users’ choice of storing and maintaining the data. After getting the outputs on the webpage screen, you can store the outputs in a folder on your computer. This helps us to see how the images are captured and stored locally in our system. You can add a file path in the code, that can store the images locally on your system if necessary.

This application can be further extended to a major project of “Attendance taking using Face Recognition Technique”, which can be used in colleges and schools, and can potentially replace normal handwritten Attendance logs. This is an example of a smart application that can be used to make our work simple.

Diagrammatic Representation of the steps for the project
Building our Machine Learning Model

We have the XML data for recognizing face and eyes respectively. Now we will write the machine learning code, that implements the technique of face and eyes detection using opencv. 

Before that, we import some necessary libraries required for our project, in the file named camera.py 

# import cv2
# import numpy as np
# import scipy.ndimage
# import pyzbar.pyzbar as pyzbar
# from PIL import Image
 

Now, we load the dataset into some variables in order to access them further. Haarcascades is the file name where all the xml files containing the values of face, eye, nose etc are stored. 

# defining face detector
# face_cascade = cv2.CascadeClassifier("haarcascades/haarcascade_frontalface_default.xml")
# eye_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_eye.xml')

The xml data required for our project is represented as shown below, and mostly consists of numbers.

Now we write the code for opening the camera, and releasing of camera in a class file. The “def” keyword is the name of the function in Python. The functions in Python are declared using the keyword “def”.

The function named “def __init__” initiates the task of opening camera for live streaming of the video. The “def __del__” function closes the camera upon termination of the window.

# class VideoCamera(object):
#    def __init__(self):
        # capturing video
#       self.video = cv2.VideoCapture(0)
#  def __del__(self):
#        # releasing camera
#        self.video.release()

Next, we build up the actual logic for face and eyes detect using opencv in Python script as follows. This function is a part of class named videocamera.

# class VideoCamera(object):
#    def __init__(self):
#        # capturing video
#        self.video = cv2.VideoCapture(0)

#    def __del__(self):
#        # releasing camera
#        self.video.release()

#    def face_eyes_detect(self):
#        ret, frame = self.video.read()
#        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
#        c=0
#        for (x,y,w,h) in faces:
#            cv2.rectangle(frame, (x,y), (x+w,y+h), (255, 0, 0), 2)
#            roi_gray = gray[y:y+h, x:x+w]
#            roi_color = frame[y:y+h, x:x+w]

#            eyes = eye_cascade.detectMultiScale(roi_gray)
#            for (ex,ey,ew,eh) in eyes:
#                cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)

#            while True:
#                k = cv2.waitKey(1000) & 0xFF
#                print("Image "+str(c)+" saved")
#                file = 'C:/Users/user/dev/HelloWorld/images/'+str(c)+'.jpg'
#                cv2.imwrite(file, frame)
#                c += 1    

        # encode Opencv raw frame to jpg and display it
#        ret, jpeg = cv2.imencode('.jpg', frame)
#        return jpeg.tobytes()

  1. The first line in the function “ret, frame” reads the data of live streaming video. The ret takes the value “1”, when the camera is open, else it takes “0” as input. The frame captures the live streaming video from time to time. 
  2. In the 2nd line, we are changing the color of image from RGB to Grayscale, i.e., we are changing the values of pixels. And then we are applying some inbuilt functions to detect faces. 
  3. The for loop, illustrates that it is having some fixed dimensions to draw a bounding rectangular box around the face and eyes, when it is detected. 
  4. If you want to store the captured images after detecting face and eyes, we can add the code of while loop, and we can give the location to store the captured images. When an image is captured, it is saved as Image 1, Image 2 saved, etc., for confirmation.
    All the images will be saved in jpg format. We can mention the type of format in which the images should be stored. The method named cv2.imwrite stores the frame in a particular file location.
  5. Finally, after capturing the detected picture of face and eyes, it displays the result at the user end. 

Creating a Webpage

We will create a webpage, in order to implement the functionality of the developed machine learning model after deployment using Flask. Here is the design of our webpage.

The above picture represents a small webpage demonstrating “Video Streaming Demonstration” and a link “face-eyes-detect”. When we click the button on the screen, the camera gets opened and the functionality will be displayed to the users who are facing the camera.

The code for creating a webpage is as follows:

<!---
<html>
  <head>
    <title>Harsha Rocks</title>
    <style>
img {
text-align: center;
display: block;
margin : auto;
display : grid;
position : fixed;
display : flex;
}
    </style>
  </head>
  <body>
    <h1>Video Streaming Demonstration</h1>
<a href="{{ url_for('video_feed') }}" align="center">face-eyes-detect</a>
  </body>
</html> --->

If the project contains only one single html file, it should be necessarily saved with the name of index. 

The above code should be saved as “index.html” in a folder named “templates” in the project folder named “HelloWorld”, that we have created in the virtual environment earlier. This is the actual format we need to follow while designing a webpage using Flask framework.

Connecting Webpage to our Model

Till now we have developed two separate files, one for developing the machine learning model for the problem statement and the other for creating a webpage, where we can access the functionality of the model. Now we will try to see how we can connect both of them.

This is the Python script with the file name saved as “app.py”. Initially we import the necessary libraries to it, and create a variable that stores the Flask app. We then guide the code to which location it needs to be redirected, when the Python scripts are executed in our system. The redirection is done through “@app.route” followed by a function named “home”

Then we include the functionality of model named “face_eyes_detect” to the camera followed by the function definition named “gen”

After adding the functionality, we display the response of the deployed model on to the web browser. The outcome of the functionality is the detection of face and eyes in the live streaming camera and the frames are stored in the folder named images. We put the debug mode to False. # from flask import Flask, render_template, Response,url_for, redirect, request.

# from flask import Flask, render_template, Response,url_for, redirect, request  
# from camera import VideoCamera  
# import cv2  
# import time  
# app = Flask(__name__)  
# @app.route("/")  
# def home():  
#     # rendering web page  
#     return render_template('index.html')  
# def gen(camera):  
#     while True:  
#         # get camera frame  
#         frame = camera.face_eyes_detect()  
#         yield(b'--frame\r\n'  
#                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')  
# @app.route("/video_feed")  
# def video_feed():  
#     return Response(gen(VideoCamera()),  
#           mimetype='multipart/x-mixed-replace; boundary=frame')  
# if __name__ == '__main__':  
#     # defining server ip address and port  
#     app.run(debug=False)

Before running the Python scripts, we need to install the libraries like opencv, flask, scipy, numpy, PIL, pyzbar etc., using the command prompt with the command named “pip install library_name” like “pip install opencv-python”, ”pip install flask”, “pip install scipy” etc.

  • When you have installed all the libraries in your system, now open the python script “app.py” and run it using the command “f5”. The output is as follows:

Image: Output obtained when we run app.py file

  • Now we need to copy the server address http://127.0.0.1:5000/ and paste it on the web browser, and we will get the output screen as follows:

  • Now when we click the link “face-eyes-detect”, we will get the functionality of detecting the face and eyes of a user, and it is seen as follows:

Without Spectacles

With Spectacles

One eye closed by hand

one eye closed

  • When these detected frames are generated, they are similarly stored in a specified location of folder named “images”. And in the Python shell we can observe, the sequence of images is saved in the folder, and looks as follows:

In the above format, we get the outcomes of images stored in our folder.

Now we will see how the images were stored in the previously created folder named “images” present in the project folder of “HelloWorld.”

Now we can use the deployed model in real time. With the help of this application, we can try some other new applications of Opencv and we can deploy it in the flask server accordingly.  

You can find all the above code with the files in the following github repository, and you can make further changes to extend this project application to some other level.

Github Link.

Conclusion

In this blog, we learnt how to deploy a model using flask server and how to connect the Machine Learning Model with the Webpage using Flask. The example project of face-eyes detection using opencv is a pretty common application in the present world. Deployment using flask is easy and simple.  

We can use the Flask Framework for deployment of ML models as it is a light weight framework. In the real-world scenario, Flask may not be the most suitable framework for bigger applications as it is a minimalist framework and works well only for lighter applications.

Profile

KnowledgeHut

Author
KnowledgeHut is an outcome-focused global ed-tech company. We help organizations and professionals unlock excellence through skills development. We offer training solutions under the people and process, data science, full-stack development, cybersecurity, future technologies and digital transformation verticals.