Background_Replacement

Background Replacement

Background Replacement is a powerful tool that enables users to easily change the background of their images, opening up endless possibilities for creative transformations and visual enhancements.

apache-2.0
Image-to-Image
PyTorch
English
by @AIOZNetwork
56

Last updated: 2 months ago


Generic badge Generic badge

Background Replacements

Summary

Introduction

Background Replacements task is a task in the field of image processing and computer vision, focusing on changing the background of an image. This task aims to remove the current background and replace it with a new background, creating a new image with a different environment or context.

Changing the background can bring many benefits and applications in various fields. In photography and graphic design, Background Replacements allow the creation of creative images with diverse backgrounds, ranging from natural landscapes, cityscapes, and seascapes to special effects and unique styles. This helps create attention-grabbing images, make a strong impression, and convey messages more effectively. In advertising and marketing, Background Replacements are also used to create professional product images that attract customers.

Combining Background Replacements with the MODNet model simplifies the process of changing the background and saves time compared to manual interventions. MODNet has been trained on thousands of images with diverse backgrounds and objects, enabling it to capture precise features and boundaries. This allows MODNet to effectively handle challenging images with small details and complex boundaries.

Parameters

Inputs

  • input - (image -.png|.jpg|.jpeg): This is the original image in which you want to replace the background.
  • background - (image -.png|.jpg|.jpeg): This is the image that you want to use as the replacement background.

Output

  • output - (image -.png): This is the resulting image after the background replacement process. It will be the original image with the new background seamlessly integrated into it. The foreground objects or subjects in the image will remain unchanged, while the background will be replaced.

Examples

inputbackgroundoutput

Usage for developers

Please find below the details to track the information and access the code for processing the model on our platform.

Requirements

python~=3.10
numpy~=1.22
torch
torchvision
Pillow

Code based on AIOZ structure

from PIL import Image
import os
from .bg_remove import BGRemove

...
def do_ai_task(
        image: Union[str, Path],
        background: Union[str, Path],
        model_storage_directory: Union[str, Path],
        device: Literal["cpu", "cuda", "gpu"] = "cpu",
        *args, **kwargs) -> Any:
        
        model_id = os.path.abspath(model_storage_directory + "...")
        bg_remover = BGRemove(model_id)
        bg_remover.image(image, background=True, background_path = background, output = "output.png")
        output = open("output.png", "rb")
    return output

Reference

This repository is built upon and inspired by the MODNet, which is a model designed for real-time portrait matting using only RGB image inputs. We extend our sincere appreciation to the creators for generously sharing their code.

License

We respect and comply with the terms of the author's license cited in the Reference section, including the main contents below:

The code, models, and demos in this repository (excluding GIF files under the folder doc/gif) are released under the Apache License 2.0 license.

Citation

@InProceedings{MODNet,
  author = {Zhanghan Ke and Jiayu Sun and Kaican Li and Qiong Yan and Rynson W.H. Lau},
  title = {MODNet: Real-Time Trimap-Free Portrait Matting via Objective Decomposition},
  booktitle = {AAAI},
  year = {2022},
}