Final-Year Engineering Project | YOLOv8 Β· FastAPI Β· React Β· OpenCV Β· Twilio
An AI-powered weapon detection system that identifies guns and knives in uploaded CCTV videos, static images, and live webcam streams using the YOLOv8 deep learning model.
Major_II/
βββ backend/ FastAPI Python backend
β βββ main.py App entry point
β βββ config.py Environment-based configuration
β βββ .env.template Credential template (copy β .env)
β βββ requirements.txt
β βββ model/
β β βββ detector.py YOLOv8 singleton + annotation
β βββ routers/
β β βββ image.py POST /detect/image
β β βββ video.py POST /detect/video + GET /detect/video/{id}
β β βββ webcam.py GET /detect/webcam/stream (MJPEG)
β βββ services/
β β βββ alert.py Twilio SMS with rate-limiting
β β βββ processing.py Frame resize, noise/blur, video writer
β βββ outputs/ Saved annotated images & videos
β
βββ frontend/ React + Vite dashboard
β βββ src/
β βββ App.jsx
β βββ api/axiosClient.js
β βββ components/
β βββ ImageDetection.jsx
β βββ VideoDetection.jsx
β βββ WebcamDetection.jsx
β βββ DetectionLog.jsx
β βββ AlertBadge.jsx
β
βββ model/ Place fine-tuned weights here
βββ dataset/ Kaggle dataset storage
βββ scripts/
β βββ download_kaggle_dataset.py
β βββ train_custom_model.py
βββ outputs/ (mirror of backend/outputs for direct access)
- Python 3.10+
- Node.js 18+
- (Optional) NVIDIA GPU + CUDA for faster inference
- (Optional) Twilio account for SMS alerts
cd backend
# Create virtual environment
python -m venv venv
venv\Scripts\activate # Windows
# source venv/bin/activate # Linux/Mac
# Install dependencies
pip install -r requirements.txt
# Configure credentials
copy .env.template .env # Windows
# cp .env.template .env # Linux/Mac
# Then edit .env with your Twilio credentials and model path| Variable | Default | Description |
|---|---|---|
TWILIO_ACCOUNT_SID |
(empty) | From console.twilio.com |
TWILIO_AUTH_TOKEN |
(empty) | From console.twilio.com |
TWILIO_FROM_NUMBER |
(empty) | Your Twilio number |
TWILIO_TO_NUMBER |
(empty) | Alert recipient number |
MODEL_PATH |
yolov8n.pt |
Path to YOLO weights |
CONFIDENCE_THRESHOLD |
0.45 |
Min confidence to flag |
FRAME_SKIP |
2 |
Frames between inferences |
WEAPON_CLASS_IDS |
(empty = all) | Comma-separated class IDs |
Note:
yolov8n.ptis auto-downloaded from Ultralytics on first run.
After fine-tuning, setMODEL_PATH=../model/weapon_detector.pt.
# From the backend/ directory with venv active
uvicorn main:app --reload --host 0.0.0.0 --port 8000Visit http://localhost:8000/docs for the interactive Swagger UI.
cd frontend
npm install
npm run devVisit http://localhost:5173 to open the dashboard.
- Open the CCTV Video tab
- Upload any
.mp4/.avifile - (Optional) Toggle "Stop on first detection" or adjust frame skip
- Click βΆ Start Detection
- Watch the progress bar update live as frames are processed
- Once complete, the output video plays inline and the detection timeline appears below
- Open the Image tab
- Drag & drop or click to upload a JPEG/PNG/WebP image
- Click π Detect Weapons
- The annotated output image appears side-by-side with bounding boxes, labels, and confidence scores
- Open the Live Webcam tab
- Select camera index (0 = default webcam)
- Click βΆ Start Live Feed
- The MJPEG stream from the backend appears with real-time overlays
# Install kaggle CLI
pip install kaggle
# Place kaggle.json from https://www.kaggle.com/settings β API
# Windows: C:\Users\<user>\.kaggle\kaggle.json
# Linux: ~/.kaggle/kaggle.json
python scripts/download_kaggle_dataset.py --dataset "owner/dataset-name" --output dataset/Recommended public datasets:
ultralytics/weapon-detection-6sczo- Search Roboflow Universe for "weapon detection" YOLO format datasets
# After dataset is downloaded and data.yaml is ready:
python scripts/train_custom_model.py \
--data dataset/data.yaml \
--model yolov8n.pt \
--epochs 50 \
--imgsz 640
# Best weights saved to model/weapon_detector.pt
# Update backend/.env: MODEL_PATH=../model/weapon_detector.pt| Method | Endpoint | Description |
|---|---|---|
GET |
/health |
Backend health + model status |
POST |
/detect/image |
Upload image β annotated result |
POST |
/detect/video |
Upload video β start async job |
GET |
/detect/video/{job_id} |
Poll job status / get result |
GET |
/detect/webcam/stream |
MJPEG webcam stream |
GET |
/detect/webcam/status |
Current FPS + detections JSON |
POST |
/detect/webcam/stop |
Stop webcam capture thread |
| Layer | Technology |
|---|---|
| ML Model | YOLOv8 (Ultralytics) |
| Backend | Python 3.10, FastAPI, Uvicorn |
| CV | OpenCV (opencv-python-headless) |
| Alerts | Twilio REST API |
| Frontend | React 18, Vite 5, Axios |
| Styling | Vanilla CSS (dark glassmorphism) |
- Use
yolov8s.pt(small) oryolov8m.pt(medium) for better accuracy at the cost of speed - Enable GPU by setting
DEVICE=0in env /--device 0in training - Increase
FRAME_SKIP(e.g. 4β8) to speed up long CCTV video processing - Lower
CONFIDENCE_THRESHOLDto catch more detections (more false positives)
Built for academic demonstration. For production deployment, add authentication, secure CORS, and rate-limiting.