jays009 commited on
Commit
170be68
·
verified ·
1 Parent(s): 95250f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -19
app.py CHANGED
@@ -6,7 +6,6 @@ from PIL import Image
6
  import requests
7
  import base64
8
  from io import BytesIO
9
- import os
10
 
11
  # Define the number of classes
12
  num_classes = 2 # Update with the actual number of classes in your dataset
@@ -34,23 +33,16 @@ transform = transforms.Compose([
34
  ])
35
 
36
  # Prediction function
37
- def process_image(data):
38
  try:
39
- # Check if the input contains a base64-encoded string
40
- if isinstance(data, dict):
41
- if "data" in data:
42
- # Base64 decoding
43
- image_data = base64.b64decode(data["data"])
44
- image = Image.open(BytesIO(image_data))
45
- elif "url" in data:
46
- # URL-based image loading
47
- response = requests.get(data["url"])
48
- image = Image.open(BytesIO(response.content))
49
- elif "path" in data:
50
- # Local path image loading
51
- image = Image.open(data["path"])
52
- else:
53
- return "Invalid input data structure."
54
 
55
  # Validate image
56
  if not isinstance(image, Image.Image):
@@ -77,11 +69,14 @@ def process_image(data):
77
  # Create the Gradio interface
78
  iface = gr.Interface(
79
  fn=process_image,
80
- inputs=gr.JSON(label="Upload an image (URL or Local Path)"), # Input: JSON to handle URL or path
 
 
 
81
  outputs=gr.Textbox(label="Prediction Result"), # Output: Prediction result
82
  live=True,
83
  title="Maize Anomaly Detection",
84
- description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
85
  )
86
 
87
  # Launch the Gradio interface
 
6
  import requests
7
  import base64
8
  from io import BytesIO
 
9
 
10
  # Define the number of classes
11
  num_classes = 2 # Update with the actual number of classes in your dataset
 
33
  ])
34
 
35
  # Prediction function
36
+ def process_image(image, image_url=None):
37
  try:
38
+ # Handle URL-based image loading
39
+ if image_url:
40
+ response = requests.get(image_url)
41
+ image = Image.open(BytesIO(response.content))
42
+
43
+ # Handle local file path image loading (Gradio File input)
44
+ elif isinstance(image, str) and os.path.isfile(image):
45
+ image = Image.open(image)
 
 
 
 
 
 
 
46
 
47
  # Validate image
48
  if not isinstance(image, Image.Image):
 
69
  # Create the Gradio interface
70
  iface = gr.Interface(
71
  fn=process_image,
72
+ inputs=[
73
+ gr.File(label="Upload an image (Local File Path)"), # Input: Local file
74
+ gr.Textbox(label="Enter Image URL", placeholder="Enter image URL here (optional)", optional=True) # Input: Image URL (optional)
75
+ ],
76
  outputs=gr.Textbox(label="Prediction Result"), # Output: Prediction result
77
  live=True,
78
  title="Maize Anomaly Detection",
79
+ description="Upload an image of maize to detect anomalies like disease or pest infestation. You can upload local images or provide an image URL."
80
  )
81
 
82
  # Launch the Gradio interface