spencercdz commited on
Commit
f891951
·
verified ·
1 Parent(s): 30f21bc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from ultralytics import YOLO
4
+ from PIL import Image
5
+ import numpy as np
6
+
7
+ # Load your model
8
+ model = YOLO('yolov8m_defence.pt') # Replace with your model path
9
+
10
+ # Define class names (based on your 18 categories)
11
+ class_names = {
12
+ 0: "Cargo Aircraft", 1: "Commercial Aircraft", 2: "Drone", 3: "Fighter Jet",
13
+ 4: "Fighter Plane", 5: "Helicopter", 6: "Light Aircraft", 7: "Missile",
14
+ 8: "Truck", 9: "Car", 10: "Tank", 11: "Bus", 12: "Van",
15
+ 13: "Cargo Ship", 14: "Yacht", 15: "Cruise Ship", 16: "Warship", 17: "Sailboat"
16
+ }
17
+
18
+ def predict(image):
19
+ """Run inference on uploaded image"""
20
+ if image is None:
21
+ return None, "Please upload an image"
22
+
23
+ # Run inference
24
+ results = model(image)
25
+
26
+ # Get the plotted image with bounding boxes
27
+ annotated_image = results[0].plot()
28
+
29
+ # Convert BGR to RGB for display
30
+ annotated_image = annotated_image[..., ::-1]
31
+
32
+ # Generate detection summary
33
+ detections = results[0].boxes
34
+ if len(detections) == 0:
35
+ summary = "No objects detected"
36
+ else:
37
+ summary = f"Detected {len(detections)} objects:\n\n"
38
+ for i, box in enumerate(detections):
39
+ class_id = int(box.cls[0])
40
+ confidence = float(box.conf[0])
41
+ class_name = class_names.get(class_id, f"Class {class_id}")
42
+ summary += f"• {class_name}: {confidence:.2%}\n"
43
+
44
+ return annotated_image, summary
45
+
46
+ # Create Gradio interface
47
+ iface = gr.Interface(
48
+ fn=predict,
49
+ inputs=gr.Image(type="pil", label="Upload Image"),
50
+ outputs=[
51
+ gr.Image(type="numpy", label="Detection Results"),
52
+ gr.Textbox(label="Detection Summary", lines=10)
53
+ ],
54
+ title="🛡️ YOLOv8m Defence Object Detection",
55
+ description="""
56
+ Upload an image to detect military and civilian vehicles, aircraft, and ships.
57
+
58
+ **Detectable Objects:** Aircraft (cargo, commercial, fighter, helicopter, etc.),
59
+ Vehicles (car, truck, tank, bus, van), Ships (cargo, yacht, cruise, warship, sailboat),
60
+ and specialized items (drone, missile).
61
+
62
+ *Developed for DSTA Brainhack 2025 - TIL-AI Category*
63
+ """,
64
+ examples=[
65
+ ["example1.jpg"], # Add your example images
66
+ ["example2.jpg"],
67
+ ["example3.jpg"],
68
+ ],
69
+ theme=gr.themes.Soft(),
70
+ allow_flagging="never"
71
+ )
72
+
73
+ if __name__ == "__main__":
74
+ iface.launch()