demo_app.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import streamlit as st
  2. from ultralytics import YOLO
  3. from PIL import Image
  4. import numpy as np
  5. import io
  6. # --- Page Config ---
  7. st.set_page_config(page_title="Palm Oil Ripeness AI", layout="wide")
  8. st.title("🌴 Palm Oil FFB Ripeness Detector")
  9. st.markdown("### R&D Proof of Concept: Automated Maturity Grading")
  10. # --- Load Model (Cached for performance) ---
  11. @st.cache_resource
  12. def load_model():
  13. return YOLO("best.pt")
  14. model = load_model()
  15. # --- Sidebar Controls ---
  16. st.sidebar.header("Settings")
  17. conf_threshold = st.sidebar.slider("Confidence Threshold", 0.1, 1.0, 0.5)
  18. # --- Image Upload ---
  19. uploaded_file = st.file_uploader("Drag and drop a Palm Oil FFB image here...", type=["jpg", "jpeg", "png"])
  20. if uploaded_file is not None:
  21. # Convert uploaded file to PIL Image
  22. image = Image.open(uploaded_file)
  23. # Layout: Original vs Predicted
  24. col1, col2 = st.columns(2)
  25. with col1:
  26. st.image(image, caption="Uploaded Image", use_container_width=True)
  27. with col2:
  28. with st.spinner('Analyzing ripeness...'):
  29. # Run Inference
  30. results = model.predict(source=image, conf=conf_threshold)
  31. # The .plot() method returns a BGR numpy array with boxes drawn
  32. annotated_img = results[0].plot()
  33. # Convert BGR (OpenCV format) to RGB (Streamlit/PIL format)
  34. annotated_img_rgb = annotated_img[:, :, ::-1]
  35. st.image(annotated_img_rgb, caption="AI Analysis Result", use_container_width=True)
  36. # --- Metrics Section ---
  37. st.divider()
  38. st.subheader("Analysis Summary")
  39. detections = results[0].boxes
  40. if len(detections) > 0:
  41. for box in detections:
  42. label = model.names[int(box.cls)]
  43. conf = float(box.conf)
  44. st.success(f"**Detected:** {label} | **Confidence:** {conf:.2%}")
  45. else:
  46. st.warning("No fruit bunches detected. Try adjusting the confidence slider.")