File size: 1,674 Bytes
43a659b
534f8cc
a91485e
4e01a45
43a659b
 
 
e5a1bbe
43a659b
4e01a45
43a659b
 
4e01a45
 
534f8cc
 
4e01a45
534f8cc
4e01a45
 
1fbedbf
4e01a45
 
 
1fbedbf
534f8cc
4e01a45
534f8cc
4e01a45
 
534f8cc
4e01a45
 
534f8cc
43a659b
534f8cc
4e01a45
43a659b
 
 
 
4e01a45
 
 
 
 
 
 
43a659b
 
be1f96e
 
a91485e
1fbedbf
4e01a45
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from PIL import Image
import numpy as np
from ultralytics import YOLO
import gradio as gr

# Load the YOLO model
MODEL_URL = 'https://huggingface.co/ayoubsa/yolo_model/resolve/main/best.pt'
model = YOLO(MODEL_URL, cache_dir="/root/.cache")

# Define the prediction function for Gradio
def predict(image):
    try:
        # Convert PIL image to NumPy array
        image_array = np.array(image)

        # Perform prediction
        results = model(image_array)

        # Access the first result
        result = results[0]

        # Extract detected classes
        detected_classes = [model.names[int(cls)] for cls in result.boxes.cls]
        print(f"Detected classes: {detected_classes}")

        # Render bounding boxes on the image
        annotated_image = result.plot()

        # Convert the annotated image to PIL format
        output_image = Image.fromarray(annotated_image)

        # Return the annotated image and detected classes as output
        return output_image, detected_classes

    except Exception as e:
        print("Error during prediction:", str(e))
        return None, ["Error during processing"]

# Create the Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload an Image"),  # Input image as PIL
    outputs=[
        gr.Image(type="pil", label="Predicted Image with Bounding Boxes"),  # Annotated image
        gr.Label(label="Detected Classes")  # Detected classes
    ],
    title="YOLO Object Detection App",
    description="Upload an image, and the YOLO model will detect objects and annotate them with bounding boxes and class labels."
)

# Launch the Gradio app
iface.launch()