Skip to main content

An open-source python library to generate synthetic datasets for computer vision tasks

Project description

SnapStitch

SnapStitch is an open-source Python library for generating synthetic datasets to enhance computer vision tasks. It simplifies the creation of superimposed images, making it especially useful when working with limited datasets.

How It Works

Image stitching is a process that generates synthetic data by placing smaller image components onto a larger background canvas. This method helps create a diverse dataset, ideal for training object detection models. Below is a step-by-step breakdown:

Example: Circuit Board Dataset

To train a model to detect components on a circuit board, you typically need a large variety of images showing the board in different orientations and configurations.

Circuit Board

Instead we propose the following approach:

  1. Take a picture of a circuit board.
  2. Extract parts like resistors, capacitors, or chips from the image.
  3. Use an empty or simplified circuit board as the background.
  4. Place the cropped components on the canvas in different positions and orientations.
Circuit Board

This approach can:

  • Quickly generate a large and varied dataset.
  • Introduce variability in positions, angles, and layouts.
  • Reduces overfitting by providing more diverse training data.

Usage

To install the snapstitch package, run:

pip install snapstitch

Assuming you have a folder structure like this:

snapstitch_project/
│
├── supermarket_dataset/
│   ├── background/
│   │   └── background_image.jpg
│   │   └── ...
│   ├── parts/
│   │   ├── bread/
│   │   │   ├── bread_part1.png
│   │   │   ├── bread_part2.png
│   │   │   └── ...
│   │   ├── canned_beans/
│   │   │   ├── canned_beans_part1.png
│   │   │   ├── canned_beans_part2.png
│   │   │   └── ...
│   │   └── jam/
│   │       ├── jam_part1.png
│   │       ├── jam_part2.png
│   │       └── ...
│   └── output/
│
└── requirements.txt

You can use the following code to generate a synthetic YOLO dataset:

from snapstitch import Stitcher, PartsLoader, BackgroundLoader, YOLOv8Generator
import albumentations as A

# Define transformations
transform = A.Compose([
    A.Rotate(limit=(-10, 5), p=1.0),
    A.HorizontalFlip(p=0.5),
    A.VerticalFlip(p=0.5),
])

# Load backgrounds
background = BackgroundLoader("supermarket_dataset/background")

# Load parts for each class with transformations
bread = PartsLoader(
    "supermarket_dataset/parts/bread", 
    scale=0.3, # Scale to apply on image
    transform=transform, # Augmentation Function
    scaling_variation=0.2 # Random variations to the scale
)
canned_beans = PartsLoader(
    "supermarket_dataset/parts/canned_beans", 
    scale=0.3,
    transform=transform, 
    scaling_variation=0.2
)
jam = PartsLoader(
    "supermarket_dataset/parts/jam", 
    scale=0.3,
    transform=transform, 
    scaling_variation=0.2
)
negative_samples = PartsLoader(
    "supermarket_dataset/parts/negative", 
    scale=0.3,
    transform=transform, 
    scaling_variation=0.2
)

# Initialize YOLOv8 generator
generator = YOLOv8Generator()

# Define Stitcher with class proportions
stitcher = Stitcher(
    generator, 
    background, 
    {
        "bread": [bread, 0.3], 
        "canned_beans": [canned_beans, 0.3], 
        "jam": [jam, 0.3], 
        "_": [negative_samples, 0.1]  # "_" for negative mining
    }, 
    image_size=30
)

# Generate datasets
stitcher.execute(10, "supermarket_dataset/output", "train_data1", train_or_val=True)  # Training data
stitcher.execute(10, "supermarket_dataset/output", "train_data2", train_or_val=False)  # Validation data
stitcher.execute(
    10, 
    "supermarket_dataset/output", 
    "train_data3", 
    train_or_val=True,
    perimeter_start=(100, 100), 
    perimeter_end=(2460, 1340)  # Custom stitching perimeter
)

For a YOLOv8 Generator, the output will be organized as follows:

