import gradio as gr import torch from ultralytics import YOLO from PIL import Image import numpy as np # Load your model model = YOLO('yolov8m_defence.pt') # Replace with your model path # Define class names (based on your 18 categories) class_names = { 0: "Cargo Aircraft", 1: "Commercial Aircraft", 2: "Drone", 3: "Fighter Jet", 4: "Fighter Plane", 5: "Helicopter", 6: "Light Aircraft", 7: "Missile", 8: "Truck", 9: "Car", 10: "Tank", 11: "Bus", 12: "Van", 13: "Cargo Ship", 14: "Yacht", 15: "Cruise Ship", 16: "Warship", 17: "Sailboat" } def predict(image): """Run inference on uploaded image""" if image is None: return None, "Please upload an image" # Run inference results = model(image) # Get the plotted image with bounding boxes annotated_image = results[0].plot() # Convert BGR to RGB for display annotated_image = annotated_image[..., ::-1] # Generate detection summary detections = results[0].boxes if len(detections) == 0: summary = "No objects detected" else: summary = f"Detected {len(detections)} objects:\n\n" for i, box in enumerate(detections): class_id = int(box.cls[0]) confidence = float(box.conf[0]) class_name = class_names.get(class_id, f"Class {class_id}") summary += f"• {class_name}: {confidence:.2%}\n" return annotated_image, summary # Create Gradio interface iface = gr.Interface( fn=predict, inputs=gr.Image(type="pil", label="Upload Image"), outputs=[ gr.Image(type="numpy", label="Detection Results"), gr.Textbox(label="Detection Summary", lines=10) ], title="YOLOv8m Defence Object Detection", description=""" Upload an image to detect military and civilian vehicles, aircraft, and ships. **Detectable Objects:** Aircraft (cargo, commercial, fighter, helicopter, etc.), Vehicles (car, truck, tank, bus, van), Ships (cargo, yacht, cruise, warship, sailboat), and specialized items (drone, missile). *Developed for DSTA Brainhack 2025 - TIL-AI Category* """, examples=[ ["examples/test1.jpg"], ["examples/test2.jpg"], ["examples/test3.jpg"], ["examples/test4.jpg"], ["examples/test5.jpg"], ], theme=gr.themes.Soft(), allow_flagging="never" ) if __name__ == "__main__": iface.launch()