Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -39,6 +39,9 @@ transform = transforms.Compose([
|
|
39 |
# Function to predict from image content
|
40 |
def predict_from_image(image):
|
41 |
try:
|
|
|
|
|
|
|
42 |
# Ensure the image is a PIL Image
|
43 |
if not isinstance(image, Image.Image):
|
44 |
raise ValueError("Invalid image format received. Please provide a valid image.")
|
@@ -53,41 +56,51 @@ def predict_from_image(image):
|
|
53 |
|
54 |
# Interpret the result
|
55 |
if predicted_class == 0:
|
56 |
-
return {"
|
57 |
elif predicted_class == 1:
|
58 |
-
return {"
|
59 |
else:
|
60 |
-
return {"
|
|
|
61 |
except Exception as e:
|
62 |
-
|
|
|
63 |
|
64 |
# Function to predict from URL
|
65 |
def predict_from_url(url):
|
66 |
try:
|
67 |
-
|
68 |
-
raise ValueError("Invalid URL format. Please provide a valid image URL.")
|
69 |
-
|
70 |
response = requests.get(url)
|
71 |
response.raise_for_status() # Ensure the request was successful
|
72 |
image = Image.open(BytesIO(response.content))
|
|
|
73 |
return predict_from_image(image)
|
74 |
except Exception as e:
|
75 |
-
|
|
|
76 |
|
77 |
-
#
|
78 |
-
def
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
# Gradio interface
|
89 |
iface = gr.Interface(
|
90 |
-
fn=
|
91 |
inputs=[
|
92 |
gr.Image(type="pil", label="Upload an Image"),
|
93 |
gr.Textbox(label="Or Enter an Image URL", placeholder="Provide a valid image URL"),
|
@@ -96,9 +109,6 @@ iface = gr.Interface(
|
|
96 |
live=True,
|
97 |
title="Maize Anomaly Detection",
|
98 |
description="Upload an image or provide a URL to detect anomalies in maize crops.",
|
99 |
-
examples=[
|
100 |
-
[None, "https://example.com/sample-image.jpg"], # Replace with a valid example URL
|
101 |
-
]
|
102 |
)
|
103 |
|
104 |
# Launch the interface
|
|
|
39 |
# Function to predict from image content
|
40 |
def predict_from_image(image):
|
41 |
try:
|
42 |
+
# Log the image processing
|
43 |
+
print(f"Processing image: {image}")
|
44 |
+
|
45 |
# Ensure the image is a PIL Image
|
46 |
if not isinstance(image, Image.Image):
|
47 |
raise ValueError("Invalid image format received. Please provide a valid image.")
|
|
|
56 |
|
57 |
# Interpret the result
|
58 |
if predicted_class == 0:
|
59 |
+
return {"result": "The photo is of fall army worm with problem ID 126."}
|
60 |
elif predicted_class == 1:
|
61 |
+
return {"result": "The photo is of a healthy maize image."}
|
62 |
else:
|
63 |
+
return {"error": "Unexpected class prediction."}
|
64 |
+
|
65 |
except Exception as e:
|
66 |
+
print(f"Error during image processing: {e}")
|
67 |
+
return {"error": str(e)}
|
68 |
|
69 |
# Function to predict from URL
|
70 |
def predict_from_url(url):
|
71 |
try:
|
72 |
+
# Fetch the image from the URL
|
|
|
|
|
73 |
response = requests.get(url)
|
74 |
response.raise_for_status() # Ensure the request was successful
|
75 |
image = Image.open(BytesIO(response.content))
|
76 |
+
print(f"Fetched image from URL: {url}")
|
77 |
return predict_from_image(image)
|
78 |
except Exception as e:
|
79 |
+
print(f"Error during URL processing: {e}")
|
80 |
+
return {"error": f"Failed to process the URL: {str(e)}"}
|
81 |
|
82 |
+
# Gradio interface with logging
|
83 |
+
def predict(image, url):
|
84 |
+
try:
|
85 |
+
if image:
|
86 |
+
result = predict_from_image(image)
|
87 |
+
elif url:
|
88 |
+
result = predict_from_url(url)
|
89 |
+
else:
|
90 |
+
result = {"error": "No input provided. Please upload an image or provide a URL."}
|
91 |
+
|
92 |
+
# Log the result
|
93 |
+
event_id = id(result) # Generate a simple event ID for logging purposes
|
94 |
+
print(f"Event ID: {event_id}, Result: {result}")
|
95 |
+
return result
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
print(f"Error in prediction function: {e}")
|
99 |
+
return {"error": str(e)}
|
100 |
|
101 |
+
# Gradio interface setup
|
102 |
iface = gr.Interface(
|
103 |
+
fn=predict,
|
104 |
inputs=[
|
105 |
gr.Image(type="pil", label="Upload an Image"),
|
106 |
gr.Textbox(label="Or Enter an Image URL", placeholder="Provide a valid image URL"),
|
|
|
109 |
live=True,
|
110 |
title="Maize Anomaly Detection",
|
111 |
description="Upload an image or provide a URL to detect anomalies in maize crops.",
|
|
|
|
|
|
|
112 |
)
|
113 |
|
114 |
# Launch the interface
|