-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp__old.py
More file actions
100 lines (76 loc) · 2.44 KB
/
Copy pathapp__old.py
File metadata and controls
100 lines (76 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import streamlit as st
from PIL import Image
import tempfile
import numpy as np
from feature_extraction import extract_features
from utils.model_utils import load_model
# -----------------------------
# Page Configuration
# -----------------------------
st.set_page_config(
page_title="Fruit Freshness Detection",
page_icon="🍎",
layout="centered"
)
# -----------------------------
# Load Model
# -----------------------------
model = load_model()
CLASS_NAMES = {
0: "🍎 Fresh Apple",
1: "🍎 Rotten Apple",
2: "🍌 Fresh Banana",
3: "🍌 Rotten Banana",
4: "🍓 Fresh Strawberry",
5: "🍓 Rotten Strawberry"
}
# -----------------------------
# Sidebar
# -----------------------------
st.sidebar.title("Fruit Freshness Detection")
st.sidebar.markdown("""
### Model Information
**Algorithm:** Random Forest
**Accuracy:** 87.72%
""")
# -----------------------------
# Main Page
# -----------------------------
st.title("🍎 Fruit Freshness Detection")
st.write("Upload a fruit image to predict whether it is **Fresh** or **Rotten**.")
uploaded_file = st.file_uploader(
"Choose an image",
type=["jpg", "jpeg", "png"]
)
st.write("Debug 1: App Loaded")
if uploaded_file is not None:
st.write("Debug 2: File Selected")
st.write(uploaded_file.name)
if uploaded_file is not None:
try:
# Display uploaded image
image = Image.open(uploaded_file).convert("RGB")
st.image(image, caption="Uploaded Image", use_container_width=True)
# Save temporarily
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp:
image.save(temp.name)
temp_path = temp.name
# Extract features
features = extract_features(temp_path)
if features is None:
st.error("Feature extraction failed.")
st.stop()
st.write("Debug 3: Extracting Features")
# Prediction
prediction = model.predict([features])[0]
st.success(f"Prediction: {CLASS_NAMES[prediction]}")
st.write("Debug 4: Prediction Completed")
# Confidence
if hasattr(model, "predict_proba"):
probabilities = model.predict_proba([features])[0]
confidence = np.max(probabilities) * 100
st.info(f"Confidence: {confidence:.2f}%")
st.progress(int(confidence))
except Exception as e:
st.error("An error occurred during prediction.")
st.exception(e)