Update src/streamlit_app.py
Browse files- src/streamlit_app.py +12 -56
src/streamlit_app.py
CHANGED
|
@@ -1,66 +1,22 @@
|
|
| 1 |
-
import os
|
| 2 |
-
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
|
| 3 |
-
os.environ["STREAMLIT_SECRETS_FILE"] = "/tmp/.streamlit/secrets.toml"
|
| 4 |
-
|
| 5 |
import streamlit as st
|
| 6 |
-
import pickle
|
| 7 |
import numpy as np
|
| 8 |
import cv2
|
| 9 |
|
| 10 |
-
|
| 11 |
-
# Load saved models
|
| 12 |
-
# ------------------------------
|
| 13 |
-
with open("src/pca_model.pkl", "rb") as f:
|
| 14 |
-
pca = pickle.load(f)
|
| 15 |
-
|
| 16 |
-
with open("src/svc_model.pkl", "rb") as f:
|
| 17 |
-
svc = pickle.load(f)
|
| 18 |
-
|
| 19 |
-
with open("src/scaler.pkl", "rb") as f:
|
| 20 |
-
scaler = pickle.load(f)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
# Label mapping
|
| 24 |
-
# ------------------------------
|
| 25 |
-
label_map = {
|
| 26 |
-
0: "fresh apple",
|
| 27 |
-
1: "fresh banana",
|
| 28 |
-
2: "fresh orange",
|
| 29 |
-
3: "rotten apple",
|
| 30 |
-
4: "rotten banana",
|
| 31 |
-
5: "rotten orange"
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
# ------------------------------
|
| 35 |
-
# Streamlit UI
|
| 36 |
-
# ------------------------------
|
| 37 |
-
st.set_page_config(page_title="Fruit Image Classification", layout="centered")
|
| 38 |
-
st.title("π Fruit Image Classification")
|
| 39 |
-
st.write("Upload an image of a fruit (fresh or rotten) to get the prediction.")
|
| 40 |
-
|
| 41 |
-
# ------------------------------
|
| 42 |
-
# File uploader
|
| 43 |
-
# ------------------------------
|
| 44 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 45 |
|
| 46 |
if uploaded_file is not None:
|
| 47 |
-
# Read
|
| 48 |
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 49 |
-
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 50 |
-
|
| 51 |
-
# Convert BGR β RGB for display
|
| 52 |
-
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 53 |
-
st.image(img_rgb, caption="Uploaded Image", use_container_width=True)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
img_pca = pca.transform(img_scaled)
|
| 62 |
-
pred = svc.predict(img_pca)[0]
|
| 63 |
-
label = label_map.get(pred, "Unknown")
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
| 4 |
|
| 5 |
+
st.title("π Dummy Image Upload Test")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
if uploaded_file is not None:
|
| 10 |
+
# Read file into OpenCV format
|
| 11 |
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 12 |
+
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR) # decode image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
if img is None:
|
| 15 |
+
st.error("β OpenCV failed to load the image. (cv2.imdecode returned None)")
|
| 16 |
+
else:
|
| 17 |
+
# Show original BGR and converted RGB
|
| 18 |
+
st.image(img, channels="BGR", caption="Uploaded Image (BGR)", use_container_width=True)
|
| 19 |
+
st.image(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), caption="Uploaded Image (RGB)", use_container_width=True)
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Show shape info
|
| 22 |
+
st.write("Image shape:", img.shape)
|