import streamlit as st from PIL import Image import torch import time import tempfile import cv2 import numpy as np from pathlib import Path import plotly.graph_objects as go import pandas as pd import traceback # To capture detailed error info # Import our classifier from waste_classifier import DualWasteClassifier # Static content moved to separate functions def load_custom_css(): """Load custom CSS for the app.""" st.markdown(""" """, unsafe_allow_html=True) class WasteVisionApp: def __init__(self): self.classifier = DualWasteClassifier() self.setup_page() self.main() def setup_page(self): """Initial page configuration.""" st.set_page_config(page_title="Waste Vision", layout="wide") load_custom_css() def main(self): """Main function to handle app logic and navigation.""" st.title("🌍 Waste Vision: Revolutionizing Waste Management") # Use tabs instead of sidebar for navigation tab1, tab2 = st.tabs(["Live Demo", "Impact and Vision"]) with tab1: self.show_live_demo() with tab2: self.show_future_vision() def upload_and_display_image(self): """Function to handle image uploading and displaying.""" uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) if uploaded_file: image = Image.open(uploaded_file) st.image(image, caption="Uploaded Image", use_column_width=True) return uploaded_file return None @st.cache_resource def classify_image(self, image_path): """Cache the classification process for efficiency.""" return self.classifier.classify_image(image_path) def classify_image_safely(self, image_path): """Handle image classification with error catching and detailed logging.""" try: # Log the image path to make sure it's being passed correctly st.write(f"Classifying image from path: {image_path}") # Perform classification with the image path result = self.classify_image(image_path) # Check if result is None (if classifier returns None on failure) if result is None: st.error("Classifier returned no result. Please check the input.") return None return result except Exception as e: # Log the full traceback to help with debugging st.error(f"Error during classification: {str(e)}") st.error(traceback.format_exc()) # Print full error details for debugging return None def show_live_demo(self): """Display the live demo section.""" st.header("🎯 Live Waste Classification Demo") col1, col2 = st.columns(2) with col1: st.subheader("Upload Image") uploaded_file = self.upload_and_display_image() if uploaded_file and st.button("Classify Waste"): with st.spinner("Analyzing..."): # Save the uploaded image to a temporary file try: with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file: # Ensure the image is saved in the correct format image = Image.open(uploaded_file) image.save(temp_file.name) temp_file_path = temp_file.name # Pass the temporary file path to the classifier result = self.classify_image_safely(temp_file_path) if result: st.markdown(f"