The paradigm has shifted! While CNNs traditionally dominated object detection with faster inference times, RF-DETR (Roboflow's Detection Transformer) has revolutionized the field. This transformer-based architecture not only outperforms CNNs in accuracy but also delivers faster inference for real-time applications.
Our custom threat detection dataset was meticulously curated and annotated to ensure robust model performance across diverse scenarios.
The model is trained to detect threats across various scales, from small concealed weapons to larger explosive devices.
-
You can use:
video_processing.py to process large videos
-
Below is the script to process a single image
1import numpy as np
2import supervision as sv
3import torch
4import requests
5from PIL import Image
6import os
7
8from rfdetr import RFDETRNano
9
10THREAT_CLASSES = {
11 1: "Gun",
12 2: "Explosive",
13 3: "Grenade",
14 4: "Knife"
15}
16
17image = Image.open("Path_to_image")
18
19# pre-trained weights
20weights_url = "https://huggingface.co/Subh775/Threat-Detection-RFDETR/resolve/main/checkpoint_best_total.pth"
21weights_filename = "checkpoint_best_total.pth"
22
23# Download weights if not already present
24if not os.path.exists(weights_filename):
25 print(f"Downloading weights from {weights_url}")
26 response = requests.get(weights_url, stream=True)
27 response.raise_for_status()
28 with open(weights_filename, 'wb') as f:
29 for chunk in response.iter_content(chunk_size=8192):
30 f.write(chunk)
31 print("Download complete.")
32
33model = RFDETRNano(resolution=640, pretrain_weights=weights_filename)
34model.optimize_for_inference()
35
36detections = model.predict(image, threshold=0.5)
37
38color = sv.ColorPalette.from_hex([
39 "#1E90FF", "#32CD32", "#FF0000", "#FF8C00"
40])
41
42text_scale = sv.calculate_optimal_text_scale(resolution_wh=image.size)
43thickness = sv.calculate_optimal_line_thickness(resolution_wh=image.size)
44
45bbox_annotator = sv.BoxAnnotator(color=color, thickness=thickness)
46label_annotator = sv.LabelAnnotator(
47 color=color,
48 text_color=sv.Color.BLACK,
49 text_scale=text_scale,
50 smart_position=True
51)
52
53labels = []
54for class_id, confidence in zip(detections.class_id, detections.confidence):
55 class_name = THREAT_CLASSES.get(class_id, f"unknown_class_{class_id}")
56 labels.append(f"{class_name} {confidence:.2f}")
57
58annotated_image = image.copy()
59annotated_image = bbox_annotator.annotate(annotated_image, detections)
60annotated_image = label_annotator.annotate(annotated_image, detections, labels)
61annotated_image.thumbnail((800, 800))
62annotated_image