Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -71,15 +71,91 @@ iface = gr.Interface(
|
|
71 |
outputs=gr.Image(type="pil", label="Detection Results"),
|
72 |
title="YOLOv8m Defence Object Detection",
|
73 |
description="""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
Upload images to detect military and civilian vehicles, aircraft, and ships using our fine-tuned YOLOv8m model.
|
|
|
|
|
|
|
75 |
|
76 |
**Detectable Objects (18 categories):** Aircraft (cargo, commercial, fighter, helicopter, etc.),
|
77 |
Vehicles (car, truck, tank, bus, van), Ships (cargo, yacht, cruise, warship, sailboat),
|
78 |
and specialized items (drone, missile).
|
79 |
|
80 |
-
**Note:** Running on
|
81 |
-
|
82 |
-
Developed for DSTA Brainhack 2025 - TIL-AI Category (Semi-Finalist)
|
83 |
""",
|
84 |
examples=[
|
85 |
["examples/test1.jpg", 0.25, 0.45],
|
@@ -94,6 +170,24 @@ iface = gr.Interface(
|
|
94 |
["examples/test10.jpg", 0.25, 0.45],
|
95 |
],
|
96 |
css=css,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
cache_examples=True
|
98 |
)
|
99 |
|
|
|
71 |
outputs=gr.Image(type="pil", label="Detection Results"),
|
72 |
title="YOLOv8m Defence Object Detection",
|
73 |
description="""
|
74 |
+
import gradio as gr
|
75 |
+
import PIL.Image as Image
|
76 |
+
from ultralytics import YOLO
|
77 |
+
import torch
|
78 |
+
import os
|
79 |
+
|
80 |
+
# Load model once at startup
|
81 |
+
print("Loading YOLOv8m Defence model...")
|
82 |
+
model = YOLO("yolov8m_defence.pt")
|
83 |
+
|
84 |
+
# Set device and optimize for CPU inference
|
85 |
+
if torch.cuda.is_available():
|
86 |
+
device = 'cuda'
|
87 |
+
print("Using GPU acceleration")
|
88 |
+
else:
|
89 |
+
device = 'cpu'
|
90 |
+
print("Using CPU inference")
|
91 |
+
# Optimize for CPU
|
92 |
+
torch.set_num_threads(2) # Limit threads for free tier
|
93 |
+
|
94 |
+
model.to(device)
|
95 |
+
|
96 |
+
def predict_image(img, conf_threshold, iou_threshold):
|
97 |
+
"""Predicts objects in an image using YOLOv8m Defence model with adjustable confidence and IOU thresholds."""
|
98 |
+
try:
|
99 |
+
results = model.predict(
|
100 |
+
source=img,
|
101 |
+
conf=conf_threshold,
|
102 |
+
iou=iou_threshold,
|
103 |
+
show_labels=True,
|
104 |
+
show_conf=True,
|
105 |
+
imgsz=640, # Keep original size for accuracy
|
106 |
+
verbose=False, # Reduce console output
|
107 |
+
device=device
|
108 |
+
)
|
109 |
+
|
110 |
+
for r in results:
|
111 |
+
im_array = r.plot()
|
112 |
+
im = Image.fromarray(im_array[..., ::-1])
|
113 |
+
|
114 |
+
return im
|
115 |
+
|
116 |
+
except Exception as e:
|
117 |
+
print(f"Error during prediction: {e}")
|
118 |
+
return img # Return original image if error occurs
|
119 |
+
|
120 |
+
# Custom CSS for font styling
|
121 |
+
css = """
|
122 |
+
body, .gradio-container {
|
123 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
|
124 |
+
}
|
125 |
+
.gr-button {
|
126 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
|
127 |
+
font-weight: 500 !important;
|
128 |
+
}
|
129 |
+
.gr-box h1 {
|
130 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
|
131 |
+
font-weight: 600 !important;
|
132 |
+
}
|
133 |
+
.gr-box p {
|
134 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
|
135 |
+
}
|
136 |
+
"""
|
137 |
+
|
138 |
+
# Create interface
|
139 |
+
iface = gr.Interface(
|
140 |
+
fn=predict_image,
|
141 |
+
inputs=[
|
142 |
+
gr.Image(type="pil", label="Upload Image"),
|
143 |
+
gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
|
144 |
+
gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
|
145 |
+
],
|
146 |
+
outputs=gr.Image(type="pil", label="Detection Results"),
|
147 |
+
title="🛡️ YOLOv8m Defence Object Detection",
|
148 |
+
description="""
|
149 |
Upload images to detect military and civilian vehicles, aircraft, and ships using our fine-tuned YOLOv8m model.
|
150 |
+
Developed for DSTA Brainhack 2025 - TIL-AI Category (Semi-Finalist).
|
151 |
+
|
152 |
+
**📋 Model Card:** [spencercdz/YOLOv8m_defence](https://huggingface.co/spencercdz/YOLOv8m_defence)
|
153 |
|
154 |
**Detectable Objects (18 categories):** Aircraft (cargo, commercial, fighter, helicopter, etc.),
|
155 |
Vehicles (car, truck, tank, bus, van), Ships (cargo, yacht, cruise, warship, sailboat),
|
156 |
and specialized items (drone, missile).
|
157 |
|
158 |
+
**Note:** Running on CPU - inference takes 20-40 seconds per image for full accuracy.
|
|
|
|
|
159 |
""",
|
160 |
examples=[
|
161 |
["examples/test1.jpg", 0.25, 0.45],
|
|
|
170 |
["examples/test10.jpg", 0.25, 0.45],
|
171 |
],
|
172 |
css=css,
|
173 |
+
cache_examples=True # Cache example results
|
174 |
+
)
|
175 |
+
|
176 |
+
if __name__ == "__main__":
|
177 |
+
iface.launch(share=True) """,
|
178 |
+
examples=[
|
179 |
+
["examples/test1.jpg", 0.25, 0.45],
|
180 |
+
["examples/test2.jpg", 0.25, 0.45],
|
181 |
+
["examples/test3.jpg", 0.25, 0.45],
|
182 |
+
["examples/test4.jpg", 0.25, 0.45],
|
183 |
+
["examples/test5.jpg", 0.25, 0.45],
|
184 |
+
["examples/test6.jpg", 0.25, 0.45],
|
185 |
+
["examples/test7.jpg", 0.25, 0.45],
|
186 |
+
["examples/test8.jpg", 0.25, 0.45],
|
187 |
+
["examples/test9.jpg", 0.25, 0.45],
|
188 |
+
["examples/test10.jpg", 0.25, 0.45],
|
189 |
+
],
|
190 |
+
css=css,
|
191 |
cache_examples=True
|
192 |
)
|
193 |
|