|
import streamlit as st |
|
import tensorflow.keras as keras |
|
from tensorflow.keras.models import load_model |
|
from tensorflow.keras.preprocessing import image |
|
import numpy as np |
|
import random |
|
|
|
model = load_model('model.h5') |
|
|
|
|
|
class_labels = ['Ahmedabad', 'Delhi', 'Kerala', 'Kolkata', 'Mumbai'] |
|
|
|
|
|
threshold = 0.3 |
|
|
|
|
|
|
|
def process_image(uploaded_image): |
|
|
|
img = image.load_img(uploaded_image, target_size=(175, 175)) |
|
img = image.img_to_array(img) |
|
img = np.expand_dims(img, axis=0) |
|
img = img / 255.0 |
|
|
|
|
|
predictions = model.predict(img) |
|
|
|
|
|
predicted_class_index = np.argmax(predictions) |
|
predicted_class_label = class_labels[predicted_class_index] |
|
accuracy = predictions[0][predicted_class_index] |
|
|
|
|
|
if all(accuracy < threshold for accuracy in predictions[0]): |
|
return "This location is not in our database." |
|
else: |
|
output = f"<span style='font-size: 24px; color: {random.choice(['#FF9800', '#FF5722', '#673AB7', '#009688'])};'>Predicted class: <strong>{predicted_class_label}</strong></span>" |
|
acc = f"<span style='font-size: 24px; color: {random.choice(['#FF9800', '#FF5722', '#673AB7', '#009688'])};'>Accuracy: <strong>{accuracy*100:.02f}%</strong></span>" |
|
return output + "<br>" + acc |
|
|
|
|
|
|
|
st.title("Location Classification") |
|
|
|
|
|
uploaded_image = st.file_uploader("Upload an image (JPG or JPEG format)", type=["jpg", "jpeg"]) |
|
|
|
|
|
if uploaded_image is not None: |
|
st.write("Uploaded image:") |
|
st.image(uploaded_image, use_column_width=True) |
|
|
|
|
|
image_path = "./uploaded_image.jpg" |
|
with open(image_path, "wb") as f: |
|
f.write(uploaded_image.getvalue()) |
|
|
|
|
|
result = process_image(image_path) |
|
st.markdown(result, unsafe_allow_html=True) |
|
|