This project is a computer vision system designed to detect weapons in video streams or files. It is intended to enhance public safety by identifying dangerous objects such as firearms in real-time or pre-recorded surveillance footage.
The system can detect the following:
- Primary Weapon Classes (Required)
- π« Firearms: handguns, pistols, rifles.
- β No Weapon: normal or safe scenarios.
Note: The system is trained to minimize false alarms from harmless objects, such as toys
- Purpose: Processes a video and produces an output video with red bounding boxes around detected weapons.
- How it works:
- Loads the trained Faster R-CNN model (
detector_epochX.pth). - Reads a video file frame by frame.
- Runs object detection on each frame.
- Draws bounding boxes and class labels on detected weapons.
- Saves the resulting video as
.avior.mp4.
- Loads the trained Faster R-CNN model (
- Configurable paths:
input_video_path = "input_video.mp4" # Set your original video here output_video_path = "output.avi" # Video with weapon detections checkpoint_path = "models/detector_epoch3.pth" # Trained model path
-
Run Command:
python src/video_inference.py
-
Purpose: Extracts frames from a video where weapons are detected, and saves them as images with bounding boxes applied.
-
How it works:
- Loads the trained Faster R-CNN model.
- Reads the input video frame by frame.
- For frames with weapons detected above a confidence threshold, saves the frame to a folder.
- Names the frames with the timestamp in seconds where the weapon appears.
-
Configurable paths:
input_video_path = "input_video.mp4" # Original video path frames_output_dir = "weapon_frames/" # Folder to save extracted frames checkpoint_path = "models/detector_epoch3.pth" # Trained model
-
Run Command:
python src/extract_weapon_frames.py
-
Output: Each frame image contains red boxes around weapons and is named as
frame_XXs.jpg.
-
Purpose: Train the Faster R-CNN model on your annotated dataset.
-
How it works:
- Reads images and JSON annotations from
data/trainanddata/val. - Creates a PyTorch dataset using
JsonDetectionDataset. - Trains a Faster-RCNN model.
- Saves checkpoints periodically and after each epoch.
- Reads images and JSON annotations from
-
Configurable paths & params:
python src/train_detector.py --data-dir data --epochs 5 --batch-size 2 --device cuda
-
Resume Training from Checkpoint:
python src/train_detector.py --data-dir data --epochs 5 --batch-size 2 --device cuda --resume models/checkpoint_epoch2_batch1000.pth
# Clone the repository
git clone https://github.com/Pushkkaarr/weapon-detection-system.git
# Navigate to the project folder
cd weapon-detection
# Install dependencies
pip install -r requirements.txtweapon-detection/
β
ββ src/
β ββ train_detector.py # Training script
β ββ video_inference.py # Weapon detection on videos
β ββ extract_weapon_frames.py # Extract frames with weapons
β ββ evaluate_model.py # Evaluate accuracy
β ββ dataset.py # JsonDetectionDataset class
β
ββ data/
β ββ train/images/ # Training images
β ββ train/labels/ # JSON annotations
β ββ val/images/ # Validation images
β ββ val/labels/ # JSON annotations
β
ββ models/ # Saved model checkpoints
ββ weapon_frames/ # Extracted frames with weapons
ββ README.md
- Videos: Change
input_video_pathinvideo_inference.pyandextract_weapon_frames.pyto the video you want to process. - Output folder: Change
output_video_pathorframes_output_dirin the scripts. - Trained Model: Ensure the correct checkpoint is loaded (
detector_epochX.pth).
-
Train the model (optional if already trained):
python src/train_detector.py --data-dir data --epochs 5 --batch-size 2 --device cuda
-
Evaluate model accuracy:
python src/evaluate_model.py
-
Detect weapons in video:
python src/video_inference.py
-
Extract frames with detected weapons:
python src/extract_weapon_frames.py
- Video Showing the video processing and actual execution of model: (Video is compressed , hence low quality)
output.compress-video-online.com.mp4
- Weapon Detection Video: Saved at
output.avior your configured path invideo_inference.py.
output.mp4
- Weapon Frames: Saved in
weapon_frames/with names likeframe_12s.jpgfor frame at 12 seconds where a weapon is detected. -
1. Training vs Validation Loss
The modelβs error values on the training dataset (Train Loss) and on unseen validation data (Validation Loss) plotted across all training epochs. This illustrates how the modelβs prediction error evolves over time for both known and unseen samples.

2. Precision Over Epochs
The ratio of correctly detected weapons to the total number of detections made by the model, displayed over epochs. This indicates the consistency of correct detections relative to all predicted detections as the training progresses.

3. Recall Over Epochs
The proportion of actual weapons in the dataset that are correctly detected by the model, tracked across training epochs. This shows how effectively the model identifies all relevant instances throughout training.

4. F1 Score Over Epochs
The harmonic mean of Precision and Recall measured across epochs. This metric combines the balance between correct detections and coverage of actual weapons over the course of training.

5. Confusion Matrix A 2Γ2 tabular representation of classification results, showing the distribution of true positives, false negatives, false positives, and true negatives across all predictions.
6. mAP (Mean Average Precision) Over Epochs
A single aggregated metric representing both detection accuracy and localization precision of weapons across epochs. mAP captures the modelβs performance in predicting correct classes and accurately placing bounding boxes over the course of training.