Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,14 @@ from tensorflow.keras.applications import efficientnet
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Define the path to your Keras model file
|
| 9 |
MODEL_PATH = 'ifswaste_classification_model_initial.keras'
|
|
@@ -32,15 +40,16 @@ def classify_waste(image):
|
|
| 32 |
"""
|
| 33 |
Classifies the input image using the loaded Keras model.
|
| 34 |
"""
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
if model is None:
|
| 38 |
print("Error: Model is None inside classify_waste.")
|
| 39 |
-
return
|
| 40 |
|
| 41 |
if image is None:
|
| 42 |
print("Error: Image is None - no valid image data received")
|
| 43 |
-
return
|
| 44 |
|
| 45 |
# Print image type and info for debugging
|
| 46 |
print(f"Image type: {type(image)}")
|
|
@@ -54,15 +63,35 @@ def classify_waste(image):
|
|
| 54 |
if not isinstance(image, Image.Image):
|
| 55 |
print(f"Converting image from {type(image)} to PIL Image")
|
| 56 |
if isinstance(image, np.ndarray):
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
else:
|
| 59 |
print(f"Unsupported image type: {type(image)}")
|
| 60 |
-
return
|
| 61 |
|
| 62 |
# Resize to match training dimensions
|
| 63 |
print(f"Resizing image to ({IMG_WIDTH}, {IMG_HEIGHT})")
|
| 64 |
img = image.resize((IMG_WIDTH, IMG_HEIGHT))
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# Convert to numpy array
|
| 67 |
print("Converting image to numpy array")
|
| 68 |
img_array = np.array(img)
|
|
@@ -78,39 +107,54 @@ def classify_waste(image):
|
|
| 78 |
|
| 79 |
# Make prediction
|
| 80 |
print("Making prediction with the model")
|
| 81 |
-
predictions = model.predict(img_array)[0]
|
| 82 |
print(f"Raw predictions: {predictions}")
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
|
|
|
| 87 |
print("=== CLASSIFY_WASTE FUNCTION COMPLETED SUCCESSFULLY ===")
|
| 88 |
-
return
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
print(f"Error during classification: {e}")
|
| 92 |
traceback.print_exc()
|
| 93 |
-
return
|
| 94 |
|
| 95 |
-
# ---
|
| 96 |
if model is not None:
|
| 97 |
print("Creating Gradio interface")
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
print("Launching Gradio interface")
|
| 113 |
-
|
| 114 |
else:
|
| 115 |
print("Model failed to load - not launching interface")
|
| 116 |
gr.Interface(
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import traceback
|
| 7 |
+
import time
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
# Print debugging information to make sure logs are visible
|
| 11 |
+
print(f"Python version: {sys.version}")
|
| 12 |
+
print(f"TensorFlow version: {tf.__version__}")
|
| 13 |
+
print(f"NumPy version: {np.__version__}")
|
| 14 |
+
print(f"PIL version: {Image.__version__}")
|
| 15 |
|
| 16 |
# Define the path to your Keras model file
|
| 17 |
MODEL_PATH = 'ifswaste_classification_model_initial.keras'
|
|
|
|
| 40 |
"""
|
| 41 |
Classifies the input image using the loaded Keras model.
|
| 42 |
"""
|
| 43 |
+
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
|
| 44 |
+
print(f"=== CLASSIFY_WASTE FUNCTION TRIGGERED AT {timestamp} ===")
|
| 45 |
|
| 46 |
if model is None:
|
| 47 |
print("Error: Model is None inside classify_waste.")
|
| 48 |
+
return [("Error", 1.0)]
|
| 49 |
|
| 50 |
if image is None:
|
| 51 |
print("Error: Image is None - no valid image data received")
|
| 52 |
+
return [("No image provided", 1.0)]
|
| 53 |
|
| 54 |
# Print image type and info for debugging
|
| 55 |
print(f"Image type: {type(image)}")
|
|
|
|
| 63 |
if not isinstance(image, Image.Image):
|
| 64 |
print(f"Converting image from {type(image)} to PIL Image")
|
| 65 |
if isinstance(image, np.ndarray):
|
| 66 |
+
# Make sure the array has valid values
|
| 67 |
+
if image.size == 0:
|
| 68 |
+
print("Error: Empty numpy array")
|
| 69 |
+
return [("Empty image data", 1.0)]
|
| 70 |
+
|
| 71 |
+
# Ensure the array is uint8 for PIL
|
| 72 |
+
if image.dtype != np.uint8:
|
| 73 |
+
print(f"Converting array from {image.dtype} to uint8")
|
| 74 |
+
# Scale values if needed
|
| 75 |
+
if image.max() <= 1.0:
|
| 76 |
+
image = (image * 255).astype(np.uint8)
|
| 77 |
+
else:
|
| 78 |
+
image = image.astype(np.uint8)
|
| 79 |
+
|
| 80 |
+
# Convert to PIL
|
| 81 |
+
image = Image.fromarray(image)
|
| 82 |
else:
|
| 83 |
print(f"Unsupported image type: {type(image)}")
|
| 84 |
+
return [("Unsupported image format", 1.0)]
|
| 85 |
|
| 86 |
# Resize to match training dimensions
|
| 87 |
print(f"Resizing image to ({IMG_WIDTH}, {IMG_HEIGHT})")
|
| 88 |
img = image.resize((IMG_WIDTH, IMG_HEIGHT))
|
| 89 |
|
| 90 |
+
# Check if image has correct mode (RGB)
|
| 91 |
+
if img.mode != "RGB":
|
| 92 |
+
print(f"Converting image from {img.mode} to RGB")
|
| 93 |
+
img = img.convert("RGB")
|
| 94 |
+
|
| 95 |
# Convert to numpy array
|
| 96 |
print("Converting image to numpy array")
|
| 97 |
img_array = np.array(img)
|
|
|
|
| 107 |
|
| 108 |
# Make prediction
|
| 109 |
print("Making prediction with the model")
|
| 110 |
+
predictions = model.predict(img_array, verbose=0)[0]
|
| 111 |
print(f"Raw predictions: {predictions}")
|
| 112 |
|
| 113 |
+
# Format predictions in the way Gradio's Label component expects
|
| 114 |
+
result = [(CLASS_NAMES[i], float(predictions[i])) for i in range(len(CLASS_NAMES))]
|
| 115 |
+
result.sort(key=lambda x: x[1], reverse=True)
|
| 116 |
+
print(f"Sorted predictions: {result}")
|
| 117 |
print("=== CLASSIFY_WASTE FUNCTION COMPLETED SUCCESSFULLY ===")
|
| 118 |
+
return result
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
print(f"Error during classification: {e}")
|
| 122 |
traceback.print_exc()
|
| 123 |
+
return [("Error: " + str(e), 1.0)]
|
| 124 |
|
| 125 |
+
# --- Gradio Interface ---
|
| 126 |
if model is not None:
|
| 127 |
print("Creating Gradio interface")
|
| 128 |
|
| 129 |
+
with gr.Blocks(title="Smart Waste Sort Classifier") as demo:
|
| 130 |
+
gr.Markdown("# Smart Waste Sort Classifier")
|
| 131 |
+
gr.Markdown("Point your webcam at an item and click 'Classify' to identify it.")
|
| 132 |
+
|
| 133 |
+
with gr.Row():
|
| 134 |
+
with gr.Column():
|
| 135 |
+
# Use webcam input component
|
| 136 |
+
image_input = gr.Image(sources=["webcam"], type="numpy", label="Webcam Feed")
|
| 137 |
+
classify_btn = gr.Button("Classify", variant="primary")
|
| 138 |
+
|
| 139 |
+
with gr.Column():
|
| 140 |
+
label_output = gr.Label(label="Classification Results")
|
| 141 |
+
|
| 142 |
+
# Connect the button to the classification function
|
| 143 |
+
classify_btn.click(
|
| 144 |
+
fn=classify_waste,
|
| 145 |
+
inputs=image_input,
|
| 146 |
+
outputs=label_output
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# Also connect the image input directly to enable auto-classification
|
| 150 |
+
image_input.change(
|
| 151 |
+
fn=classify_waste,
|
| 152 |
+
inputs=image_input,
|
| 153 |
+
outputs=label_output
|
| 154 |
+
)
|
| 155 |
|
| 156 |
print("Launching Gradio interface")
|
| 157 |
+
demo.launch()
|
| 158 |
else:
|
| 159 |
print("Model failed to load - not launching interface")
|
| 160 |
gr.Interface(
|