ayoubsa commited on
Commit
534f8cc
·
verified ·
1 Parent(s): a91485e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -160,6 +160,7 @@ iface = gr.Interface(
160
  iface.launch()"""
161
 
162
  from PIL import Image
 
163
  import gradio as gr
164
  from ultralytics import YOLO
165
 
@@ -170,19 +171,30 @@ model = YOLO(MODEL_URL)
170
  # Define the prediction function
171
  def predict(image):
172
  try:
173
- print("Received image:", type(image)) # Check the type of the received image (should be PIL)
174
- results = model(image) # Perform object detection using YOLO
175
-
176
- # Print details of the results object
177
- print("Results:", results) # Check the contents of results
178
-
179
- results.render() # Render bounding boxes on the image
180
-
181
- output_image = Image.fromarray(results.imgs[0]) # Convert to PIL image
182
- print("Predicted image type:", type(output_image)) # Ensure output is PIL Image
 
 
 
 
 
 
 
 
 
 
183
  return output_image
 
184
  except Exception as e:
185
- print("Error during prediction:", e)
186
  return None
187
 
188
  # Create the Gradio interface
 
160
  iface.launch()"""
161
 
162
  from PIL import Image
163
+ import numpy as np
164
  import gradio as gr
165
  from ultralytics import YOLO
166
 
 
171
  # Define the prediction function
172
  def predict(image):
173
  try:
174
+ # Debugging: Check image type
175
+ print("Input image type:", type(image))
176
+
177
+ # Ensure input is converted to a format the YOLO model supports
178
+ if isinstance(image, Image.Image): # If PIL image, convert to NumPy
179
+ image = np.array(image) # Convert to NumPy array
180
+ print("Converted image to NumPy array.")
181
+
182
+ # Perform prediction
183
+ results = model.predict(image) # Perform object detection
184
+ print("YOLO prediction completed.")
185
+
186
+ # Render bounding boxes on the image
187
+ results.render()
188
+ print("Bounding boxes rendered.")
189
+
190
+ # Convert back to PIL for output
191
+ output_image = Image.fromarray(results.imgs[0])
192
+ print("Output image type:", type(output_image))
193
+
194
  return output_image
195
+
196
  except Exception as e:
197
+ print("Error during prediction:", str(e))
198
  return None
199
 
200
  # Create the Gradio interface