snapstitch_project/
│
├── supermarket_dataset/
│   ├── background/
│   ├── parts/
│   │   ├── bread/
│   │   ├── canned_beans/
│   │   └── jam/
│   └── output/
│       ├── images/
│       │   ├── train/
│       │   │   ├── train_data1_0.png
│       │   │   ├── train_data1_1.png
│       │   │   └── ...
│       │   └── val/
│       │       ├── val_data1_0.png
│       │       ├── val_data1_1.png
│       │       └── ...
│       └── labels/
│           ├── train/
│           │   ├── train_data1_0.txt
│           │   ├── train_data1_1.txt
│           │   └── ...
│           └── val/
│               ├── val_data1_0.txt
│               ├── val_data1_1.txt
│               └── ...
│
└── requirements.txt

Features

This library is currently under development. Key features and functionalities are being implemented step-by-step. The following features have been implemented so far:

  • Superimpose objects onto backgrounds to quickly generate diverse, labeled datasets for object detection tasks.
  • Automated YOLOv8 dataset generation, including images and labels in the required format for immediate model training.

Aiming to give users more control and extend this to other computer vision tasks, we aim to:

  • Support for additional annotation formats: Expand compatibility to other object detection frameworks like Pascal VOC and COCO.
  • Advanced augmentation techniques: Allow users to add augmentations to the images before stitching through the Albumentations library.
  • Other Computer Vision tasks: Generate synthetic datasets for segmentation, oriented-bounding boxes, and pose estimation.

Contributing

Contributions to SnapStitch are welcome! Here’s how you can help:

  1. Fork the Repository: Click on the fork button in the upper right corner of the GitHub repository.

  2. Clone Your Fork: Clone your fork to your local machine using:

    git clone https://github.com/yourusername/snapstitch.git
    
  3. Set Up Your Development Environment:

    • Navigate into the cloned directory:
      cd snapstitch
      
    • Initialize PDM for the project:
      pdm install
      
  4. Create a Branch: Create a new branch for your feature or bug fix:

    git checkout -b feature/your-feature-name
    
  5. Make Changes: Implement your feature or fix the bug.

  6. Run Tests: Before committing, run the tests to ensure everything is working correctly:

    pdm run pytest
    
  7. Check Flake8 Compliance: Ensure your code complies with the PEP 8 style guide by running Flake8:

    pdm run flake8
    

    Address any issues reported by Flake8 before proceeding.

  8. Commit Your Changes: Commit your changes with a clear and concise message:

    git commit -m "Add feature: your feature description"
    
  9. Push to Your Fork: Push your changes to your fork:

    git push origin feature/your-feature-name
    
  10. Submit a Pull Request: Go to the original repository and submit a pull request with a description of your changes.

License

SnapStitch is released under the MIT License. See the LICENSE file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snapstitch-0.1.2.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snapstitch-0.1.2-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file snapstitch-0.1.2.tar.gz.

File metadata

  • Download URL: snapstitch-0.1.2.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.0 CPython/3.11.11 Linux/6.5.0-1025-azure

File hashes

Hashes for snapstitch-0.1.2.tar.gz
Algorithm Hash digest
SHA256 dce3f892e51f3de030b9b6112fda13e2dd8abe2584de2e5986a7a1b71f2df9d7
MD5 40f23783b0bda5cb568f13f699991a89
BLAKE2b-256 c301fd95a6cf815cb64e61a3e8589600337ce74de21b9cc33df6be326a004d87

See more details on using hashes here.

File details

Details for the file snapstitch-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: snapstitch-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.0 CPython/3.11.11 Linux/6.5.0-1025-azure

File hashes

Hashes for snapstitch-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 054d79c5ef8fd20bc323c2f96b4e21157d7a28d7e924efec6ddac9f3d7b7bb9a
MD5 af213de3e8f395be29ec1cbc5f542eb9
BLAKE2b-256 210129f4f6e684f4171a56be34651386e339652f0420a4a503cb299235a9c803

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page