jays009 commited on
Commit
42edc6c
·
verified ·
1 Parent(s): 5cadf06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -38
app.py CHANGED
@@ -10,7 +10,7 @@ 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
14
 
15
  # Download model from Hugging Face
16
  def download_model():
@@ -48,40 +48,44 @@ transform = transforms.Compose([
48
  def predict(image):
49
  try:
50
  print(f"Received image input: {image}")
51
-
52
- # Check if the input contains a base64-encoded string
53
- if isinstance(image, dict) and image.get("data"):
54
- try:
55
- image_data = base64.b64decode(image["data"])
56
- image = Image.open(BytesIO(image_data))
57
- print(f"Decoded base64 image: {image}")
58
- except Exception as e:
59
- print(f"Error decoding base64 image: {e}")
60
- return f"Error decoding base64 image: {e}"
61
-
62
- # Check if the input is a URL
63
- elif isinstance(image, str) and image.startswith("http"):
64
- try:
65
- response = requests.get(image)
66
- image = Image.open(BytesIO(response.content))
67
- print(f"Fetched image from URL: {image}")
68
- except Exception as e:
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.")
84
- return "Invalid image format received."
 
 
 
 
85
 
86
  # Apply transformations
87
  image = transform(image).unsqueeze(0)
@@ -108,11 +112,11 @@ def predict(image):
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",
114
  description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
115
  )
116
 
117
- # Launch the Gradio interface
118
- iface.launch(share=True, show_error=True)
 
10
  import os
11
 
12
  # Define the number of classes
13
+ num_classes = 2
14
 
15
  # Download model from Hugging Face
16
  def download_model():
 
48
  def predict(image):
49
  try:
50
  print(f"Received image input: {image}")
51
+
52
+ # Check if the input is a PIL Image type
53
+ if isinstance(image, Image.Image):
54
+ print(f"Image is already loaded as PIL Image: {image}")
55
+ else:
56
+ # Try to handle base64-encoded image
57
+ if isinstance(image, dict) and image.get("data"):
58
+ try:
59
+ image_data = base64.b64decode(image["data"])
60
+ image = Image.open(BytesIO(image_data))
61
+ print(f"Decoded base64 image: {image}")
62
+ except Exception as e:
63
+ print(f"Error decoding base64 image: {e}")
64
+ return f"Error decoding base64 image: {e}"
65
+
66
+ # Try to fetch the image from a URL
67
+ elif isinstance(image, str) and image.startswith("http"):
68
+ try:
69
+ response = requests.get(image)
70
+ image = Image.open(BytesIO(response.content))
71
+ print(f"Fetched image from URL: {image}")
72
+ except Exception as e:
73
+ print(f"Error fetching image from URL: {e}")
74
+ return f"Error fetching image from URL: {e}"
75
+
76
+ # Try to load the image from a local file path
77
+ elif isinstance(image, str) and os.path.isfile(image):
78
+ try:
79
+ image = Image.open(image)
80
+ print(f"Loaded image from local path: {image}")
81
+ except Exception as e:
82
+ print(f"Error loading image from local path: {e}")
83
+ return f"Error loading image from local path: {e}"
84
+
85
+ # Validate that the image is correctly loaded
86
+ if not isinstance(image, Image.Image):
87
+ print("Invalid image format received.")
88
+ return "Invalid image format received."
89
 
90
  # Apply transformations
91
  image = transform(image).unsqueeze(0)
 
112
  iface = gr.Interface(
113
  fn=predict,
114
  inputs=gr.Image(type="pil", label="Upload an image or provide a URL or local path"), # Input: Image, URL, or Local Path
115
+ outputs=gr.Textbox(label="Prediction Result"),
116
  live=True,
117
  title="Maize Anomaly Detection",
118
  description="Upload an image of maize to detect anomalies like disease or pest infestation. You can provide local paths, URLs, or base64-encoded images."
119
  )
120
 
121
+ # Launch the Gradio interface
122
+ iface.launch(share=True, show_error=True)