Reyal commited on
Commit
ddc8207
Β·
verified Β·
1 Parent(s): 79e6b11

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +28 -10
src/streamlit_app.py CHANGED
@@ -9,14 +9,14 @@ import tensorflow as tf
9
  # CONFIG
10
  # ======================
11
  st.set_page_config(page_title="Tomato Detector πŸ…")
12
- st.title("πŸ… Tomato Disease Detection (Local Image Mode)")
13
 
14
  # ======================
15
  # PATHS
16
  # ======================
17
  MODEL_PATH = "/app/src/best_tomato_model.h5"
18
  JSON_PATH = "/app/src/class_indices.json"
19
- IMAGE_PATH = "/app/src/test.jpg" # πŸ”₯ LOCAL IMAGE
20
 
21
  # ======================
22
  # DEBUG FILES
@@ -34,10 +34,6 @@ if not os.path.exists(JSON_PATH):
34
  st.error("class_indices.json not found")
35
  st.stop()
36
 
37
- if not os.path.exists(IMAGE_PATH):
38
- st.error("test.png not found in /app/src")
39
- st.stop()
40
-
41
  # ======================
42
  # LOAD MODEL
43
  # ======================
@@ -49,12 +45,13 @@ model = load_model()
49
  st.success("Model loaded")
50
 
51
  # ======================
52
- # LOAD CLASSES (FIXED ORDER)
53
  # ======================
54
  with open(JSON_PATH) as f:
55
  class_indices = json.load(f)
56
 
57
  class_names = [None] * len(class_indices)
 
58
  for k, v in class_indices.items():
59
  class_names[v] = k
60
 
@@ -72,16 +69,37 @@ def preprocess(img):
72
  return np.expand_dims(arr, axis=0)
73
 
74
  # ======================
75
- # LOAD LOCAL IMAGE
 
 
 
 
 
 
 
 
76
  # ======================
77
- image = Image.open(IMAGE_PATH)
 
 
 
 
 
 
 
 
 
78
 
79
- st.image(image, caption="Local test image", use_column_width=True)
 
 
 
80
 
81
  # ======================
82
  # PREDICT
83
  # ======================
84
  try:
 
85
  img = preprocess(image)
86
 
87
  preds = model.predict(img, verbose=0)
 
9
  # CONFIG
10
  # ======================
11
  st.set_page_config(page_title="Tomato Detector πŸ…")
12
+ st.title("πŸ… Tomato Disease Detection")
13
 
14
  # ======================
15
  # PATHS
16
  # ======================
17
  MODEL_PATH = "/app/src/best_tomato_model.h5"
18
  JSON_PATH = "/app/src/class_indices.json"
19
+ DEFAULT_IMAGE_PATH = "/app/src/test.jpg" # fallback image
20
 
21
  # ======================
22
  # DEBUG FILES
 
34
  st.error("class_indices.json not found")
35
  st.stop()
36
 
 
 
 
 
37
  # ======================
38
  # LOAD MODEL
39
  # ======================
 
45
  st.success("Model loaded")
46
 
47
  # ======================
48
+ # LOAD CLASSES
49
  # ======================
50
  with open(JSON_PATH) as f:
51
  class_indices = json.load(f)
52
 
53
  class_names = [None] * len(class_indices)
54
+
55
  for k, v in class_indices.items():
56
  class_names[v] = k
57
 
 
69
  return np.expand_dims(arr, axis=0)
70
 
71
  # ======================
72
+ # FILE UPLOADER UI
73
+ # ======================
74
+ uploaded_file = st.file_uploader(
75
+ "Upload tomato leaf image πŸ…",
76
+ type=["jpg", "jpeg", "png"]
77
+ )
78
+
79
+ # ======================
80
+ # IMAGE SOURCE SELECT
81
  # ======================
82
+ if uploaded_file is not None:
83
+ image = Image.open(uploaded_file)
84
+ st.success("Using uploaded image")
85
+ else:
86
+ if os.path.exists(DEFAULT_IMAGE_PATH):
87
+ image = Image.open(DEFAULT_IMAGE_PATH)
88
+ st.warning("Using default test image")
89
+ else:
90
+ st.error("No image available")
91
+ st.stop()
92
 
93
+ # ======================
94
+ # SHOW IMAGE
95
+ # ======================
96
+ st.image(image, caption="Input image", use_column_width=True)
97
 
98
  # ======================
99
  # PREDICT
100
  # ======================
101
  try:
102
+
103
  img = preprocess(image)
104
 
105
  preds = model.predict(img, verbose=0)