Skip to content

Object Detection With PyTorch and Custom the Model By Flask is an end-to-end solution for deploying object detection models using PyTorch, with an easy-to-use Flask web interface for serving predictions. The project leverages the power of the Faster R-CNN model with a ResNet-50-FPN-V2 backbone to accurately detect and localize objects in images.

License

Notifications You must be signed in to change notification settings

TareqAlKushari/Object-Detection-With-PyTorch-and-Custom-the-Model-By-Flask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎯 Object Detection with PyTorch & Flask

GitHub Repo Size Last Commit Issues Forks Stars

A complete end-to-end object detection system that leverages PyTorch's Faster R-CNN pretrained model and serves detection via a Flask web application and REST API. This project supports detecting objects in images and videos using a modular and extensible pipeline.

πŸ“Έ Demo

Output Demo

Screenshot of the model.

πŸš€ Features

  • πŸ” Object detection using Faster R-CNN ResNet50 FPN V2 with COCO weights
  • πŸ–ΌοΈ Detect objects in images with bounding box visualization
  • πŸŽ₯ Detect objects frame-by-frame in videos with FPS display
  • 🌐 Web interface for easy image input and result visualization
  • βš™οΈ REST API backend for programmatic detection requests
  • πŸ’» Modular Python scripts for model loading, inference, and visualization
  • πŸ—‚οΈ Organized project structure for ease of use and extension

πŸ“ Project Structure

Object-Detection-With-PyTorch-and-Custom-the-Model-By-Flask
β”œβ”€β”€ assets/                  # Static assets for UI or documentation
β”œβ”€β”€ data/                    # Dataset files (annotations, images)
β”œβ”€β”€ docs/                    # Documentation resources
β”œβ”€β”€ input/                   # Input images/videos for testing
β”œβ”€β”€ outputs/                 # Output detections and results
β”œβ”€β”€ python/                  # Core Python modules
β”‚   β”œβ”€β”€ detect_utils.py      # predict() + draw_boxes()
β”‚   β”œβ”€β”€ model.py             # Load pretrained Faster R-CNN model
β”‚   └── utils.py             # COCO class names
β”œβ”€β”€ static/                  # Static files served by Flask
β”‚   β”œβ”€β”€ css/                 # CSS styles
β”‚   β”‚   └── main.css         # Stylesheet for the web UI
β”‚   └── uploads/             # Uploaded images directory
β”œβ”€β”€ templates/               # HTML templates for Flask web pages
β”‚   β”œβ”€β”€ base.html            # HTML base layout
β”‚   └── homepage.html        # Home page with upload and detection UI
β”œβ”€β”€ translations/            # Language translations (if applicable)
β”œβ”€β”€ api_app.py               # Flask app and routes
β”œβ”€β”€ detect_api.py            # Core detection for Flask app
β”œβ”€β”€ detect_image.py          # CLI: detect objects in image
β”œβ”€β”€ detect_video.py          # CLI: detect objects in video
β”œβ”€β”€ LICENSE                  # License file (MIT)
β”œβ”€β”€ README.md                # This README file
└── requirements.txt         # Python dependencies

πŸ’‘ Installation

  1. Clone the repository:
git clone https://github.com/TareqAlKushari/Object-Detection-With-PyTorch-and-Custom-the-Model-By-Flask.git
cd Object-Detection-With-PyTorch-and-Custom-the-Model-By-Flask
  1. (Optional) Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install required packages:
pip install -r requirements.txt

🎯 Usage

Image Detection

Detect objects on a single image and save the output with bounding boxes.

python detect_image.py path/to/image.jpg --threshold 0.5

The output image will be saved in outputs/ directory.

Video Detection

Detect objects on a video file (or webcam stream) frame-by-frame with FPS display.

python detect_video.py path/to/video.mp4 --threshold 0.5

Processed video will be saved in outputs/ directory.

Press q to quit the video window early.

Flask Web Application

Start the interactive web app to upload images and visualize detection results in the browser.

python api_app.py

Open http://localhost:9000 in your browser.

  • Input the path to an image accessible to the server for detection.
  • View the annotated image rendered in the web page.

REST API Integration

Use detect_api.py functions to integrate detection into other applications or create REST endpoints.

Example curl request (assuming you add an API endpoint):

curl -X POST -F image=@path/to/image.jpg http://localhost:5000/predict

🧠 How It Works

πŸ” Backend Inference Flow

  • python/model.py: Loads pretrained model from torchvision.models.detection

  • detect_utils.py:

    • predict(image, model, device, threshold) – preprocesses image, runs inference, filters predictions
    • draw_boxes() – draws bounding boxes and class labels using OpenCV
  • utils.py: Defines the 91 COCO classes used for label mapping

🎨 Web UI Logic

  • HTML/CSS in templates/ and static/css/

  • Input form allows user to submit image path

  • api_app.py:

    • Renders homepage.html
    • Calls detect_api.py to run detection on the input
    • Saves and displays output via HTML <img>

🧾 COCO Classes (Examples)

  • person, bicycle, car, motorcycle, airplane, bus
  • dog, cat, horse, sheep, cow
  • bottle, chair, laptop, keyboard, clock
  • ... and more (total: 91 categories)

πŸ“· Sample Output

Input Detected Output

βš™οΈ Dependencies

  • Python 3.7+
  • PyTorch
  • torchvision
  • OpenCV
  • Flask
  • Pillow
  • numpy

(See requirements.txt for full list.)

πŸ“„ License

This project is licensed under the MIT License.

πŸ‘€ Author

Tareq Al Kushari πŸ”— GitHub Profile


If you encounter any issues or have suggestions, please open an issue or submit a pull request. Happy detecting! πŸš€

About

Object Detection With PyTorch and Custom the Model By Flask is an end-to-end solution for deploying object detection models using PyTorch, with an easy-to-use Flask web interface for serving predictions. The project leverages the power of the Faster R-CNN model with a ResNet-50-FPN-V2 backbone to accurately detect and localize objects in images.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •