willco-afk commited on
Commit
39ae59e
·
verified ·
1 Parent(s): 4f607d9

Update app.py

Browse files

warning removed ~ considering additional UI changes and image presentations

Files changed (1) hide show
  1. app.py +31 -56
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 requests
7
- from io import BytesIO
 
 
8
 
9
- # Load the trained Keras model (ensure the model file is available in your Hugging Face Space)
10
- model = tf.keras.models.load_model("your_trained_model.keras")
11
 
12
- # Function to predict if the tree is decorated or undecorated
13
- def predict_image(image):
14
- image = image.resize((224, 224)) # Adjust size as required by your model
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
- # Layout with two sections for Undecorated and Decorated Trees
25
- col1, col2 = st.columns(2)
26
- with col1:
27
- st.header("Undecorated Trees")
28
- with col2:
29
- st.header("Decorated Trees")
30
 
31
- # Folder paths (since images are uploaded directly to the root folder)
32
- undecorated_folder = "./" # Root folder where undecorated images are uploaded
33
- decorated_folder = "./" # Root folder where decorated images are uploaded
 
34
 
35
- # Helper function to display images in a grid format
36
- def display_images_in_grid(image_folder, is_decorated=False):
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
- for i in range(rows):
46
- cols = st.columns(5)
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
- # Display undecorated tree images
62
- with col1:
63
- display_images_in_grid(undecorated_folder, is_decorated=False)
 
64
 
65
- # Display decorated tree images
66
- with col2:
67
- display_images_in_grid(decorated_folder, is_decorated=True)
 
 
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}")