Commit
Β·
aee8033
1
Parent(s):
9264cb9
fix: Get absolute path for images
Browse files
{assets β src/assets}/animal.jpg
RENAMED
|
File without changes
|
{assets β src/assets}/building.jpg
RENAMED
|
File without changes
|
{assets β src/assets}/object.jpg
RENAMED
|
File without changes
|
{assets β src/assets}/vehicle.jpg
RENAMED
|
File without changes
|
src/streamlit_app.py
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
|
| 4 |
from predictor import predict_image
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
# π PAGE SETUP
|
| 7 |
st.set_page_config(page_title="Image Classifier App", page_icon="π€", layout="centered")
|
| 8 |
st.html("""
|
|
@@ -15,10 +20,12 @@ st.html("""
|
|
| 15 |
|
| 16 |
# π INITIALIZE SESSION STATE
|
| 17 |
# We initialize session state variables to manage app state
|
| 18 |
-
if "
|
| 19 |
-
st.session_state["
|
| 20 |
-
if "
|
| 21 |
-
st.session_state["
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# π MAIN APP LAYOUT
|
| 24 |
with st.container():
|
|
@@ -41,13 +48,19 @@ with st.container():
|
|
| 41 |
st.header("Upload an Image", divider=True)
|
| 42 |
|
| 43 |
# File uploader widget
|
| 44 |
-
|
| 45 |
label="Drag and drop an image here or click to browse",
|
| 46 |
type=["jpg", "jpeg", "png"],
|
| 47 |
help="Maximum file size is 200MB",
|
| 48 |
key="image_uploader",
|
| 49 |
)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
st.html("<br>")
|
| 52 |
st.subheader("Or Try an Example", divider=True)
|
| 53 |
|
|
@@ -73,57 +86,60 @@ with st.container():
|
|
| 73 |
with col_results:
|
| 74 |
st.header("Results", divider=True)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
img_path = f"./assets/{selected_example.lower()}.jpg"
|
| 93 |
-
st.session_state["selected_image"] = Image.open(img_path)
|
| 94 |
-
except FileNotFoundError:
|
| 95 |
-
st.error(
|
| 96 |
-
f"Error: The example image '{selected_example.lower()}.jpg' was not found."
|
| 97 |
-
)
|
| 98 |
-
st.stop()
|
| 99 |
-
|
| 100 |
-
if st.session_state["selected_image"] is not None:
|
| 101 |
-
st.image(
|
| 102 |
-
st.session_state["selected_image"],
|
| 103 |
-
caption="Image to be classified",
|
| 104 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
|
|
|
| 107 |
with st.spinner("Analyzing image..."):
|
| 108 |
-
# Call our modularized prediction function!
|
| 109 |
try:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
value=f"{predicted_label.replace('_', ' ').title()}",
|
| 117 |
-
delta=f"{predicted_score * 100:.2f}%",
|
| 118 |
-
help="The predicted category and its confidence score.",
|
| 119 |
-
delta_color="normal",
|
| 120 |
)
|
| 121 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
st.error(f"An error occurred during prediction: {e}")
|
| 124 |
-
else:
|
| 125 |
-
st.error("Please upload an image or select an example to classify.")
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
# π DESCRIPTION TAB
|
| 129 |
with tab_description:
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
from predictor import predict_image
|
| 7 |
|
| 8 |
+
APP_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 9 |
+
ASSETS_DIR = os.path.join(APP_DIR, "assets")
|
| 10 |
+
|
| 11 |
# π PAGE SETUP
|
| 12 |
st.set_page_config(page_title="Image Classifier App", page_icon="π€", layout="centered")
|
| 13 |
st.html("""
|
|
|
|
| 20 |
|
| 21 |
# π INITIALIZE SESSION STATE
|
| 22 |
# We initialize session state variables to manage app state
|
| 23 |
+
if "uploaded_image" not in st.session_state:
|
| 24 |
+
st.session_state["uploaded_image"] = None
|
| 25 |
+
if "example_selected" not in st.session_state:
|
| 26 |
+
st.session_state["example_selected"] = False
|
| 27 |
+
if "prediction_result" not in st.session_state:
|
| 28 |
+
st.session_state["prediction_result"] = None
|
| 29 |
|
| 30 |
# π MAIN APP LAYOUT
|
| 31 |
with st.container():
|
|
|
|
| 48 |
st.header("Upload an Image", divider=True)
|
| 49 |
|
| 50 |
# File uploader widget
|
| 51 |
+
uploaded_file = st.file_uploader(
|
| 52 |
label="Drag and drop an image here or click to browse",
|
| 53 |
type=["jpg", "jpeg", "png"],
|
| 54 |
help="Maximum file size is 200MB",
|
| 55 |
key="image_uploader",
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# Update state when a new file is uploaded
|
| 59 |
+
if uploaded_file is not st.session_state.uploaded_image:
|
| 60 |
+
st.session_state.uploaded_image = uploaded_file
|
| 61 |
+
st.session_state.example_selected = False
|
| 62 |
+
st.session_state.prediction_result = None
|
| 63 |
+
|
| 64 |
st.html("<br>")
|
| 65 |
st.subheader("Or Try an Example", divider=True)
|
| 66 |
|
|
|
|
| 86 |
with col_results:
|
| 87 |
st.header("Results", divider=True)
|
| 88 |
|
| 89 |
+
image_to_process = None
|
| 90 |
+
|
| 91 |
+
# Logic to handle which image to display
|
| 92 |
+
if st.session_state.uploaded_image:
|
| 93 |
+
# Get the image from the uploaded file
|
| 94 |
+
image_to_process = Image.open(st.session_state.uploaded_image)
|
| 95 |
+
elif selected_example:
|
| 96 |
+
# Load the selected example image using a robust path
|
| 97 |
+
try:
|
| 98 |
+
img_path = os.path.join(
|
| 99 |
+
ASSETS_DIR, f"{selected_example.lower()}.jpg"
|
| 100 |
+
)
|
| 101 |
+
image_to_process = Image.open(img_path)
|
| 102 |
+
except FileNotFoundError:
|
| 103 |
+
st.error(
|
| 104 |
+
f"Error: The example image '{selected_example.lower()}.jpg' was not found."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
)
|
| 106 |
+
st.stop()
|
| 107 |
+
|
| 108 |
+
# Display image and run prediction when button is clicked
|
| 109 |
+
if image_to_process:
|
| 110 |
+
st.image(image_to_process, caption="Image to be classified")
|
| 111 |
|
| 112 |
+
if classify_button:
|
| 113 |
+
# Run the prediction logic
|
| 114 |
with st.spinner("Analyzing image..."):
|
|
|
|
| 115 |
try:
|
| 116 |
+
# π Prediction function call π
|
| 117 |
+
from predictor import predict_image
|
|
|
|
| 118 |
|
| 119 |
+
predicted_label, predicted_score = predict_image(
|
| 120 |
+
image_to_process
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
)
|
| 122 |
+
st.session_state.prediction_result = {
|
| 123 |
+
"label": predicted_label.replace("_", " ").title(),
|
| 124 |
+
"score": predicted_score,
|
| 125 |
+
}
|
| 126 |
except Exception as e:
|
| 127 |
st.error(f"An error occurred during prediction: {e}")
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
# Display the prediction result if available
|
| 130 |
+
if st.session_state.prediction_result:
|
| 131 |
+
st.metric(
|
| 132 |
+
label="Prediction",
|
| 133 |
+
value=st.session_state.prediction_result["label"],
|
| 134 |
+
delta=f"{st.session_state.prediction_result['score'] * 100:.2f}%",
|
| 135 |
+
help="The predicted category and its confidence score.",
|
| 136 |
+
delta_color="normal",
|
| 137 |
+
)
|
| 138 |
+
st.balloons()
|
| 139 |
+
else:
|
| 140 |
+
st.info("Click 'Classify Image' to see the prediction.")
|
| 141 |
+
else:
|
| 142 |
+
st.info("Choose an image to get a prediction.")
|
| 143 |
|
| 144 |
# π DESCRIPTION TAB
|
| 145 |
with tab_description:
|