Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -46,8 +46,7 @@ transform = transforms.Compose([
|
|
46 |
|
47 |
def predict(image):
|
48 |
try:
|
49 |
-
|
50 |
-
print(f"Received image: {image}")
|
51 |
|
52 |
# Check if the input contains a base64-encoded string
|
53 |
if isinstance(image, dict) and image.get("data"):
|
@@ -56,6 +55,7 @@ def predict(image):
|
|
56 |
image = Image.open(BytesIO(image_data))
|
57 |
print(f"Decoded base64 image: {image}")
|
58 |
except Exception as e:
|
|
|
59 |
return f"Error decoding base64 image: {e}"
|
60 |
|
61 |
# Check if the input is a URL
|
@@ -65,15 +65,24 @@ def predict(image):
|
|
65 |
image = Image.open(BytesIO(response.content))
|
66 |
print(f"Fetched image from URL: {image}")
|
67 |
except Exception as e:
|
|
|
68 |
return f"Error fetching image from URL: {e}"
|
69 |
|
|
|
|
|
|
|
|
|
|
|
70 |
# Apply transformations
|
71 |
image = transform(image).unsqueeze(0)
|
|
|
|
|
72 |
image = image.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
|
73 |
|
74 |
with torch.no_grad():
|
75 |
outputs = model(image)
|
76 |
predicted_class = torch.argmax(outputs, dim=1).item()
|
|
|
77 |
|
78 |
if predicted_class == 0:
|
79 |
return "The photo you've sent is of fall army worm with problem ID 126."
|
@@ -82,17 +91,18 @@ def predict(image):
|
|
82 |
else:
|
83 |
return "Unexpected class prediction."
|
84 |
except Exception as e:
|
|
|
85 |
return f"Error processing image: {e}"
|
86 |
|
87 |
# Create the Gradio interface
|
88 |
iface = gr.Interface(
|
89 |
fn=predict,
|
90 |
-
inputs=gr.Image(type="pil"),
|
91 |
-
outputs=gr.Textbox(),
|
92 |
live=True,
|
93 |
title="Maize Anomaly Detection",
|
94 |
description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
|
95 |
)
|
96 |
|
97 |
# Launch the Gradio interface
|
98 |
-
iface.launch(share=True)
|
|
|
46 |
|
47 |
def predict(image):
|
48 |
try:
|
49 |
+
print(f"Received image input: {image}")
|
|
|
50 |
|
51 |
# Check if the input contains a base64-encoded string
|
52 |
if isinstance(image, dict) and image.get("data"):
|
|
|
55 |
image = Image.open(BytesIO(image_data))
|
56 |
print(f"Decoded base64 image: {image}")
|
57 |
except Exception as e:
|
58 |
+
print(f"Error decoding base64 image: {e}")
|
59 |
return f"Error decoding base64 image: {e}"
|
60 |
|
61 |
# Check if the input is a URL
|
|
|
65 |
image = Image.open(BytesIO(response.content))
|
66 |
print(f"Fetched image from URL: {image}")
|
67 |
except Exception as e:
|
68 |
+
print(f"Error fetching image from URL: {e}")
|
69 |
return f"Error fetching image from URL: {e}"
|
70 |
|
71 |
+
# Validate that the image is correctly loaded
|
72 |
+
if not isinstance(image, Image.Image):
|
73 |
+
print("Invalid image format received.")
|
74 |
+
return "Invalid image format received."
|
75 |
+
|
76 |
# Apply transformations
|
77 |
image = transform(image).unsqueeze(0)
|
78 |
+
print(f"Transformed image tensor: {image.shape}")
|
79 |
+
|
80 |
image = image.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
|
81 |
|
82 |
with torch.no_grad():
|
83 |
outputs = model(image)
|
84 |
predicted_class = torch.argmax(outputs, dim=1).item()
|
85 |
+
print(f"Prediction output: {outputs}, Predicted class: {predicted_class}")
|
86 |
|
87 |
if predicted_class == 0:
|
88 |
return "The photo you've sent is of fall army worm with problem ID 126."
|
|
|
91 |
else:
|
92 |
return "Unexpected class prediction."
|
93 |
except Exception as e:
|
94 |
+
print(f"Error processing image: {e}")
|
95 |
return f"Error processing image: {e}"
|
96 |
|
97 |
# Create the Gradio interface
|
98 |
iface = gr.Interface(
|
99 |
fn=predict,
|
100 |
+
inputs=gr.Image(type="pil", label="Upload an image or provide a URL"), # Input: Image or URL
|
101 |
+
outputs=gr.Textbox(label="Prediction Result"), # Output: Predicted class
|
102 |
live=True,
|
103 |
title="Maize Anomaly Detection",
|
104 |
description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
|
105 |
)
|
106 |
|
107 |
# Launch the Gradio interface
|
108 |
+
iface.launch(share=True, show_error=True)
|