diabolic6045 commited on
Commit
3ec44d0
1 Parent(s): c5456a2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow.keras as keras
3
+ from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.preprocessing import image
5
+ import numpy as np
6
+ import random
7
+
8
+ model = load_model('model.h5')
9
+
10
+ # Define class labels
11
+ class_labels = ['Ahmedabad', 'Delhi', 'Kerala', 'Kolkata', 'Mumbai']
12
+
13
+ # Set the threshold for minimum accuracy
14
+ threshold = 0.3
15
+
16
+
17
+ # Create a function to process the uploaded image
18
+ def process_image(uploaded_image):
19
+ # Load and preprocess the input image
20
+ img = image.load_img(uploaded_image, target_size=(175, 175)) #150 for my model
21
+ img = image.img_to_array(img)
22
+ img = np.expand_dims(img, axis=0)
23
+ img = img / 255.0
24
+
25
+ # Make predictions on the input image
26
+ predictions = model.predict(img)
27
+
28
+ # Get the predicted class label and accuracy
29
+ predicted_class_index = np.argmax(predictions)
30
+ predicted_class_label = class_labels[predicted_class_index]
31
+ accuracy = predictions[0][predicted_class_index]
32
+
33
+ # Check if accuracy is below the threshold for all classes
34
+ if all(accuracy < threshold for accuracy in predictions[0]):
35
+ return "This location is not in our database."
36
+ else:
37
+ output = f"<span style='font-size: 24px; color: {random.choice(['#FF9800', '#FF5722', '#673AB7', '#009688'])};'>Predicted class: <strong>{predicted_class_label}</strong></span>"
38
+ acc = f"<span style='font-size: 24px; color: {random.choice(['#FF9800', '#FF5722', '#673AB7', '#009688'])};'>Accuracy: <strong>{accuracy*100:.02f}%</strong></span>"
39
+ return output + "<br>" + acc
40
+
41
+
42
+ # Set Streamlit app title
43
+ st.title("Location Classification")
44
+
45
+ # Add a file uploader to the app
46
+ uploaded_image = st.file_uploader("Upload an image (JPG or JPEG format)", type=["jpg", "jpeg"])
47
+
48
+ # Process the uploaded image and display the result
49
+ if uploaded_image is not None:
50
+ st.write("Uploaded image:")
51
+ st.image(uploaded_image, use_column_width=True)
52
+
53
+ # Convert the uploaded image to a file path
54
+ image_path = "./uploaded_image.jpg"
55
+ with open(image_path, "wb") as f:
56
+ f.write(uploaded_image.getvalue())
57
+
58
+ # Process the image and display the result
59
+ result = process_image(image_path)
60
+ st.markdown(result, unsafe_allow_html=True)