Handles copy-paste augmentation
Project description
cpauger
Overview
cpauger package provides functionality for cropping objects in source image(s) and pasting in destination image(s) (background images) and generating coco annotations for the resulting data.
It provides the following functionality
- Cropping and pasting objects
- Specify location to paste the object at in destination image
- Resizing object to be pasted
- Specify for each object, number to paste into a given image
- Generating random images and annotations
- Visualing the pasted objects
Why the need
Training of computer vision models to generalize well requires providing both diverse objects and different backgrounds on which the objects are captured. In reality, this ideal scenario is plagued by the following scenario
- Lack of data on target object in desired scene that model is expected to make prediction. In this case you want to predict objects in a scene that you have not yet captured the object in
- Balance target object inbalance for minority objects in the image
To handle any of the above scenario, copy-paste augmentation has proven worthwhile. Essentially, you crop and paste the object on the background image of interest for model training.
Installation
$ pip install cpauger
Usage
To run the complete function for copy-paste augmentation and generate annotation, you need images in a folder (source) and their annotation in COCO format, a list of images from which objects are to be cropped, objects to crop, images in a folder to use as background images.
Use crop_paste_obj function to run copy-paste augmentation as follows:
from cpauger.generate_coco_ann import generate_random_images_and_annotation
from cpauger.augment_image import crop_paste_obj
source_img_dir = "source_imgs"
destination_img_dir = "dest_imgs
src_imgs, src_ann = generate_random_images_and_annotation(image_height=224, image_width=224,
number_of_images=100,
output_dir=source_img_dir,
img_ext ="jpg",
image_name="src_img",
parallelize=True,
save_ann_as="src_annotation.json",
)
dest_imgs, _ = generate_random_images_and_annotation(image_height=224, image_width=224,
number_of_images=100,
output_dir=destination_img_dir,
img_ext ="jpg",
image_name="dest_img",
parallelize=True,
save_ann_as= "dest_annotation.json",
)
crop_paste_obj(object_to_cropped=["object_1"],
imgnames_for_crop=src_imgs,
img_dir=source_img_dir,
coco_ann_file="src_annotation.json",
bkgs=dest_imgs,
objs_paste_num={"object_1":1},
output_img_dir="cpaug_output",
save_coco_ann_as="cpaug_ann.json",
min_x=None, min_y=None,
max_x=None, max_y=None,
sample_location_randomly=True,
visualize_dir="viz_output_dir
)
For complete tutorials on several other utilities provided by the package, refer to
Test code
To run unit test for cpauger core functionalities, use pytest after cloning the repo and run the command.
pytest
Contributing
Interested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.
License
cpauger was created by linus agbleze. It is licensed under the terms of the MIT license.
Credits
cpauger was created with cookiecutter and the py-pkgs-cookiecutter template.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cpauger-1.1.2.tar.gz.
File metadata
- Download URL: cpauger-1.1.2.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
062cbfa7252b6788d0d7bd20f968db25d447e8e01bb8a973aeda6bac81a52873
|
|
| MD5 |
ba8abe257f8ff10a403343253dbfef24
|
|
| BLAKE2b-256 |
69c3033158816bf4fa75dea233d3aa61f3cc70b4f61365be11dd9475ccb0591f
|
File details
Details for the file cpauger-1.1.2-py3-none-any.whl.
File metadata
- Download URL: cpauger-1.1.2-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8875ac22a1be5f4e3a71d31f20ab70b5cc104fa0a12f66b61e8dc37232a33b68
|
|
| MD5 |
102784ec62a82e0e5dac59fb66eea315
|
|
| BLAKE2b-256 |
3d88a064e871034bf59bfd6f64c643859267f2ab8aab40b03e77d49a8c936919
|