Image Classifier with Streamlit

Let’s build deep learning models in simple lines of code using streamlit.

Pooja Tambe
Geek Culture
3 min readAug 11, 2022

--

Photo by Lee Campbell on Unsplash

Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom web apps for machine learning and data science. We will see with a few lines how to build a simple image classifier.

Streamlit Features

  • Simple API for building apps in a few lines of code.
  • Automatically updates as you save the source file.
  • No need to write a backend, connect a frontend, define routes, handle HTTP requests, and write HTML, CSS, and JavaScript.
  • Adding a widget is the same as declaring a variable.

Installation

First, install streamlit in the python environment using the following command.

Getting Started

For this demo, I will be using a pre-trained VGG19 model for image classification. This classifier will classify images of categories Sea, Glaciers, Mountain, Forest, Street, and Buildings. Check the Github repository to build a simple image classifier using transfer learning and train it.

Initially, we will create a python file and write the following code in it.

The python file is executed using the following command.

We will get output on our browser.

Fig-1

Streamlit provides different text formats such as title, header, subheader, and caption. In this case, markdown is used.

We can set the background image to our web app to add visual effects.

In streamlit, we need to save the python file as we make changes to it. Streamlit recognizes changes in the source file and asks if we want to rerun it. Also, we can select ‘Always Rerun’ to reflect changes automatically. After adding background, our application will look like this:

Fig-2

First, we need to upload the image to our image classifier. That uploaded image is pre-processed before feeding to a classifier.

Our classifier needs input. The image of specified types (‘png’ and ‘jpg’ in this case) are fed to the classifier using st.file_uploader widget. The input images are processed in the format required for the model. We will use st.columns() function that allows inserting elements in containers laid out side-by-side.

In the end, we will load our model, apply pre-processed image, generate output and write it to a container.

When we save the python file, these changes will reflect on the browser:

FIg-3

Streamlit lets you create a web app with few lines of code. I hope this demo will help you to create your app with streamlit.

References

  1. https://docs.streamlit.io/

Happy Coding!!

--

--