Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Commit 
							
							·
						
						418cf06
	
1
								Parent(s):
							
							3d77e30
								
Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -4,6 +4,21 @@ import numpy as np | |
| 4 | 
             
            from PIL import Image
         | 
| 5 | 
             
            import urllib.request
         | 
| 6 | 
             
            from utils import *  # Assuming the gen_labels() and preprocess() functions are in this module
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 7 |  | 
| 8 | 
             
            # Load labels
         | 
| 9 | 
             
            labels = gen_labels()
         | 
| @@ -31,15 +46,10 @@ if 'image' in locals():  # Check if image variable exists | |
| 31 |  | 
| 32 | 
             
                if st.button('Predict'):
         | 
| 33 | 
             
                    try:
         | 
| 34 | 
            -
                         | 
| 35 | 
            -
                         | 
| 36 | 
            -
                        prediction = model.predict(img_array[np.newaxis, ...])
         | 
| 37 | 
            -
                        
         | 
| 38 | 
            -
                        # Get the predicted class name
         | 
| 39 | 
            -
                        predicted_class_index = np.argmax(prediction[0], axis=-1)
         | 
| 40 | 
            -
                        predicted_class_name = labels[predicted_class_index]
         | 
| 41 |  | 
| 42 | 
            -
                        st.info('The uploaded image has been classified as "{}" waste.'.format( | 
| 43 |  | 
| 44 | 
             
                    except Exception as e:
         | 
| 45 | 
             
                        st.error(f"An error occurred: {e}")
         | 
|  | |
| 4 | 
             
            from PIL import Image
         | 
| 5 | 
             
            import urllib.request
         | 
| 6 | 
             
            from utils import *  # Assuming the gen_labels() and preprocess() functions are in this module
         | 
| 7 | 
            +
            from your_model_module import model_arc  # Import your model function
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            # Function to classify the garbage
         | 
| 10 | 
            +
            def classify_garbage(img_path, model):
         | 
| 11 | 
            +
                processed_img = preprocess_image(img_path)
         | 
| 12 | 
            +
                prediction = model.predict(processed_img)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                class_labels = ["cardboard", "glass", "metal", "paper", "plastic", "trash"]
         | 
| 15 | 
            +
                predicted_class = np.argmax(prediction, axis=1)[0]
         | 
| 16 | 
            +
                classification_result = class_labels[predicted_class]
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                # Get the confidence (probability) of the predicted class
         | 
| 19 | 
            +
                confidence = prediction[0][predicted_class] * 100  # Convert probability to percentage
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                return classification_result, confidence
         | 
| 22 |  | 
| 23 | 
             
            # Load labels
         | 
| 24 | 
             
            labels = gen_labels()
         | 
|  | |
| 46 |  | 
| 47 | 
             
                if st.button('Predict'):
         | 
| 48 | 
             
                    try:
         | 
| 49 | 
            +
                        model = model_arc()  # Initialize your model
         | 
| 50 | 
            +
                        predicted_class, confidence = classify_garbage(image, model)
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 51 |  | 
| 52 | 
            +
                        st.info('The uploaded image has been classified as "{}" waste with {:.2f}% confidence.'.format(predicted_class, confidence))
         | 
| 53 |  | 
| 54 | 
             
                    except Exception as e:
         | 
| 55 | 
             
                        st.error(f"An error occurred: {e}")
         | 

