Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -45,70 +45,53 @@ transform = transforms.Compose([
|
|
45 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
46 |
])
|
47 |
|
48 |
-
def
|
49 |
try:
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
image = Image.open(BytesIO(image_data))
|
57 |
-
print(f"Decoded base64 image: {image}")
|
58 |
-
except Exception as e:
|
59 |
-
print(f"Error decoding base64 image: {e}")
|
60 |
-
return f"Error decoding base64 image: {e}"
|
61 |
-
|
62 |
-
# Check if the input is a URL
|
63 |
-
elif isinstance(image, str) and image.startswith("http"):
|
64 |
-
try:
|
65 |
-
response = requests.get(image)
|
66 |
image = Image.open(BytesIO(response.content))
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
predicted_class = torch.argmax(outputs, dim=1).item()
|
95 |
-
print(f"Prediction output: {outputs}, Predicted class: {predicted_class}")
|
96 |
-
|
97 |
-
if predicted_class == 0:
|
98 |
-
return "The photo you've sent is of fall army worm with problem ID 126."
|
99 |
-
elif predicted_class == 1:
|
100 |
-
return "The photo you've sent is of a healthy maize image."
|
101 |
else:
|
102 |
-
return "
|
103 |
except Exception as e:
|
104 |
print(f"Error processing image: {e}")
|
105 |
return f"Error processing image: {e}"
|
106 |
|
107 |
# Create the Gradio interface
|
108 |
iface = gr.Interface(
|
109 |
-
fn=
|
110 |
-
inputs=gr.
|
111 |
-
outputs=gr.Textbox(label="Prediction Result"), # Output:
|
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."
|
|
|
45 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
46 |
])
|
47 |
|
48 |
+
def process_image(image_input):
|
49 |
try:
|
50 |
+
# Process the image input (URL, local file, or base64)
|
51 |
+
if isinstance(image_input, dict):
|
52 |
+
# Check if the input contains a URL
|
53 |
+
if image_input.get("url"):
|
54 |
+
image_url = image_input["url"]
|
55 |
+
response = requests.get(image_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
image = Image.open(BytesIO(response.content))
|
57 |
+
# Check if the input contains a file path
|
58 |
+
elif image_input.get("path"):
|
59 |
+
image_path = image_input["path"]
|
60 |
+
image = Image.open(image_path)
|
61 |
+
# Handle base64 if it's included
|
62 |
+
elif image_input.get("data"):
|
63 |
+
image_data = base64.b64decode(image_input["data"])
|
64 |
+
image = Image.open(BytesIO(image_data))
|
65 |
+
else:
|
66 |
+
return "Invalid input data format. Please provide a URL or path."
|
67 |
+
|
68 |
+
# Apply transformations
|
69 |
+
image = transform(image).unsqueeze(0)
|
70 |
+
image = image.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
|
71 |
+
|
72 |
+
# Make the prediction
|
73 |
+
with torch.no_grad():
|
74 |
+
outputs = model(image)
|
75 |
+
predicted_class = torch.argmax(outputs, dim=1).item()
|
76 |
+
|
77 |
+
# Return prediction result
|
78 |
+
if predicted_class == 0:
|
79 |
+
return "The photo you've sent is of fall army worm with problem ID 126."
|
80 |
+
elif predicted_class == 1:
|
81 |
+
return "The photo you've sent is of a healthy maize image."
|
82 |
+
else:
|
83 |
+
return "Unexpected class prediction."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
else:
|
85 |
+
return "Invalid input. Please provide a dictionary with 'url' or 'path'."
|
86 |
except Exception as e:
|
87 |
print(f"Error processing image: {e}")
|
88 |
return f"Error processing image: {e}"
|
89 |
|
90 |
# Create the Gradio interface
|
91 |
iface = gr.Interface(
|
92 |
+
fn=process_image,
|
93 |
+
inputs=gr.JSON(label="Upload an image (URL or Local Path)"), # Input: JSON to handle URL or path
|
94 |
+
outputs=gr.Textbox(label="Prediction Result"), # Output: Prediction result
|
95 |
live=True,
|
96 |
title="Maize Anomaly Detection",
|
97 |
description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
|