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()