Spaces:
Runtime error
Runtime error
okeowo1014
commited on
Commit
•
14e8902
1
Parent(s):
a7b6739
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,58 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import numpy as np
|
3 |
from tensorflow.keras.preprocessing import image
|
4 |
-
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
# Load the model (outside the main app function for efficiency)
|
11 |
-
model = load_model(model_path)
|
12 |
|
13 |
def main():
|
14 |
-
|
15 |
-
|
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 |
-
|
|
|
|
|
|
|
55 |
|
|
|
|
|
|
|
|
|
56 |
# if __name__ == '__main__':
|
57 |
-
#
|
|
|
1 |
import streamlit as st
|
2 |
+
import cv2
|
3 |
import numpy as np
|
4 |
from tensorflow.keras.preprocessing import image
|
5 |
+
import tensorflow as tf
|
6 |
|
7 |
+
# Load the saved model (replace with your model filename)
|
8 |
+
model = tf.keras.models.load_model('cat_dog_classifier.keras')
|
9 |
+
|
10 |
+
# Image dimensions for the model
|
11 |
+
img_width, img_height = 224, 224
|
12 |
+
|
13 |
+
|
14 |
+
def preprocess_image(img):
|
15 |
+
"""Preprocesses an image for prediction."""
|
16 |
+
img = cv2.resize(img, (img_width, img_height))
|
17 |
+
img = img.astype('float32') / 255.0
|
18 |
+
img = np.expand_dims(img, axis=0)
|
19 |
+
return img
|
20 |
+
|
21 |
+
|
22 |
+
def predict_class(image):
|
23 |
+
"""Predicts image class and probabilities."""
|
24 |
+
preprocessed_img = preprocess_image(image)
|
25 |
+
prediction = model.predict(preprocessed_img)
|
26 |
+
class_names = ['cat', 'dog'] # Adjust class names according to your model
|
27 |
+
return class_names[np.argmax(prediction)], np.max(prediction)
|
28 |
+
|
29 |
+
|
30 |
+
def display_results(class_name, probability):
|
31 |
+
"""Displays prediction results in a progress bar style."""
|
32 |
+
st.write(f"**Predicted Class:** {class_name}")
|
33 |
+
|
34 |
+
# Create a progress bar using st.progress
|
35 |
+
progress = st.progress(0)
|
36 |
+
for i in range(100):
|
37 |
+
progress.progress(i + 1)
|
38 |
+
if i == int(probability * 100):
|
39 |
+
break
|
40 |
+
st.write(f"**Probability:** {probability:.2f}")
|
41 |
|
|
|
|
|
42 |
|
43 |
def main():
|
44 |
+
"""Main app function."""
|
45 |
+
st.title("Image Classifier")
|
46 |
+
st.write("Upload an image to classify it as cat or dog.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
|
49 |
+
if uploaded_file is not None:
|
50 |
+
image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), cv2.IMREAD_COLOR)
|
51 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
52 |
|
53 |
+
predicted_class, probability = predict_class(image)
|
54 |
+
display_results(predicted_class, probability)
|
55 |
+
|
56 |
+
main()
|
57 |
# if __name__ == '__main__':
|
58 |
+
# main()
|