jays009 commited on
Commit
01a4ed7
·
verified ·
1 Parent(s): 40efeb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -1
app.py CHANGED
@@ -7,6 +7,7 @@ from PIL import Image
7
  import requests
8
  import base64
9
  from io import BytesIO
 
10
 
11
  # Define the number of classes
12
  num_classes = 2 # Update with the actual number of classes in your dataset
@@ -68,6 +69,15 @@ def predict(image):
68
  print(f"Error fetching image from URL: {e}")
69
  return f"Error fetching image from URL: {e}"
70
 
 
 
 
 
 
 
 
 
 
71
  # Validate that the image is correctly loaded
72
  if not isinstance(image, Image.Image):
73
  print("Invalid image format received.")
@@ -97,7 +107,7 @@ def predict(image):
97
  # Create the Gradio interface
98
  iface = gr.Interface(
99
  fn=predict,
100
- inputs=gr.Image(type="pil", label="Upload an image or provide a URL"), # Input: Image or URL
101
  outputs=gr.Textbox(label="Prediction Result"), # Output: Predicted class
102
  live=True,
103
  title="Maize Anomaly Detection",
 
7
  import requests
8
  import base64
9
  from io import BytesIO
10
+ import os
11
 
12
  # Define the number of classes
13
  num_classes = 2 # Update with the actual number of classes in your dataset
 
69
  print(f"Error fetching image from URL: {e}")
70
  return f"Error fetching image from URL: {e}"
71
 
72
+ # Check if the input is a local file path
73
+ elif isinstance(image, str) and os.path.isfile(image):
74
+ try:
75
+ image = Image.open(image)
76
+ print(f"Loaded image from local path: {image}")
77
+ except Exception as e:
78
+ print(f"Error loading image from local path: {e}")
79
+ return f"Error loading image from local path: {e}"
80
+
81
  # Validate that the image is correctly loaded
82
  if not isinstance(image, Image.Image):
83
  print("Invalid image format received.")
 
107
  # Create the Gradio interface
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"), # Output: Predicted class
112
  live=True,
113
  title="Maize Anomaly Detection",