By using Ultralytics YOLO real-time weapon detection with tracking, cooldown-based alerts, persistence filtering, and multi-channel notifications (Email, Telegram). This Repo also Supports VLMs for better insights of the incidents (LLaVA, PaliGemma, Qwen).
Clone and Install
git clone https://github.com/K-saif/Weapon-Detection-and-Alarm-System.git
cd Weapon-Detection-and-Alarm-System
pip install -r requirements.txtNotes:
ultralyticsis required for the detection pipeline.requestsis required for Telegram alerts.transformersandaccelerateare needed for VLM features.
Setup Alerts
- Enable 2-Step Verification in your Google Account
- Go to App Passwords (search in account settings)
- Generate a new app password for "Mail"
- Use this 16-character password as
ALERT_EMAIL_PASS
- Message @BotFather on Telegram → send
/newbot - Follow prompts to name your bot and get the bot token
- Start a chat with your bot (send any message)
- Visit
https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates - Find
"chat":{"id":XXXXXX}— that number is your chat ID
Create a .env file in the weapon_detection/ folder:
# weapon_detection/.env
# Email Configuration
ALERT_SMTP_SERVER=smtp.gmail.com
ALERT_SMTP_PORT=587
ALERT_EMAIL_SENDER=youremail@gmail.com
ALERT_EMAIL_PASS=your_16char_app_password
ALERT_EMAIL_RECEIVER=alert_receiver@gmail.com
# Telegram Configuration
ALERT_TELEGRAM_BOT_TOKEN=your_bot_token
ALERT_TELEGRAM_CHAT_ID=your_chat_idTip: Leave a value empty to disable that channel.
Supported Inputs
| Input | source |
|---|---|
| Webcam | 0 (default) |
| Video file | path/to/video.mp4 |
| RTSP stream | rtsp://user:pass@ip:554/stream |
| HTTP stream | http://ip:8080/video |
Single images and image directories are not supported — the pipeline requires a continuous frame stream.
Run Detection
# Webcam
python main.py source=0 conf=0.8 device=cpu
# Video file
python main.py source=path/to/video.mp4 conf=0.8 device=cpu
# RTSP camera
python main.py source=rtsp://user:pass@192.168.1.10:554/stream device=gpuAlert Logic
The pipeline includes:
- Persistence gating: object must appear for
persist_framesbefore alert. - Cooldown timer: same track alerts again only after
cooldownseconds. - Stale cleanup: track state is removed after
stale_framesmissing frames. - Async dispatch: alert channels run concurrently via thread pool.
Note: keep cooldown high to avoid spamming alerts, especially in crowded scenes.
For training custom models follow the Ultralytics YOLO training guide. After training, paste your best.pt into models folder or use weights=path/to/best.pt.
Note: This model was trained only for demonstration purposes on a small custom dataset. For best results, you should train on more representative dataset of weapons in various conditions.
|--main.py
|--models/
| |--best.pt
|--weapon_detection/
| |--__init__.py
| |--cli.py
| |--config.py
| |--events.py
| |--channels.py
| |--dispatcher.py
| |--tracking.py
| |--runner.py
...
A web-based dashboard is currently under development to provide real-time monitoring and visualization of weapon detection events. The dashboard will offer:
- Real-time video feed with detection overlays
- Live alert notifications and history
- System configuration and management interface
- Detection statistics and insights
- Easy access to incident snapshots and logs
Check the Dashboard README for setup and usage details.
| Argument | Type | Default | Description |
|---|---|---|---|
weights |
str |
models/best.pt |
Path to the YOLO model weights file. |
source |
int/ str |
0 |
Video input source (webcam index, file path, RTSP/HTTP stream URL). |
device |
str |
cpu |
Inference device: cpu or gpu. |
conf |
float |
0.4 |
Detection confidence threshold. |
alert_classes |
list[int] |
0 |
Class IDs that trigger alerts (comma-separated in CLI, e.g., 0,1). |
persist_frames |
int |
8 |
Frames required before the first alert for a tracked object. |
cooldown |
int |
60 |
Seconds to wait before alerting again for the same track. |
stale_frames |
int |
30 |
Missing frames before tracked state is removed. |
output_dir |
str |
alerts |
Directory used for saved snapshots and alert artifacts. |
workers |
int |
4 |
Maximum async worker threads for alert channels. |
use_vlm |
bool |
false |
Enable VLM querying for detected weapons. |
vlm_model |
str |
paligemma |
VLM backend to use: llava, paligemma, or qwen. |
Example:
python main.py source=0 device=cpu conf=0.8 output_dir=alerts- Add person detection and tracking to correlate weapons with individuals.
- Implement a web dashboard for real-time monitoring and alert management.
- Support for lightweight models like onnx or tflite for edge deployment.
Contributions are welcome! Please open an issue or submit a pull request for bug fixes, improvements, or new features.
This project is licensed under the MIT License.