jays009 commited on
Commit
2eeccb6
·
verified ·
1 Parent(s): 163e73a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -39,7 +39,7 @@ transform = transforms.Compose([
39
 
40
  # Function to predict from image content
41
  def predict_from_image(image):
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.")
45
 
@@ -59,27 +59,30 @@ def predict_from_image(image):
59
  else:
60
  return {"error": "Unexpected class prediction."}
61
 
62
- # Function to predict from URL
63
- def predict_from_url(url):
64
  try:
65
- response = requests.get(url)
66
- response.raise_for_status() # Ensure the request was successful
67
- image = Image.open(BytesIO(response.content))
 
 
 
 
 
 
68
  return predict_from_image(image)
69
  except Exception as e:
70
- return {"error": f"Failed to process the URL: {str(e)}"}
71
 
72
  # Gradio interface
73
  iface = gr.Interface(
74
- fn=lambda image, url: predict_from_image(image) if image else predict_from_url(url),
75
- inputs=[
76
- gr.Image(type="pil", label="Upload an Image"),
77
- gr.Textbox(label="Or Enter an Image URL", placeholder="Provide a valid image URL"),
78
- ],
79
  outputs=gr.JSON(label="Prediction Result"),
80
  live=True,
81
  title="Maize Anomaly Detection",
82
- description="Upload an image or provide a URL to detect anomalies in maize crops.",
83
  )
84
 
85
  # Launch the interface
 
39
 
40
  # Function to predict from image content
41
  def predict_from_image(image):
42
+ print(f"Processing image: {image}")
43
  if not isinstance(image, Image.Image):
44
  raise ValueError("Invalid image format received. Please provide a valid image.")
45
 
 
59
  else:
60
  return {"error": "Unexpected class prediction."}
61
 
62
+ # Function to predict from path or URL
63
+ def predict_from_path_or_url(path_or_url):
64
  try:
65
+ if path_or_url.startswith("http://") or path_or_url.startswith("https://"):
66
+ response = requests.get(path_or_url)
67
+ response.raise_for_status() # Ensure the request was successful
68
+ image = Image.open(BytesIO(response.content))
69
+ elif os.path.isfile(path_or_url):
70
+ image = Image.open(path_or_url)
71
+ else:
72
+ return {"error": "Invalid path or URL. Please provide a valid URL or local file path."}
73
+
74
  return predict_from_image(image)
75
  except Exception as e:
76
+ return {"error": f"Failed to process the path or URL: {str(e)}"}
77
 
78
  # Gradio interface
79
  iface = gr.Interface(
80
+ fn=predict_from_image, # Adjust to handle images only
81
+ inputs=[gr.Image(type="pil", label="Upload an Image")],
 
 
 
82
  outputs=gr.JSON(label="Prediction Result"),
83
  live=True,
84
  title="Maize Anomaly Detection",
85
+ description="Upload an image to detect anomalies in maize crops.",
86
  )
87
 
88
  # Launch the interface