YOLOv8, developed by Ultralytics, continues the legacy of the highly popular YOLO (You Only Look Once) series. This version brings significant improvements in both speed and accuracy, making it a top choice for real-time object detection tasks. Its efficient CNN-based architecture is optimized for performance on both CPUs and GPUs.
This repository features a fine-tuned YOLOv8 Nano model specifically trained for Threat Detection, designed to identify four critical threat categories with high precision and speed.
YOLOv8n Threat Detection is a specialized computer vision model for security and surveillance. Leveraging the speed and efficiency of the YOLOv8 Nano architecture, this model accurately detects potential threats in real-time scenarios.
The threat categories are:
| Class ID | Threat Type | Description |
|---|---|---|
| 1 | Gun | Any type of firearm weapon including pistols, rifles, and other firearms |
| 2 | Explosive | Fire, explosion scenarios, and explosive devices |
| 3 | Grenade | Hand grenades and similar explosive devices |
| 4 | Knife | Bladed weapons including knives, daggers, and sharp objects |
The model was trained on a custom threat detection dataset, meticulously curated and annotated for robust performance across various scenarios.




| Metric | Gun | Explosive | Grenade | Knife | Overall |
|---|---|---|---|---|---|
| mAP@50:95 | 47.8% | 48.5% | 76.6% | 48.2% | 55.3% |
| mAP@50 | 78.3% | 74.1% | 92.1% | 80.9% | 81.3% |
| Precision | 83.3% | 77.8% | 96.5% | 79.7% | 84.3% |
| Recall | 69.0% | 68.2% | 89.9% | 78.1% | 76.3% |
| Metric | Gun | Explosive | Grenade | Knife | Overall |
|---|---|---|---|---|---|
| mAP@50:95 | 65.3% | 35.7% | 83.2% | 49.8% | 58.5% |
| mAP@50 | 93.1% | 60.5% | 91.1% | 79.7% | 81.1% |
| Precision | 96.7% | 49.7% | 93.1% | 86.5% | 81.5% |
| Recall | 83.0% | 83.0% | 83.0% | 83.0% | 83.0% |
best.pt - Main model weights !pip install ultralytics
# process video in batches
import cv2
from ultralytics import YOLO
from huggingface_hub import hf_hub_download
import torch
from tqdm import tqdm
# Configuration
MODEL_REPO = "Subh775/Threat-Detection-YOLOv8n"
INPUT_VIDEO = "input_video.mp4"
OUTPUT_VIDEO = "output_video.mp4"
CONFIDENCE_THRESHOLD = 0.4
BATCH_SIZE = 32 # Adjust based on GPU memory
# Setup device
device = 0 if torch.cuda.is_available() else "cpu"
print(f"Using device: {'GPU' if device == 0 else 'CPU'}")
# Load model
model_path = hf_hub_download(repo_id=MODEL_REPO, filename="weights/best.pt")
model = YOLO(model_path)
# Process video
cap = cv2.VideoCapture(INPUT_VIDEO)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(OUTPUT_VIDEO, fourcc, fps, (frame_width, frame_height))
frames_batch = []
with tqdm(total=total_frames, desc="Processing video") as pbar:
while cap.isOpened():
success, frame = cap.read()
if success:
frames_batch.append(frame)
if len(frames_batch) == BATCH_SIZE:
# Batch inference
results = model(frames_batch, conf=CONFIDENCE_THRESHOLD,
device=device, verbose=False)
# Write annotated frames
for result in results:
annotated_frame = result.plot()
out.write(annotated_frame)
pbar.update(len(frames_batch))
frames_batch = []
else:
break
# Process remaining frames
if frames_batch:
results = model(frames_batch, conf=CONFIDENCE_THRESHOLD,
device=device, verbose=False)
for result in results:
annotated_frame = result.plot()
out.write(annotated_frame)
pbar.update(len(frames_batch))
cap.release()
out.release()
print(f"Processed video saved to: {OUTPUT_VIDEO}")
Disclaimer: This model is for research and educational purposes. It should not be used for deployment in real-world security applications without further extensive validation.