π« Pistol Object Detection using Faster R-CNN (ResNet50 RPN)
π Overview
Pistol Object Detection using Faster R-CNN with ResNet50 RPN is an end-to-end deep learning project built to detect and localize pistols in images for potential safety and security applications.
It leverages state-of-the-art object detection architecture β Faster R-CNN with a ResNet50 Feature Pyramid Network (FPN) β fine-tuned on a custom dataset of pistol images.
The project showcases the full lifecycle of AI solution development:
βοΈ Model Training β π Object Detection β π Streamlit Web App β π€ Telegram Bot Deployment
π― Project Highlights
- π§ Deep Learning Model β Faster R-CNN (ResNet50 backbone) trained for high-accuracy pistol detection
- ποΈ Custom Dataset β Curated and annotated dataset of pistols in real-world scenarios
- π§© Transfer Learning β Efficient adaptation from COCO pre-trained weights
- π¨ Bounding Box Visualization β Real-time pistol localization with labeled boxes
- β‘ Streamlit Web App β Interactive image-based detection interface
- π¬ Telegram Bot Integration β Chat-based pistol detection for fast mobile access
- π Performance Optimization β SGD optimizer, StepLR scheduler, data augmentation, and checkpointing
π§© Model Architecture
The model architecture is based on Faster R-CNN with ResNet50 FPN, a two-stage object detection pipeline:
- Region Proposal Network (RPN) β Generates potential bounding boxes
- ROI Pooling + Classification Head β Refines proposals and predicts class labels
Architecture Summary:
- Backbone:
ResNet50 Feature Pyramid Network
- ROI Head: Custom
FastRCNNPredictor
- Loss Function: Multi-task loss (Classification + Regression)
- Optimizer: SGD (momentum=0.9)
- Learning Rate Scheduler: StepLR
- Framework: PyTorch (torchvision)
π¦ Dataset & Preprocessing
The dataset contains images of pistols and backgrounds, annotated in COCO-like JSON format.
| Component |
Details |
| Classes |
2 β ['bg', 'Pistol'] |
| Image Size |
Variable (resized dynamically) |
| Format |
JPG |
Preprocessing Steps:
- Image normalization (
ToTensor())
- Data augmentation: random flip, scale, color jitter
- Label encoding for class mapping
- Batched DataLoader for GPU efficiency
π§ Model Training
from torchvision.models.detection import fasterrcnn_resnet50_fpn
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
model = fasterrcnn_resnet50_fpn(weights='COCO_V1')
in_features = model.roi_heads.box_predictor.cls_score.in_features
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes=2)
π Streamlit Web App Deployment
A clean, interactive Streamlit web interface enables users to upload images and visualize pistol detections in real-time.
π§© Features
- π€ Upload an image (JPG/PNG)
- βοΈ Model runs inference with bounding boxes and confidence scores
- πΌοΈ Displays uploaded and detected images side-by-side
- π₯ Dynamic red bounding boxes and white labels for detected pistols
- ποΈ Adjustable confidence threshold for precision control
βΆοΈ Run the App
streamlit run app.py
π₯οΈ App Layout & βοΈ Tech Stack
π§© App Layout
- Left: Uploaded Image
- Right: Pistol Detection Results
βοΈ Tech Stack
- Frontend: Streamlit
- Backend: PyTorch
- Frameworks: torchvision, Pillow, numpy
π€ Telegram Bot Deployment
A fully functional Telegram Bot allows pistol detection directly from chat β fast, simple, and secure!
βοΈ Commands
/start β Welcome and usage info
/help β Guide on how to use the bot
/about β Model and project details
π§ How It Works
- User sends an image πΈ
- Bot detects pistol π
- Returns the image with bounding boxes π₯
βΆοΈ Run the Bot
python telegram_bot.py
βοΈ Required Setup
To get started with the Pistol Object Detection system, follow these simple setup steps:
Clone the Repository
git clone https://github.com/yourusername/pistol-object-detection.git
cd pistol-object-detection
Install Dependencies
pip install -r requirements.txt
Install Dependencies
pip install -r requirements.txt
Set Up Telegram Bot (Optional)
- π Run the Applications
π§ For Streamlit Web App
Launch the interactive web app to visualize pistol detection in real-time:
streamlit run app.py
π€ For Telegram Bot
Run the bot to enable pistol detection directly via chat:
python telegram_bot.py
Once running, the bot supports:
- /start β Welcome message
- /help β How to use the bot
- /about β Project and model details
βοΈ Installation
π§© Clone Repository
git clone https://github.com/yourusername/pistol-object-detection.git
cd pistol-object-detection
π¦ Install Dependencies
pip install -r requirements.txt
Required Packages
- torch
- torchvision
- streamlit
- pillow
- opencv-python
- python-telegram-bot
- numpy
- python-dotenv
π§± Project Structure
π Pistol Object Detection using Faster R-CNN
β
βββ π app.py # Streamlit web app
βββ π telegram_bot.py # Telegram bot integration
βββ π detection.py # Core detection logic
βββ π pistol_object_detector.pth # Trained model weights
βββ π requirements.txt # Python dependencies
βββ π .env # Telegram bot token
βββ π data/ # Dataset directory
βββ π outputs/ # Model outputs & results
βββ π README.md # Project documentation
π§© Key Learnings
- Built a custom Faster R-CNN object detector for domain-specific pistol detection.
- Used transfer learning with fine-tuning for faster convergence.
- Developed real-time, interactive AI apps using Streamlit and Telegram.
- Integrated deep learning with communication platforms for instant inference.
π§° Tools & Frameworks
Category & Tools
Deep Learning
Data Handling
Visualization
Deployment
- Streamlit
- Telegram Bot API
Utilities
π Future Enhancements
- π Expand to detect multiple weapon types (rifles, knives, etc.)
- π§© Integrate YOLOv8 for faster, real-time detection
- π§ Deploy on edge devices (Jetson Nano, Raspberry Pi)
- π Create a web-based analytics dashboard for detections
- π Enable CCTV-based alert system for proactive monitoring
π§‘ Acknowledgements
Special thanks to:
- π§ PyTorch & Torchvision teams for robust deep learning frameworks
- π» Streamlit for enabling rapid AI app deployment
- π€ Telegram Bot API for seamless conversational integration
- π Faster R-CNN (Ren et al.) researchers for their foundational work in object detection
π¨βπ» Author
Sayam Kumar
π§ sayamk565@gmail.com
π LinkedIn | GitHub
βAI isnβt just about automation β itβs about making intelligence visible.β
π License
This project is licensed under the MIT License.
You are free to use, modify, and distribute it with proper attribution.