Reader small image

You're reading from  Mastering OpenCV 4 with Python

Product typeBook
Published inMar 2019
Reading LevelExpert
PublisherPackt
ISBN-139781789344912
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Alberto Fernández Villán
Alberto Fernández Villán
author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán

Right arrow

Mobile and Web Computer Vision with Python and OpenCV

Web computing is an interesting topic because it allows us to leverage cloud computing. In this sense, there are a lot of Python web frameworks that can be used to deploy your applications. These frameworks provide a collection of packages, allowing the developers to focus on the core logic of the application rather than having to handle low-level details (for example, protocols, sockets, or process and thread management, among others).

In this chapter, we are going to use Flask, which is a small and powerful Python web framework available under the BSD license, in order to build computer vision and deep learning web applications. Additionally, we are going to see how to deploy our applications to the cloud rather than run them on our computer, by leveraging cloud computing.

The main sections of this chapter are as follows...

Technical requirements

The technical requirements are as follows:

  • Python and OpenCV
  • Python-specific IDE
  • NumPy and Matplotlib packages
  • Git client
  • Flask (see how to install Flask in the next subsection, Installing the packages)
  • Keras (see how to install Keras in the next subsection, Installing the packages)
  • TensorFlow (see how to install TensorFlow in the next subsection, Installing the packages)
  • Requests (see how to install requests in the next subsection, Installing the packages)
  • Pillow (see how to install Pillow in the next subsection, Installing the packages)

Further details about how to install these requirements are present in Chapter 1, Setting Up OpenCV. The GitHub repository for Mastering OpenCV 4 with Python, containing all the supporting project files necessary to work through the book from the first chapter to the last one, can be accessed in this URL: https://github...

Introduction to Python web frameworks

Python web frameworks (https://wiki.python.org/moin/WebFrameworks) provide a collection of packages that allow developers to focus on the core logic of the application rather than having to handle low-level details (for example, protocols, sockets or process, and thread management, among others). Furthermore, these frameworks can be categorized into full-stack and non-full-stack frameworks. Django and Flask are two popular web frameworks for Python, which we will discuss later on in this chapter:

The perfect example of a Full-stack frameworks is Django (https://www.djangoproject.com/), which is a free, open source, full-stack Python framework, trying to include all the necessary features by default, as opposed to offering them as separate libraries. Django makes it easier to create web applications, and requires less time than other frameworks...

Introduction to Flask

Here is Flask's Hello World application which, as we mentioned, contains only a few lines of code. This can be seen in hello.py script, as follows:

# Import required packages:
from flask import Flask

app = Flask(__name__)


@app.route("/")
def hello():
return "Hello World!"


if __name__ == "__main__":
app.run()

After importing the required package, we will create an instance of Flask class, which will be our Web Server Gateway Interface (WSGI) application. The route() decorator is used to indicate what URL should trigger the hello() function, which will print the message Hello World!.

In Flask, you can use the route() decorator to bind a function to a URL.

Execute this script with the following command:

$ python hello.py

You will see this message in your console, telling you that the web server has been started:

 * Serving...

Web computer vision applications using OpenCV and Flask

In this section, we will see how to create web computer vision applications using OpenCV and Flask. We will start with the equivalent Hello world application using OpenCV and Flask.

A minimal example to introduce OpenCV and Flask

Script hello_opencv.py is coded in order to show how you can use OpenCV to perform a very basic web computer vision application. The code of this script is shown next:

# Import required packages:
import cv2
from flask import Flask, request, make_response
import numpy as np
import urllib.request

app = Flask(__name__)


@app.route('/canny', methods=['GET'])
def canny_processing():
# Get the image:
with urllib.request.urlopen(request...

Deep learning API using Keras and Flask

In Chapter 12, Introduction to Deep Learning, we saw how to create deep learning applications using both TensorFlow and Keras. In this section, we will see how to create a deep learning API using both Keras and Flask.

More specifically, we are going to see how to work with pre-trained deep learning architectures included in Keras and, then, we will see how to create a deep learning API using these pre-trained deep learning architectures.

Keras applications

Keras Applications (https://keras.io/applications/, compatible with Python 2.7-3.6 and distributed under the MIT license) is the application module of the Keras deep learning library, providing both deep learning model definitions...

Deploying a Flask application to the cloud

If you have developed a Flask application you can run in your computer, you can easily make it public by deploying it to the cloud. There are a lot of options if you want to deploy your application to the cloud (for example, Google App Engine: https://cloud.google.com/appengine/, Microsoft Azure: https://azure.microsoft.com, Heroku: https://devcenter.heroku.com/, and Amazon Web Services: https://aws.amazon.com, among others). Additionally, you can also use PythonAnywhere (www.pythonanywhere.com), which is a Python online integrated development environment (IDE) and web hosting environment, making it easy to create and run Python programs in the cloud.

PythonAnywhere is very simple, and also the recommended way of hosting machine learning-based web applications. PythonAnywhere provides some interesting features, such as WSGI-based web...

Summary

In this last chapter of the book, we have seen how to create web applications using Python web frameworks and discovered the potential of web frameworks like Flask. More specifically, we have developed several web computer vision and web deep learning applications using OpenCV, Keras, and Flask and learned how to ingrate it with them to provide the web applications machine learning and deep learning capabilities. Additionally, we have also covered how to deploy a Flask application to the cloud using PythonAnywhere, which provides capabilities for web hosting. Finally, we have also seen how to perform requests (for example, GET and POST) from the browser (GET requests) and, also, programmatically (GET and POST requests) to create a web face API using OpenCV and Flask, and to create a deep learning API using OpenCV.

Questions

  • What two main categories of web frameworks exist?
  • What is the purpose of the route() decorator in Flask?
  • How can you run the Flask server application to be accessible from any other computer on the network?
  • What is the purpose of the jsonify() function?
  • What is the purpose of the errorhandler() decorator in Flask?
  • What is Keras Applications?
  • What is PythonAnywhere?

Further reading

The following references will help you dive deeper into Flask (and also Django):

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering OpenCV 4 with Python
Published in: Mar 2019Publisher: PacktISBN-13: 9781789344912
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán