jithin14 commited on
Commit
83ec8fa
·
verified ·
1 Parent(s): 0ccb899

update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -31
app.py CHANGED
@@ -59,7 +59,7 @@ def get_transform():
59
 
60
  # Define function to load model
61
  @st.cache_resource
62
- def load_model(model_path="models/model.pth"):
63
  # Try to find model_info.json to determine which model architecture to use
64
  model_dir = os.path.dirname(model_path)
65
  model_info_path = os.path.join(model_dir, "model_info.json")
@@ -290,41 +290,36 @@ def main():
290
  with upload_container:
291
  uploaded_file = st.file_uploader("Choose a leaf image to analyze", type=["jpg", "jpeg", "png"])
292
 
293
- # Add example images
294
- st.markdown("<h3>Or try an example:</h3>", unsafe_allow_html=True)
295
- example_cols = st.columns(4)
296
 
297
- # Example images from URLs
298
- example_images = [
299
- {"name": "Healthy Leaf", "path": "https://www.gardendesign.com/pictures/images/900x705Max/site_3/helianthus-yellow-flower-pixabay_12708.jpg"},
300
- {"name": "Mild Infestation", "path": "https://extension.umn.edu/sites/extension.umn.edu/files/styles/caption_medium/public/two-spotted-spider-mite-feeding-damage-JWeiland-iStock.jpg?itok=VLzrPzLc"},
301
- {"name": "Severe Infestation", "path": "https://www.planetnatural.com/wp-content/uploads/2012/12/spider-mite-damage-1.jpg"},
302
- {"name": "Spider Mite Closeup", "path": "https://bugguide.net/images/raw/ZR0/ZXR/ZR0ZXRCZMLJLUL1LJLQZ5RRR9RHH5RQZIRFZIRCZKRQZ5RNLKRFZ5RULARCZIRGZIZ9RTLSZPR.jpg"},
303
- ]
304
 
305
- use_example = None
306
- for i, example in enumerate(example_images):
307
- with example_cols[i]:
308
- st.image(example["path"], caption=example["name"], width=150)
309
- if st.button(f"Use Example {i+1}", key=f"example_{i}"):
310
- use_example = example["path"]
 
 
 
311
 
312
- # Process image
313
- if uploaded_file is not None or use_example is not None:
314
- # Handle example image
315
- if use_example is not None:
316
  try:
317
- import requests
318
- from io import BytesIO
319
- response = requests.get(use_example)
320
- image = Image.open(BytesIO(response.content)).convert('RGB')
321
- except Exception as e:
322
- st.error(f"Error loading example image: {e}")
323
  return
324
- else:
325
- # User uploaded image
326
- image = Image.open(uploaded_file).convert('RGB')
327
-
328
  st.markdown("---")
329
 
330
  analysis_container = st.container()
@@ -333,6 +328,7 @@ def main():
333
 
334
  with col1:
335
  st.markdown('<h3 class="sub-header">Uploaded Image</h3>', unsafe_allow_html=True)
 
336
  st.image(image, caption="", use_container_width=True)
337
 
338
  with col2:
 
59
 
60
  # Define function to load model
61
  @st.cache_resource
62
+ def load_model(model_path="model_comparison/best_model/model.pth"):
63
  # Try to find model_info.json to determine which model architecture to use
64
  model_dir = os.path.dirname(model_path)
65
  model_info_path = os.path.join(model_dir, "model_info.json")
 
290
  with upload_container:
291
  uploaded_file = st.file_uploader("Choose a leaf image to analyze", type=["jpg", "jpeg", "png"])
292
 
293
+ # Add test images from test_images folder
294
+ st.markdown("<h3>Or select a test image:</h3>", unsafe_allow_html=True)
295
+ test_cols = st.columns(4)
296
 
297
+ # Get test images from test_images folder
298
+ test_images = []
299
+ for root, dirs, files in os.walk("test_images"):
300
+ for file in files:
301
+ if file.lower().endswith((".jpg", ".jpeg", ".png")):
302
+ test_images.append({"name": file, "path": os.path.join(root, file)})
 
303
 
304
+ # Display test images
305
+ use_test = None
306
+ for i, test_img in enumerate(test_images):
307
+ with test_cols[i % 4]:
308
+ folder_type = "Healthy" if "healthy" in test_img["path"].lower() else "Spider Mite Infested"
309
+ st.image(test_img["path"], caption=folder_type, width=150)
310
+ if st.button(f"Use {folder_type}", key=f"test_button_{i}"):
311
+ use_test = test_img["path"]
312
+ uploaded_file = use_test
313
 
314
+ # Process image if uploaded
315
+ if uploaded_file is not None:
316
+ # Handle test image path
317
+ if isinstance(uploaded_file, str):
318
  try:
319
+ image = Image.open(uploaded_file).convert('RGB')
320
+ except:
321
+ st.error(f"Could not open test image: {uploaded_file}")
 
 
 
322
  return
 
 
 
 
323
  st.markdown("---")
324
 
325
  analysis_container = st.container()
 
328
 
329
  with col1:
330
  st.markdown('<h3 class="sub-header">Uploaded Image</h3>', unsafe_allow_html=True)
331
+ image = Image.open(uploaded_file).convert('RGB')
332
  st.image(image, caption="", use_container_width=True)
333
 
334
  with col2: