Flask Web Application to Classify Image using VGG16

Nowadays it is comparatively easy to make an image classifier since we have been enriched with powerful tools like Keras, TensorFlow, PyTorch, etc. Like others, Keras provides significantly top-performing models which can be implemented without much effort.

In this article, I will describe building a Web Application for classification using VGG16 model from Keras and Flask — a python web framework.

VGG 16

Here, I will use VGG16. It is a transfer learning model. It achieved 92.7% top-5 test accuracy in ImageNet. I recommend this article to read. It shows the fundamental idea of VGG16.

Image source: neurohive.io

ImageNet is a large dataset of 15 million labeled high-resolution images belonging to roughly 22,000 categories.

Flask

Flask is a lightweight framework for building web applications in python.

Required Packages

keras, numpy, tensorflow, pillow and flask.

Let's Build!

At first, install virtualenv which is going to be responsible for managing project’s packages. It intends to make an isolated Python environment.

pip install virtualenv

Create a folder named keras-imagenet (or what you want), inside this folder run

virtualenv venv

A folder will be generated named ‘venv’, which contains all the necessary executables to use the packages that our project would need. Activate the virtual environment by

source venv/bin/activate

Install the required packages.

Now create a file app.py which will have the basic configuration of our application.

app.py

You should update the UPLOAD_FOLDER variable according to your “uploads” folder path. It is time to make the main python script (I name it index.py) which is going to render image uploading form and pushing the image to the classifier model.

index.py

Don’t forget to create a templates folder and put an Html file in it. Write down index.html as below. This is our dirty image upload form. :P

Till now we build our web part. Now jump into the classifier part. As I said earlier, I am going to use VGG16 of Keras. Our classifier is going to be very simple but it will work perfectly. So, make a file named main.py, write the below codes.

main.py

getPreiction function will get an image and let VGG16 transfer learning model predict the image. This function will return the label and accuracy (%) respectively.

Great! Our super-duper app is ready to GO!. Now it’s time to test. To run the flask app, hit the below commands in your terminal

export FLASK_APP=index.pyflask run

By default, the app will be live on port 5000.

Our wonderful form

Let our model predict what this thing is called.

Test Image

During the first attempt, it will take more time. After a while, the prediction result will be visible.

Label: African_elephant Accuracy: 62.42102384567261

So that’s it. Don’t forget to press 👏 as much as you can so that others can reach it. Make sure to follow my Medium and LinkedIn profile, to get the latest updates of mine.

Happy Learning

Software Engineer | Professional Scrum Master | Research Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store