ayoubsa commited on
Commit
4e01a45
·
verified ·
1 Parent(s): 1fbedbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -29
app.py CHANGED
@@ -158,63 +158,59 @@ iface = gr.Interface(
158
 
159
  # Launch the Gradio app
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
 
167
  # Load the YOLO model
168
  MODEL_URL = 'https://huggingface.co/ayoubsa/yolo_model/resolve/main/best.pt'
169
  model = YOLO(MODEL_URL)
170
 
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
- # Print detected class names
187
- detected_classes = results.names # Get class names for each detected object
188
- print("Detected classes:", detected_classes)
189
 
190
- # Print the names of the detected classes
191
- for i, name in enumerate(detected_classes):
192
- print(f"Detected object {i+1}: {name}")
193
 
194
  # Render bounding boxes on the image
195
- results.render()
196
- print("Bounding boxes rendered.")
197
 
198
- # Convert back to PIL for output
199
- output_image = Image.fromarray(results.imgs[0])
200
- print("Output image type:", type(output_image))
201
 
202
- return output_image
 
203
 
204
  except Exception as e:
205
  print("Error during prediction:", str(e))
206
- return None
207
 
208
  # Create the Gradio interface
209
  iface = gr.Interface(
210
  fn=predict,
211
- inputs=gr.Image(type="pil", label="Upload an Image"), # Upload input as PIL Image
212
- outputs=gr.Image(type="pil", label="Predicted Image with Bounding Boxes"), # Output image
213
- title="Object Detection App",
214
- description="Upload an image, and the YOLO model will detect objects in it."
 
 
 
215
  )
216
 
217
  # Launch the Gradio app
218
  iface.launch()
219
 
220
 
 
 
 
158
 
159
  # Launch the Gradio app
160
  iface.launch()"""
 
161
  from PIL import Image
162
  import numpy as np
 
163
  from ultralytics import YOLO
164
+ import gradio as gr
165
 
166
  # Load the YOLO model
167
  MODEL_URL = 'https://huggingface.co/ayoubsa/yolo_model/resolve/main/best.pt'
168
  model = YOLO(MODEL_URL)
169
 
170
+ # Define the prediction function for Gradio
171
  def predict(image):
172
  try:
173
+ # Convert PIL image to NumPy array
174
+ image_array = np.array(image)
 
 
 
 
 
175
 
176
  # Perform prediction
177
+ results = model(image_array)
 
178
 
179
+ # Access the first result
180
+ result = results[0]
 
181
 
182
+ # Extract detected classes
183
+ detected_classes = [model.names[int(cls)] for cls in result.boxes.cls]
184
+ print(f"Detected classes: {detected_classes}")
185
 
186
  # Render bounding boxes on the image
187
+ annotated_image = result.plot()
 
188
 
189
+ # Convert the annotated image to PIL format
190
+ output_image = Image.fromarray(annotated_image)
 
191
 
192
+ # Return the annotated image and detected classes as output
193
+ return output_image, detected_classes
194
 
195
  except Exception as e:
196
  print("Error during prediction:", str(e))
197
+ return None, ["Error during processing"]
198
 
199
  # Create the Gradio interface
200
  iface = gr.Interface(
201
  fn=predict,
202
+ inputs=gr.Image(type="pil", label="Upload an Image"), # Input image as PIL
203
+ outputs=[
204
+ gr.Image(type="pil", label="Predicted Image with Bounding Boxes"), # Annotated image
205
+ gr.Label(label="Detected Classes") # Detected classes
206
+ ],
207
+ title="YOLO Object Detection App",
208
+ description="Upload an image, and the YOLO model will detect objects and annotate them with bounding boxes and class labels."
209
  )
210
 
211
  # Launch the Gradio app
212
  iface.launch()
213
 
214
 
215
+
216
+