A real-time AI-powered security camera system that detects weapons (pistols, knives, rifles, grenades) using your webcam or IP camera. When a weapon is detected, it automatically:
- πΈ Takes a snapshot of the person
- π₯ Records a video clip
- π§ Sends an email alert with photo
- π± Sends an SMS (via Twilio)
- π€ Sends a Telegram alert with photo
- π Plays an alarm sound
- π Shows everything on a live web dashboard
These are actual detections captured by the system:
| Pistol Detection | Knife Detection |
|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Your Webcam
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββ
β Frame captured every ~33ms β
β β
β 1. Night Mode (auto-brighten if dark) β
β 2. Face Detection (built-in OpenCV) β
β 3. Person Detection (YOLOv8 β local) β
β 4. Weapon Detection (Roboflow AI β cloud) β
βββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ Weapon found with confidence β₯ 35%
β AND detected in 3+ consecutive frames?
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββ
β Alert Pipeline (all run in background) β
β β
β πΈ Save person snapshot β
β π₯ Record 10-second video clip β
β π Play alarm beep β
β π§ Send email with photo β
β π± Send SMS β
β π€ Send Telegram message + photo β
β πΎ Log to SQLite database β
βββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
π Web Dashboard at http://localhost:5000
(live camera feed + alert history table)
Security_cam/
β
βββ main.py β Run this to start the system
βββ config.py β ALL your settings go here
β
βββ detector.py β Sends frames to Roboflow AI for weapon detection
βββ dashboard.py β Flask web dashboard (live feed + alert log)
βββ database.py β SQLite database (stores all alerts)
βββ emailer.py β Sends email alerts with photo attached
βββ sms_alert.py β Sends SMS via Twilio
βββ telegram_alert.py β Sends Telegram alerts with photo
βββ snapshot.py β Saves person photo + records video clip
βββ alarm.py β Plays beep alarm sound
βββ scheduler.py β Limits active hours + sends daily report
β
βββ yolov8n.pt β YOLOv8 model (person detection, runs locally)
βββ security_log.db β Auto-created SQLite database
βββ snapshots/ β Auto-created folder for saved photos
βββ videos/ β Auto-created folder for saved video clips
- Python 3.8 or higher
- A webcam (built-in laptop cam works fine)
- A free Roboflow account (for weapon detection AI)
- Optional: Gmail account, Twilio account, Telegram bot
git clone https://github.com/YOUR_USERNAME/Security_cam.git
cd Security_campip install opencv-python ultralytics flask requests numpyIf you want SMS alerts, also install:
pip install twilioThis is the AI engine that detects weapons. It's free to use.
- Go to https://roboflow.com and create a free account
- After logging in, click your profile icon β Settings β API Keys
- Copy your Private API Key
Open config.py and fill in your details:
# ββ Roboflow (REQUIRED) ββββββββββββββββββββββββ
ROBOFLOW_API_KEY = "paste_your_api_key_here" # β Your Roboflow API key
ROBOFLOW_MODEL = "weapon-detection-f1lih/1" # β Keep this as-is
# ββ Email (OPTIONAL) ββββββββββββββββββββββββββ
EMAIL_SENDER = "[email protected]" # β Your Gmail address
EMAIL_PASSWORD = "your_app_password" # β Gmail App Password (not your real password)
EMAIL_RECEIVERS = ["[email protected]"] # β Who gets alerts
# ββ Camera ββββββββββββββββββββββββββββββββββββ
CAMERA_INDEXES = [0] # β 0 = built-in webcam, 1 = second cameraFor EMAIL_PASSWORD: Gmail requires an "App Password" (not your normal password). Go to your Google Account β Security β 2-Step Verification β App Passwords β Generate one.
python main.pyYou will see:
[DB] Initialized.
[INFO] Loading person detection model...
[DETECTOR] Background detection thread started.
[DASHBOARD] Running at http://localhost:5000
[INFO] Starting 1 camera(s)...
[INFO] Dashboard β http://localhost:5000
[INFO] Press Q in any camera window to quit.
[CAM 0] Opened.
- A camera window will open showing the live feed with detection boxes
- Open your browser at http://localhost:5000 for the web dashboard
- Press Q in the camera window to quit
EMAIL_SENDER = "[email protected]" # Gmail that sends alerts
EMAIL_PASSWORD = "xxxx xxxx xxxx xxxx" # Gmail App Password
EMAIL_RECEIVERS = ["[email protected]"] # Can add multiple recipients
SMTP_HOST = "smtp.gmail.com" # Leave as-is for Gmail
SMTP_PORT = 587 # Leave as-isTWILIO_ENABLED = False # Change to True to enable
TWILIO_SID = "ACxxx..." # From your Twilio dashboard
TWILIO_AUTH = "your_token" # From your Twilio dashboard
TWILIO_FROM = "+1xxxxxxxxxx" # Your Twilio phone number
TWILIO_TO = ["+91xxxxxxx"] # Recipients (any country)TELEGRAM_ENABLED = False # Change to True to enable
TELEGRAM_TOKEN = "123:ABCxxx" # From @BotFather on Telegram
TELEGRAM_CHAT_IDS = ["123456789"] # Your chat ID (use @userinfobot)How to get a Telegram Bot Token:
- Open Telegram, search for
@BotFather- Send
/newbotand follow instructions- Copy the token it gives you
CAMERA_INDEXES = [0] # [0] = webcam, [0, 1] = two cameras
FRAME_WIDTH = 640 # Resolution width
FRAME_HEIGHT = 480 # Resolution height
AUTO_RECONNECT = True # Auto-reconnect if camera disconnects
NIGHT_MODE = False # Auto-brighten in dark environmentsCONFIDENCE = 0.35 # Minimum confidence (0.0β1.0). Lower = more sensitive
ALERT_COOLDOWN = 30 # Seconds to wait before re-alerting for same weapon
MIN_DETECT_FRAMES = 3 # Weapon must appear in 3 consecutive frames (reduces false alarms)
MOTION_DETECTION = True # Only call AI when motion is detected (saves API calls)
MOTION_THRESHOLD = 5000 # Motion sensitivity β lower number = more sensitiveSAVE_VIDEO_CLIP = True # Save a video clip when weapon detected
VIDEO_CLIP_SECONDS = 10 # Length of video clip in seconds
ALARM_ENABLED = True # Beep sound when weapon detected
ALARM_DURATION = 3 # How long the alarm beeps (seconds)SCHEDULE_ENABLED = False # True = only run during set hours
SCHEDULE_START_HOUR = 22 # 10 PM
SCHEDULE_END_HOUR = 6 # 6 AM (supports overnight schedules)DAILY_REPORT_ENABLED = True # Send a summary email every day
DAILY_REPORT_HOUR = 8 # Send at 8 AMWEAPON_CLASSES = {"pistol", "knife", "rifle", "grenade", "missile"}
# These are the labels the AI model can detectOpen http://localhost:5000 in your browser after starting the system.
Features:
- π· Live camera feed(s) with real-time video stream
- π Alert log table showing all detections
- π’ Live indicator β auto-refreshes every 3 seconds (no page reload needed)
- Shows: ID, Time, Camera, Weapon type, Snapshot path, Email/SMS/Telegram status
To print all logged alerts in the terminal:
python database.pyOutput example:
ID Time Cam Weapon Snapshot
----------------------------------------------------------------------
3 2026-03-01 22:04:58 0 pistol snapshots/pistol_20260301_220449.jpg
2 2026-03-01 22:04:49 0 knife snapshots/knife_20260301_220449.jpg
1 2026-03-01 21:12:28 0 pistol snapshots/pistol_20260301_211228.jpg
Total alerts: 3
[CAM 0] Opened. β Good
If it doesn't open, try CAMERA_INDEXES = [1] in config.py (some laptops use index 1).
- Make sure
ROBOFLOW_API_KEYis set correctly inconfig.py - Check your internet connection (Roboflow runs in the cloud)
- Lower the confidence:
CONFIDENCE = 0.25 - Lower MIN_DETECT_FRAMES:
MIN_DETECT_FRAMES = 1(more sensitive, more false alarms)
- Make sure you are using a Gmail App Password, NOT your regular password
- Enable 2-Step Verification on your Google account first
- Check that
EMAIL_SENDERandEMAIL_PASSWORDare correct
Install missing packages:
pip install opencv-python ultralytics flask requests numpyMake sure port 5000 is not already in use. You can change the port in dashboard.py:
app.run(host="0.0.0.0", port=5001, ...) # change to any free port| Package | Purpose | Install |
|---|---|---|
opencv-python |
Camera capture, face detection, drawing boxes | pip install opencv-python |
ultralytics |
YOLOv8 person detection (runs locally) | pip install ultralytics |
flask |
Web dashboard server | pip install flask |
requests |
Roboflow API calls, Telegram API calls | pip install requests |
numpy |
Image processing (motion detection) | pip install numpy |
twilio |
SMS alerts (optional) | pip install twilio |
All at once:
pip install opencv-python ultralytics flask requests numpy twilio| Service | Required? | Where to get it | Config key |
|---|---|---|---|
| Roboflow | YES | roboflow.com β Settings β API Keys | ROBOFLOW_API_KEY |
| Gmail App Password | Optional | Google Account β Security β App Passwords | EMAIL_PASSWORD |
| Twilio | Optional | twilio.com β Console | TWILIO_SID, TWILIO_AUTH |
| Telegram Bot | Optional | Telegram β @BotFather β /newbot | TELEGRAM_TOKEN |
To use 2 cameras simultaneously:
# config.py
CAMERA_INDEXES = [0, 1] # runs both cameras in separate threadsEach camera gets its own window and its own feed in the dashboard.
| Type | Location | Example |
|---|---|---|
| Snapshots | snapshots/ |
snapshots/pistol_20260301_211228.jpg |
| Video clips | videos/ |
videos/knife_20260301_215628.avi |
| Database | security_log.db |
SQLite, view with python database.py |
# Start the system
python main.py
# View all logged alerts
python database.py
# Run only the dashboard (to view existing logs)
python dashboard.pyThis system is intended for legitimate security monitoring on property you own or have permission to monitor. Always comply with local privacy laws and regulations regarding surveillance. Do not use this system to monitor people without their knowledge where prohibited by law.
Pull requests are welcome. For major changes, open an issue first to discuss what you'd like to change.
Built with OpenCV, YOLOv8, Roboflow, Flask, and Python.







