Update app.py
Browse files
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 |
-
#
|
175 |
-
|
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
|
184 |
-
print("YOLO prediction completed.")
|
185 |
|
186 |
-
#
|
187 |
-
|
188 |
-
print("Detected classes:", detected_classes)
|
189 |
|
190 |
-
#
|
191 |
-
for
|
192 |
-
|
193 |
|
194 |
# Render bounding boxes on the image
|
195 |
-
|
196 |
-
print("Bounding boxes rendered.")
|
197 |
|
198 |
-
# Convert
|
199 |
-
output_image = Image.fromarray(
|
200 |
-
print("Output image type:", type(output_image))
|
201 |
|
202 |
-
|
|
|
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"), #
|
212 |
-
outputs=
|
213 |
-
|
214 |
-
|
|
|
|
|
|
|
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 |
+
|