Spaces:
Runtime error
Runtime error
okeowo1014
commited on
Commit
•
a7b6739
1
Parent(s):
e1ede78
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,57 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.preprocessing import image
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
|
6 |
+
# Define constants
|
7 |
+
IMG_WIDTH, IMG_HEIGHT = 224, 224 # Adjust if your model requires different dimensions
|
8 |
+
model_path = 'dog_cat_classifier_model.keras' # Replace with your model path
|
9 |
+
|
10 |
+
# Load the model (outside the main app function for efficiency)
|
11 |
+
model = load_model(model_path)
|
12 |
+
|
13 |
+
def main():
|
14 |
+
"""
|
15 |
+
Streamlit app for image classification with user-friendly interface.
|
16 |
+
"""
|
17 |
+
|
18 |
+
# Title and description
|
19 |
+
st.title("Intriguing Image Classifier")
|
20 |
+
st.write("Upload an image and discover its most likely category along with probabilities in a compelling way!")
|
21 |
+
|
22 |
+
# File uploader and sidebar for image selection
|
23 |
+
uploaded_file = st.file_uploader("Choose an Image", type=['jpg', 'jpeg', 'png'])
|
24 |
+
image_selected = st.sidebar.selectbox("Select Image", (None, "Uploaded Image"))
|
25 |
+
|
26 |
+
if uploaded_file is not None:
|
27 |
+
image_display = image.load_img(uploaded_file, target_size=(IMG_WIDTH, IMG_HEIGHT))
|
28 |
+
st.image(image_display, caption="Uploaded Image", use_column_width=True)
|
29 |
+
image_selected = "Uploaded Image"
|
30 |
+
|
31 |
+
# Preprocess image if one is selected
|
32 |
+
if image_selected:
|
33 |
+
img_array = image.img_to_array(image_display)
|
34 |
+
img_array = np.expand_dims(img_array, axis=0)
|
35 |
+
img_array /= 255.0 # Rescale pixel values to [0, 1]
|
36 |
+
|
37 |
+
# Make predictions and get class labels (assuming your model outputs probabilities)
|
38 |
+
predictions = model.predict(img_array)
|
39 |
+
class_labels = [f"{label}: {prob:.2%}" for label, prob in zip(get_class_labels(model), predictions[0])]
|
40 |
+
|
41 |
+
# Display predictions in an intriguing way (replace with your preferred method)
|
42 |
+
st.header("Predictions:")
|
43 |
+
progress_bar_width = 800 # Adjust for desired visual style
|
44 |
+
|
45 |
+
for label, prob in zip(class_labels, predictions[0]):
|
46 |
+
progress_bar = st.progress(label)
|
47 |
+
progress_bar.progress(int(prob * 100)) # Update progress bar based on probability
|
48 |
+
|
49 |
+
# Function to retrieve class labels from the model (replace if your model structure is different)
|
50 |
+
def get_class_labels(model):
|
51 |
+
class_names = list(model.class_names) # Assuming class names are directly accessible
|
52 |
+
return class_names
|
53 |
+
|
54 |
+
main()
|
55 |
+
|
56 |
+
# if __name__ == '__main__':
|
57 |
+
# main()
|