Spaces:
Sleeping
Sleeping
Update app.py
Browse fileswarning removed ~ considering additional UI changes and image presentations
app.py
CHANGED
|
@@ -1,67 +1,42 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import tensorflow as tf
|
| 5 |
import numpy as np
|
| 6 |
-
import
|
| 7 |
-
from
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
image_array = np.array(image) / 255.0 # Normalize the image
|
| 16 |
-
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
| 17 |
-
prediction = model.predict(image_array)
|
| 18 |
-
return "Decorated" if prediction[0][0] > 0.5 else "Undecorated"
|
| 19 |
-
|
| 20 |
-
# UI Setup
|
| 21 |
-
st.set_page_config(page_title="Tree Decoration Prediction", layout="wide")
|
| 22 |
-
st.title("Tree Decoration Prediction App")
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
images = os.listdir(image_folder)
|
| 38 |
-
# Filter out any non-image files
|
| 39 |
-
images = [img for img in images if img.endswith(('.png', '.jpg', '.jpeg'))]
|
| 40 |
-
|
| 41 |
-
# Create a 2-row grid with 5 columns
|
| 42 |
-
num_images = len(images)
|
| 43 |
-
rows = (num_images // 5) + (1 if num_images % 5 else 0) # Calculate the number of rows
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
for j in range(5):
|
| 48 |
-
index = i * 5 + j
|
| 49 |
-
if index < num_images:
|
| 50 |
-
image_path = os.path.join(image_folder, images[index])
|
| 51 |
-
image = Image.open(image_path)
|
| 52 |
-
|
| 53 |
-
# Display image as clickable
|
| 54 |
-
with cols[j]:
|
| 55 |
-
st.image(image, caption=images[index], use_column_width=True)
|
| 56 |
-
# Add hover effect and clickable functionality
|
| 57 |
-
if st.button(f"Predict {images[index]}", key=f"button_{index}"):
|
| 58 |
-
result = predict_image(image)
|
| 59 |
-
st.write(f"Prediction: {result}")
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
import warnings
|
| 8 |
|
| 9 |
+
# Filter warnings (done before loading model and processing)
|
| 10 |
+
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
| 11 |
|
| 12 |
+
# Load your pre-trained Keras model (adjust the path as needed)
|
| 13 |
+
MODEL_PATH = "your_trained_model.keras" # Model file location in Hugging Face Space
|
| 14 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Define the function to predict decoration
|
| 17 |
+
def predict_decoration(image: Image.Image):
|
| 18 |
+
# Preprocess the image to match the model input format
|
| 19 |
+
image = image.resize((224, 224)) # Resize to match model's expected input size
|
| 20 |
+
image_array = np.array(image) / 255.0 # Normalize the image to [0, 1]
|
| 21 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
| 22 |
|
| 23 |
+
# Make prediction
|
| 24 |
+
prediction = model.predict(image_array)
|
| 25 |
+
# Assuming the model outputs a binary result (decorated or undecorated)
|
| 26 |
+
return "Decorated" if prediction[0] > 0.5 else "Undecorated"
|
| 27 |
|
| 28 |
+
# Set up Streamlit interface
|
| 29 |
+
st.title("Tree Decoration Predictor")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Upload image through Streamlit
|
| 32 |
+
uploaded_image = st.file_uploader("Upload an image of a tree", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
if uploaded_image:
|
| 35 |
+
# Open and display the uploaded image
|
| 36 |
+
image = Image.open(uploaded_image)
|
| 37 |
+
st.image(image, caption="Uploaded Tree Image", use_column_width=True)
|
| 38 |
|
| 39 |
+
# Predict decoration when the button is clicked
|
| 40 |
+
if st.button("Predict Decoration"):
|
| 41 |
+
prediction = predict_decoration(image)
|
| 42 |
+
st.write(f"Prediction: {prediction}")
|