Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,44 +39,23 @@ transform = transforms.Compose([
|
|
| 39 |
])
|
| 40 |
|
| 41 |
# Prediction function for an uploaded image
|
| 42 |
-
def predict_from_image(
|
| 43 |
try:
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
logging.info("Received image for prediction")
|
| 50 |
-
|
| 51 |
-
# Apply transformations
|
| 52 |
-
image_tensor = transform(image).unsqueeze(0)
|
| 53 |
-
|
| 54 |
-
# Predict
|
| 55 |
-
with torch.no_grad():
|
| 56 |
-
outputs = model(image_tensor)
|
| 57 |
-
predicted_class = torch.argmax(outputs, dim=1).item()
|
| 58 |
-
|
| 59 |
-
# Interpret the result
|
| 60 |
-
if predicted_class == 0:
|
| 61 |
-
return {"result": "The photo is of fall army worm with problem ID 126."}
|
| 62 |
-
elif predicted_class == 1:
|
| 63 |
-
return {"result": "The photo is of a healthy maize image."}
|
| 64 |
-
else:
|
| 65 |
-
return {"error": "Unexpected class prediction."}
|
| 66 |
except Exception as e:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
live=True,
|
| 76 |
-
title="Maize Anomaly Detection",
|
| 77 |
-
description="Upload an image to detect anomalies in maize crops.",
|
| 78 |
)
|
| 79 |
|
| 80 |
-
# Launch the interface locally
|
| 81 |
if __name__ == "__main__":
|
| 82 |
-
|
|
|
|
| 39 |
])
|
| 40 |
|
| 41 |
# Prediction function for an uploaded image
|
| 42 |
+
def predict_from_image(image_url):
|
| 43 |
try:
|
| 44 |
+
# Download the image from the provided URL
|
| 45 |
+
response = requests.get(image_url)
|
| 46 |
+
image = Image.open(BytesIO(response.content))
|
| 47 |
+
# Process the image...
|
| 48 |
+
return {"result": "Image processed successfully"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
+
return {"error": str(e)}
|
| 51 |
+
|
| 52 |
+
demo = gr.Interface(
|
| 53 |
+
fn=predict_from_image,
|
| 54 |
+
inputs="text",
|
| 55 |
+
outputs="json",
|
| 56 |
+
title="Image Processing",
|
| 57 |
+
description="Enter a URL to an image",
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
|
|
|
| 60 |
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|