Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ from io import BytesIO
|
|
10 |
import os
|
11 |
|
12 |
# Define the number of classes
|
13 |
-
num_classes = 2
|
14 |
|
15 |
# Download model from Hugging Face
|
16 |
def download_model():
|
@@ -48,40 +48,44 @@ transform = transforms.Compose([
|
|
48 |
def predict(image):
|
49 |
try:
|
50 |
print(f"Received image input: {image}")
|
51 |
-
|
52 |
-
# Check if the input
|
53 |
-
if isinstance(image,
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
85 |
|
86 |
# Apply transformations
|
87 |
image = transform(image).unsqueeze(0)
|
@@ -108,11 +112,11 @@ def predict(image):
|
|
108 |
iface = gr.Interface(
|
109 |
fn=predict,
|
110 |
inputs=gr.Image(type="pil", label="Upload an image or provide a URL or local path"), # Input: Image, URL, or Local Path
|
111 |
-
outputs=gr.Textbox(label="Prediction Result"),
|
112 |
live=True,
|
113 |
title="Maize Anomaly Detection",
|
114 |
description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
|
115 |
)
|
116 |
|
117 |
-
# Launch the Gradio interface
|
118 |
-
iface.launch(share=True, show_error=True)
|
|
|
10 |
import os
|
11 |
|
12 |
# Define the number of classes
|
13 |
+
num_classes = 2
|
14 |
|
15 |
# Download model from Hugging Face
|
16 |
def download_model():
|
|
|
48 |
def predict(image):
|
49 |
try:
|
50 |
print(f"Received image input: {image}")
|
51 |
+
|
52 |
+
# Check if the input is a PIL Image type
|
53 |
+
if isinstance(image, Image.Image):
|
54 |
+
print(f"Image is already loaded as PIL Image: {image}")
|
55 |
+
else:
|
56 |
+
# Try to handle base64-encoded image
|
57 |
+
if isinstance(image, dict) and image.get("data"):
|
58 |
+
try:
|
59 |
+
image_data = base64.b64decode(image["data"])
|
60 |
+
image = Image.open(BytesIO(image_data))
|
61 |
+
print(f"Decoded base64 image: {image}")
|
62 |
+
except Exception as e:
|
63 |
+
print(f"Error decoding base64 image: {e}")
|
64 |
+
return f"Error decoding base64 image: {e}"
|
65 |
+
|
66 |
+
# Try to fetch the image from a URL
|
67 |
+
elif isinstance(image, str) and image.startswith("http"):
|
68 |
+
try:
|
69 |
+
response = requests.get(image)
|
70 |
+
image = Image.open(BytesIO(response.content))
|
71 |
+
print(f"Fetched image from URL: {image}")
|
72 |
+
except Exception as e:
|
73 |
+
print(f"Error fetching image from URL: {e}")
|
74 |
+
return f"Error fetching image from URL: {e}"
|
75 |
+
|
76 |
+
# Try to load the image from a local file path
|
77 |
+
elif isinstance(image, str) and os.path.isfile(image):
|
78 |
+
try:
|
79 |
+
image = Image.open(image)
|
80 |
+
print(f"Loaded image from local path: {image}")
|
81 |
+
except Exception as e:
|
82 |
+
print(f"Error loading image from local path: {e}")
|
83 |
+
return f"Error loading image from local path: {e}"
|
84 |
+
|
85 |
+
# Validate that the image is correctly loaded
|
86 |
+
if not isinstance(image, Image.Image):
|
87 |
+
print("Invalid image format received.")
|
88 |
+
return "Invalid image format received."
|
89 |
|
90 |
# Apply transformations
|
91 |
image = transform(image).unsqueeze(0)
|
|
|
112 |
iface = gr.Interface(
|
113 |
fn=predict,
|
114 |
inputs=gr.Image(type="pil", label="Upload an image or provide a URL or local path"), # Input: Image, URL, or Local Path
|
115 |
+
outputs=gr.Textbox(label="Prediction Result"),
|
116 |
live=True,
|
117 |
title="Maize Anomaly Detection",
|
118 |
description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
|
119 |
)
|
120 |
|
121 |
+
# Launch the Gradio interface
|
122 |
+
iface.launch(share=True, show_error=True)
|