Image Classification of Uploaded Files Using Streamlit’s Killer New Feature

From: https://towardsdatascience.com/image-classification-of-uploaded-files-using-streamlits-killer-new-feature-7dd6aa35fe0

It’s no secret I’m a massive fan of Streamlit. I think it’s the best way to share early versions of Data Science projects. It’s especially great for demoing your project to a less technical audience. Before investing in frontend developers to build you a proper UI, Streamlit allows you to get some features out in the wild for users to test.

A commonly requested feature that has been left out for far too long is the file uploader. You can use the uploader for basically any file. Probably best to only use this feature with users you can trust. Be careful about the files you let users upload if you’re going to use this feature externally.

As someone who spends massive amounts of time on Computer Vision problems, I’m constantly looking for ways to best get users testing and interacting with solutions for Object Detection, Classification, Anomaly Detection, and much more…

Implementation

Here’s the final product, explained in code below…

I think you’ll find the code strikingly simple.

For the UI:

UI — Full Code: LINK

uploaded_file = st.file_uploader("text here", type="file_type")

The file uploader can be very simply implemented in one line. You can then pass uploaded_file as needed. For an image it might be something like this to display the image:

# Using PIL
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image.')

For a CSV you could use pandas very simply like:

dataframe = pd.read_csv(uploaded_file) 

As, you can see, there’s a wide class of problems you can approach with file_uploader().

For inference using pre-trained VGG16:

Inference — Full Code: LINK

This should be pretty self-explanatory, but for the uninitiated:

# Use pre-trained models
# Other pre-trained: https://keras.io/applications/model = VGG16()

We then manipulate our image:

# Load image, resize image, make array and reshape
image = load_img(image_path, target_size=(224, 224))
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
image = preprocess_input(image)

We are now ready for inference:

yhat = model.predict(image)

And finally we return the results:

label = decode_predictions(yhat)    
# return highest probability
label = label[0][0]
return label

Closing

You can find the full code on Github. Here’s the final result:

Here’s the docs if you want to skip straight to your own implementation: https://streamlit.io/docs/api.html#streamlit.file_uploader

2 thoughts on “Image Classification of Uploaded Files Using Streamlit’s Killer New Feature

  1. Also, in case you are struggling with rendering Jupyter Notebook files here on the WordPress platform, I figured a hack around it that works as of right now. Take a look at some of my recent blog posts (http://realdevtalk.com), and you’ll notice that the notebooks show up as normal Jupyter Notebooks. It took a while to figure out, but I’m glad I was able to get it done. If you’re interested, let me know and I can help you set it up for yourself as well!

    Like

Leave a comment