|
import streamlit as st |
|
from tensorflow.keras.applications.resnet50 import ResNet50 |
|
from tensorflow.keras.preprocessing import image |
|
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions |
|
import numpy as np |
|
|
|
st.title("Image Classification with ResNet50 :baby:") |
|
|
|
uploaded_file = st.file_uploader("Upload an image on a object,animal,plant etc.", type=["jpg", "jpeg","png"]) |
|
|
|
if uploaded_file is not None: |
|
img = image.load_img(uploaded_file, target_size=(224, 224)) |
|
img = image.img_to_array(img) |
|
img = np.expand_dims(img, axis=0) |
|
img = preprocess_input(img) |
|
|
|
model = ResNet50(weights='imagenet') |
|
pred = model.predict(img) |
|
decoded_pred = decode_predictions(pred, top=3)[0] |
|
|
|
st.image(uploaded_file, caption='Uploaded Image', use_column_width=True) |
|
|
|
sentence = "This image is " |
|
for i, (code, name, probability) in enumerate(decoded_pred): |
|
if i == 0: |
|
top_name = name.lower() |
|
sentence += f"{probability * 100:.2f}% a {top_name}" |
|
else: |
|
sentence += f", {probability * 100:.2f}% a {name.lower()}" |
|
sentence += "." |
|
st.markdown(f"<h1>{top_name.upper()}</h1>", unsafe_allow_html=True) |
|
st.write(sentence) |