File size: 932 Bytes
5720142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from ultralytics import YOLO

# Load YOLO model
model = YOLO('yolov8n.pt')

def detect_and_count_cars(image):
    results = model.predict(image, conf=0.25)
    detections = results[0].boxes.boxes.cpu().numpy()
    car_count = 0

    # Count cars
    for detection in detections:
        class_id = int(detection[5])
        if model.names[class_id] == 'car':
            car_count += 1

    annotated_image = results[0].plot()
    return annotated_image, f"Number of cars detected: {car_count}"

# Create Gradio interface
interface = gr.Interface(
    fn=detect_and_count_cars,
    inputs=gr.Image(type="numpy", label="Upload an Image"),
    outputs=[
        gr.Image(type="numpy", label="Annotated Image"),
        gr.Textbox(label="Car Count")
    ],
    title="Car Detection and Counting",
    description="Upload an image to detect and count cars using YOLOv8."
)

# Launch the interface
interface.launch()