Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,3 @@
|
|
| 1 |
-
"""import gradio as gr
|
| 2 |
-
from ultralytics import YOLO
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import numpy as np
|
| 5 |
-
|
| 6 |
-
# Load the YOLO model
|
| 7 |
-
MODEL_URL = "https://huggingface.co/ayoubsa/yolo_model/resolve/main/best.pt"
|
| 8 |
-
model = YOLO(MODEL_URL)
|
| 9 |
-
|
| 10 |
-
# Define the prediction function
|
| 11 |
-
def predict(input_img):
|
| 12 |
-
try:
|
| 13 |
-
# Convert PIL Image to NumPy array
|
| 14 |
-
image_array = np.array(input_img)
|
| 15 |
-
|
| 16 |
-
# Perform inference
|
| 17 |
-
results = model(image_array)
|
| 18 |
-
|
| 19 |
-
# Extract detected class names
|
| 20 |
-
detected_classes = [model.names[int(cls)] for cls in results[0].boxes.cls]
|
| 21 |
-
|
| 22 |
-
# Render results on the image
|
| 23 |
-
results[0].plot() # Render bounding boxes on the image
|
| 24 |
-
output_image = Image.fromarray(results[0].orig_img)
|
| 25 |
-
|
| 26 |
-
return output_image, {cls: 1.0 for cls in detected_classes} # Dummy scores for simplicity
|
| 27 |
-
except Exception as e:
|
| 28 |
-
print(f"Error during processing: {e}")
|
| 29 |
-
return None, {"Error": str(e)}
|
| 30 |
-
|
| 31 |
-
# Gradio app configuration
|
| 32 |
-
gradio_app = gr.Interface(
|
| 33 |
-
predict,
|
| 34 |
-
inputs=gr.Image(label="Upload an Image", type="pil"),
|
| 35 |
-
outputs=[
|
| 36 |
-
gr.Image(label="Predicted Image with Bounding Boxes"), # Rendered image with bounding boxes
|
| 37 |
-
gr.Label(label="Detected Classes"), # Detected class names
|
| 38 |
-
],
|
| 39 |
-
title="YOLO Object Detection App",
|
| 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 |
-
|
| 47 |
import gradio as gr
|
| 48 |
from ultralytics import YOLO
|
| 49 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|