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') # Define class labels class_labels = ['Ahmedabad', 'Delhi', 'Kerala', 'Kolkata', 'Mumbai'] # Set the threshold for minimum accuracy threshold = 0.3 # Create a function to process the uploaded image def process_image(uploaded_image): # Load and preprocess the input image img = image.load_img(uploaded_image, target_size=(175, 175)) #150 for my model img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = img / 255.0 # Make predictions on the input image predictions = model.predict(img) # Get the predicted class label and accuracy predicted_class_index = np.argmax(predictions) predicted_class_label = class_labels[predicted_class_index] accuracy = predictions[0][predicted_class_index] # Check if accuracy is below the threshold for all classes if all(accuracy < threshold for accuracy in predictions[0]): return "This location is not in our database." else: output = f"Predicted class: {predicted_class_label}" acc = f"Accuracy: {accuracy*100:.02f}%" return output + "
" + acc # Set Streamlit app title st.title("Location Classification") # Add a file uploader to the app uploaded_image = st.file_uploader("Upload an image (JPG or JPEG format)", type=["jpg", "jpeg"]) # Process the uploaded image and display the result if uploaded_image is not None: st.write("Uploaded image:") st.image(uploaded_image, use_column_width=True) # Convert the uploaded image to a file path image_path = "./uploaded_image.jpg" with open(image_path, "wb") as f: f.write(uploaded_image.getvalue()) # Process the image and display the result result = process_image(image_path) st.markdown(result, unsafe_allow_html=True)