Skip to content

Successfully established a robust Pistol Object Detection system using Faster R-CNN with ResNet50 RPN, capable of accurately identifying and localizing firearms in images. Integrated the model into both a Streamlit web app for interactive visualization and a Telegram bot for real-time pistol detection and alerting.

License

Notifications You must be signed in to change notification settings

SayamAlt/Pistol-Object-Detection-using-Faster-R-CNN

Repository files navigation

πŸ”« 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:

  1. Region Proposal Network (RPN) – Generates potential bounding boxes
  2. 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

  1. User sends an image πŸ“Έ
  2. Bot detects pistol πŸ”
  3. 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:

  1. Clone the Repository

    git clone https://github.com/yourusername/pistol-object-detection.git
    cd pistol-object-detection
  2. Install Dependencies

    pip install -r requirements.txt
  3. Install Dependencies

    pip install -r requirements.txt
  4. Set Up Telegram Bot (Optional)

  • πŸ€– Create a bot using @BotFather on Telegram.
  • πŸ”‘ Retrieve your API Token and add it to a .env file in your project directory:
    TELEGRAM_BOT_TOKEN=your_token_here
  1. πŸš€ 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

  • PyTorch
  • Torchvision

Data Handling

  • OpenCV
  • PIL
  • NumPy

Visualization

  • Matplotlib
  • Seaborn

Deployment

  • Streamlit
  • Telegram Bot API

Utilities

  • dotenv
  • tqdm

🌍 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
πŸ“§ [email protected]
πŸ”— 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.

About

Successfully established a robust Pistol Object Detection system using Faster R-CNN with ResNet50 RPN, capable of accurately identifying and localizing firearms in images. Integrated the model into both a Streamlit web app for interactive visualization and a Telegram bot for real-time pistol detection and alerting.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published