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
- π§ 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
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)
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
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)A clean, interactive Streamlit web interface enables users to upload images and visualize pistol detections in real-time.
- π€ 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
streamlit run app.py- Left: Uploaded Image
- Right: Pistol Detection Results
- Frontend: Streamlit
- Backend: PyTorch
- Frameworks: torchvision, Pillow, numpy
A fully functional Telegram Bot allows pistol detection directly from chat β fast, simple, and secure!
/startβ Welcome and usage info/helpβ Guide on how to use the bot/aboutβ Model and project details
- User sends an image πΈ
- Bot detects pistol π
- Returns the image with bounding boxes π₯
python telegram_bot.pyTo 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)
- π€ Create a bot using @BotFather on Telegram.
- π Retrieve your API Token and add it to a
.envfile in your project directory:TELEGRAM_BOT_TOKEN=your_token_here
- π Run the Applications
Launch the interactive web app to visualize pistol detection in real-time:
streamlit run app.pyRun the bot to enable pistol detection directly via chat:
python telegram_bot.pyOnce running, the bot supports:
- /start β Welcome message
- /help β How to use the bot
- /about β Project and model details
git clone https://github.com/yourusername/pistol-object-detection.git
cd pistol-object-detectionpip install -r requirements.txt- torch
- torchvision
- streamlit
- pillow
- opencv-python
- python-telegram-bot
- numpy
- python-dotenv
π 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
- 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.
- PyTorch
- Torchvision
- OpenCV
- PIL
- NumPy
- Matplotlib
- Seaborn
- Streamlit
- Telegram Bot API
- dotenv
- tqdm
- π 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
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
Sayam Kumar
π§ [email protected]
π LinkedIn | GitHub
βAI isnβt just about automation β itβs about making intelligence visible.β
This project is licensed under the MIT License.
You are free to use, modify, and distribute it with proper attribution.