ayoubsa commited on
Commit
43a659b
·
verified ·
1 Parent(s): f0d7864

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -1
app.py CHANGED
@@ -128,7 +128,7 @@ iface = gr.Interface(
128
  )
129
 
130
  # Launch the Gradio app
131
- iface.launch()"""
132
 
133
  import gradio as gr
134
  import torch
@@ -156,5 +156,41 @@ iface = gr.Interface(
156
  description="Upload an image, and the YOLO model will detect objects in it."
157
  )
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  # Launch the Gradio app
160
  iface.launch()
 
128
  )
129
 
130
  # Launch the Gradio app
131
+ iface.launch()
132
 
133
  import gradio as gr
134
  import torch
 
156
  description="Upload an image, and the YOLO model will detect objects in it."
157
  )
158
 
159
+ # Launch the Gradio app
160
+ iface.launch()"""
161
+
162
+ from PIL import Image
163
+ import numpy as np
164
+ from ultralytics import YOLO
165
+ import gradio as gr
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
+ print("Received image:", type(image)) # Check the type of the received image
175
+ results = model(image) # Perform object detection using YOLO
176
+ results.render() # Render bounding boxes on the image
177
+
178
+ output_image = Image.fromarray(results.imgs[0]) # Convert to PIL image
179
+
180
+ print("Predicted image:", type(output_image)) # Ensure output is PIL Image
181
+ return output_image
182
+ except Exception as e:
183
+ print("Error during prediction:", e)
184
+ return None
185
+
186
+ # Create the Gradio interface
187
+ iface = gr.Interface(
188
+ fn=predict,
189
+ inputs=gr.Image(type="pil", label="Upload an Image"), # Upload input as PIL Image
190
+ outputs=gr.Image(type="pil", label="Predicted Image with Bounding Boxes"), # Output image
191
+ title="Object Detection App",
192
+ description="Upload an image, and the YOLO model will detect objects in it."
193
+ )
194
+
195
  # Launch the Gradio app
196
  iface.launch()