Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
|
@@ -40,5 +40,51 @@ gradio_app = gr.Interface(
|
|
| 40 |
description="Upload an image, and the YOLO model will detect objects in it.",
|
| 41 |
)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if __name__ == "__main__":
|
| 44 |
gradio_app.launch()
|
|
|
|
|
|
| 1 |
+
"""import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 40 |
description="Upload an image, and the YOLO model will detect objects in it.",
|
| 41 |
)
|
| 42 |
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
gradio_app.launch()"""
|
| 45 |
+
|
| 46 |
+
import gradio as gr
|
| 47 |
+
from ultralytics import YOLO
|
| 48 |
+
from PIL import Image
|
| 49 |
+
import numpy as np
|
| 50 |
+
|
| 51 |
+
# Load the YOLO model
|
| 52 |
+
MODEL_URL = "https://huggingface.co/ayoubsa/yolo_model/resolve/main/best.pt"
|
| 53 |
+
model = YOLO(MODEL_URL)
|
| 54 |
+
|
| 55 |
+
# Define the prediction function
|
| 56 |
+
def predict(input_img):
|
| 57 |
+
try:
|
| 58 |
+
# Convert PIL Image to NumPy array
|
| 59 |
+
image_array = np.array(input_img)
|
| 60 |
+
|
| 61 |
+
# Perform inference
|
| 62 |
+
results = model(image_array)
|
| 63 |
+
|
| 64 |
+
# Extract detected class names
|
| 65 |
+
detected_classes = [model.names[int(cls)] for cls in results[0].boxes.cls]
|
| 66 |
+
|
| 67 |
+
# Render results on the image
|
| 68 |
+
rendered_image = results[0].plot() # This method returns the image with bounding boxes
|
| 69 |
+
output_image = Image.fromarray(rendered_image) # Convert the rendered image to a PIL Image
|
| 70 |
+
|
| 71 |
+
return output_image, {cls: 1.0 for cls in detected_classes} # Dummy scores for simplicity
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"Error during processing: {e}")
|
| 74 |
+
return None, {"Error": str(e)}
|
| 75 |
+
|
| 76 |
+
# Gradio app configuration
|
| 77 |
+
gradio_app = gr.Interface(
|
| 78 |
+
predict,
|
| 79 |
+
inputs=gr.Image(label="Upload an Image", type="pil"),
|
| 80 |
+
outputs=[
|
| 81 |
+
gr.Image(label="Predicted Image with Bounding Boxes"), # Rendered image with bounding boxes
|
| 82 |
+
gr.Label(label="Detected Classes"), # Detected class names
|
| 83 |
+
],
|
| 84 |
+
title="YOLO Object Detection App",
|
| 85 |
+
description="Upload an image, and the YOLO model will detect objects in it.",
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
if __name__ == "__main__":
|
| 89 |
gradio_app.launch()
|
| 90 |
+
|