Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ from PIL import Image
|
|
| 6 |
import requests
|
| 7 |
import base64
|
| 8 |
from io import BytesIO
|
| 9 |
-
import os
|
| 10 |
|
| 11 |
# Define the number of classes
|
| 12 |
num_classes = 2 # Update with the actual number of classes in your dataset
|
|
@@ -34,23 +33,16 @@ transform = transforms.Compose([
|
|
| 34 |
])
|
| 35 |
|
| 36 |
# Prediction function
|
| 37 |
-
def process_image(
|
| 38 |
try:
|
| 39 |
-
#
|
| 40 |
-
if
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
response = requests.get(data["url"])
|
| 48 |
-
image = Image.open(BytesIO(response.content))
|
| 49 |
-
elif "path" in data:
|
| 50 |
-
# Local path image loading
|
| 51 |
-
image = Image.open(data["path"])
|
| 52 |
-
else:
|
| 53 |
-
return "Invalid input data structure."
|
| 54 |
|
| 55 |
# Validate image
|
| 56 |
if not isinstance(image, Image.Image):
|
|
@@ -77,11 +69,14 @@ def process_image(data):
|
|
| 77 |
# Create the Gradio interface
|
| 78 |
iface = gr.Interface(
|
| 79 |
fn=process_image,
|
| 80 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 81 |
outputs=gr.Textbox(label="Prediction Result"), # Output: Prediction result
|
| 82 |
live=True,
|
| 83 |
title="Maize Anomaly Detection",
|
| 84 |
-
description="Upload an image of maize to detect anomalies like disease or pest infestation. You can
|
| 85 |
)
|
| 86 |
|
| 87 |
# Launch the Gradio interface
|
|
|
|
| 6 |
import requests
|
| 7 |
import base64
|
| 8 |
from io import BytesIO
|
|
|
|
| 9 |
|
| 10 |
# Define the number of classes
|
| 11 |
num_classes = 2 # Update with the actual number of classes in your dataset
|
|
|
|
| 33 |
])
|
| 34 |
|
| 35 |
# Prediction function
|
| 36 |
+
def process_image(image, image_url=None):
|
| 37 |
try:
|
| 38 |
+
# Handle URL-based image loading
|
| 39 |
+
if image_url:
|
| 40 |
+
response = requests.get(image_url)
|
| 41 |
+
image = Image.open(BytesIO(response.content))
|
| 42 |
+
|
| 43 |
+
# Handle local file path image loading (Gradio File input)
|
| 44 |
+
elif isinstance(image, str) and os.path.isfile(image):
|
| 45 |
+
image = Image.open(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Validate image
|
| 48 |
if not isinstance(image, Image.Image):
|
|
|
|
| 69 |
# Create the Gradio interface
|
| 70 |
iface = gr.Interface(
|
| 71 |
fn=process_image,
|
| 72 |
+
inputs=[
|
| 73 |
+
gr.File(label="Upload an image (Local File Path)"), # Input: Local file
|
| 74 |
+
gr.Textbox(label="Enter Image URL", placeholder="Enter image URL here (optional)", optional=True) # Input: Image URL (optional)
|
| 75 |
+
],
|
| 76 |
outputs=gr.Textbox(label="Prediction Result"), # Output: Prediction result
|
| 77 |
live=True,
|
| 78 |
title="Maize Anomaly Detection",
|
| 79 |
+
description="Upload an image of maize to detect anomalies like disease or pest infestation. You can upload local images or provide an image URL."
|
| 80 |
)
|
| 81 |
|
| 82 |
# Launch the Gradio interface
|