Skip to content

firrexguptaji/retail-ai-pipeline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

21 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›’ Retail AI Pipeline โ€“ Product Detection & Grouping

๐Ÿ“Œ Overview

This project implements a production-style AI pipeline to detect and group similar retail products from shelf images.

It is built using a microservice architecture with clear separation of responsibilities:

  • Detector Service โ†’ detects product bounding boxes
  • Grouping Service โ†’ groups visually + spatially similar products
  • Gateway Service โ†’ orchestrates services and provides UI

๐Ÿ—๏ธ Architecture


Client (UI)
โ†“
Gateway Service (Flask)
โ†“
Detector Service (YOLO)
โ†“
Grouping Service (ResNet + Clustering)
โ†“
Final Output (Image + JSON)


โš™๏ธ Services

1๏ธโƒฃ Gateway Service

  • Handles image upload
  • Orchestrates detector โ†’ grouping
  • Returns final JSON + image
  • Serves output images

2๏ธโƒฃ Detector Service

  • Uses YOLOv8

  • Implements:

    • Sliding window detection
    • Low confidence threshold
    • Area filtering
    • Non-Max Suppression (NMS)
    • Aspect ratio filtering
    • Duplicate removal
  • Outputs:

    • Bounding boxes
    • Cropped images

3๏ธโƒฃ Grouping Service

  • Uses ResNet18 embeddings

  • Combines:

    • Visual features
    • Spatial features
  • Applies:

    • Feature normalization
    • Agglomerative clustering
  • Outputs:

    • Group IDs
    • Annotated image

๐Ÿง  Key Design Decisions

  • Microservices โ†’ modular & scalable
  • Sliding window detection โ†’ improves recall
  • Spatial + visual fusion โ†’ better grouping
  • Config-driven system โ†’ no hardcoding
  • Logging โ†’ easier debugging

๐Ÿš€ How to Run

Dev Mode

๐Ÿ”น 1. Create Virtual Environment (Recommended)

Windows

python -m venv venv
venv\Scripts\activate

Mac/Linux

python3 -m venv venv
source venv/bin/activate

๐Ÿ”น 2. Install dependencies

pip install -r requirements/requirements-dev.txt

๐Ÿ”น 3. Start services

Option A (recommended)

python run_all.py

Option B (manual)

# Terminal 1
cd detector_service
python app.py

# Terminal 2
cd grouping_service
python app.py

# Terminal 3
cd gateway
python app.py

๐Ÿ”น 4. Open UI

http://127.0.0.1:5000

๐Ÿณ Docker Deployment

๐Ÿ”น Run with Docker

cd docker
docker-compose up --build

๐Ÿ”น Access UI

http://localhost:5000

๐Ÿ”น Service Ports

Service Port
Gateway 5000
Detector 8001
Grouping 8002

๐Ÿงช API Flow

  1. Upload image โ†’ Gateway
  2. Gateway โ†’ Detector
  3. Gateway โ†’ Grouping
  4. Final response returned

Example Response

{
  "request_id": "...",
  "output_image": "/outputs/result_xxx.jpg",
  "results": [
    {
      "bbox": [x1, y1, x2, y2],
      "group_id": 0
    }
  ]
}

๐Ÿ“‚ Project Structure

project/
โ”‚
โ”œโ”€โ”€ notebooks/
โ”‚   โ””โ”€โ”€ model.ipynb
โ”œโ”€โ”€ gateway/
โ”œโ”€โ”€ detector_service/
โ”œโ”€โ”€ grouping_service/
โ”œโ”€โ”€ logs/
โ”œโ”€โ”€ docs/
โ”œโ”€โ”€ models/
โ”œโ”€โ”€ outputs/
โ”œโ”€โ”€ docker/
โ”‚   โ””โ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ run_all.py
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md

๐Ÿ“Š Notebook (Model Development)

The notebook (notebooks/model.ipynb) was used during the experimentation phase.

๐Ÿ” Purpose

  • Prototyping detection pipeline
  • Testing slicing strategy
  • Developing filtering logic
  • Validating grouping approach
  • Analyzing clustering behavior

๐Ÿ”„ Transition to Production

Notebook Production
Inline code Microservices
Hardcoded values Config-driven
Sequential flow API-based pipeline

๐ŸŽฏ Key Learnings Applied

  • Sliding window improves recall
  • Area filtering removes noise
  • Spatial + visual features improve grouping
  • Normalization stabilizes clustering

๐Ÿ”ง Configuration & Tuning

All parameters are configurable via config.py.


๐ŸŽฏ Detector Config

SLICE_SIZE = 512
OVERLAP = 0.4
IOU_THRESHOLD = 0.4
MIN_AREA_RATIO = 0.0005
MAX_AREA_RATIO = 0.03

๐ŸŽฏ Grouping Config

DISTANCE_THRESHOLD = 0.6
SPATIAL_WEIGHT = 0.1
IMAGE_SIZE = 224
CLUSTERING_METRIC = "euclidean"
CLUSTERING_LINKAGE = "average"

๐Ÿ” Parameter Insights

  • DISTANCE_THRESHOLD

    • lower โ†’ more groups
    • higher โ†’ fewer groups
  • SPATIAL_WEIGHT

    • 0 โ†’ visual only
    • 0.1 โ†’ balanced
    • higher โ†’ spatial bias

๐Ÿงช Experiments

  • Increase DISTANCE_THRESHOLD โ†’ merge clusters
  • Reduce SPATIAL_WEIGHT โ†’ visual grouping
  • Increase OVERLAP โ†’ better detection

โš™๏ธ Environment Variables

DISTANCE_THRESHOLD=0.7
SPATIAL_WEIGHT=0.2

๐Ÿ–ผ๏ธ Example Output

Below is an example demonstrating detection and grouping results from the pipeline.

๐Ÿ“ฅ Input Image

Input Image


๐Ÿ“ค Output (Grouped Products)

Output Image


๐Ÿ“Š Sample JSON Output

{
  "request_id": "example-id",
  "output_image": "/outputs/result_example.jpg",
  "results": [
    {
      "bbox": [100, 200, 300, 400],
      "group_id": 2
    },
    {
      "bbox": [320, 210, 500, 390],
      "group_id": 2
    }
  ]
}

โšก Features

  • End-to-end ML pipeline
  • Microservice architecture
  • UI + API integration
  • Config-driven design
  • Logging support

๐Ÿ”ฎ Future Improvements

  • Fine-tuned embeddings
  • Better clustering (DBSCAN / metric learning)
  • Independent service scaling

๐Ÿ Conclusion

This project demonstrates the transition from:

Notebook โ†’ Production-ready ML system

Combining:

  • Machine Learning
  • Backend Engineering
  • System Design

๐Ÿ‘ค Author

Aman Gupta

About

Scalable microservice-based AI pipeline for retail product detection and brand grouping using YOLOv8 and embedding-based clustering.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors