How to Remove Backgrounds and Cut Out Objects from Pictures with AI

Jan 29, 2021 | Educational

Welcome to the exciting world of AI-driven image editing! In this guide, you’ll learn how to remove backgrounds and cut out objects from pictures using powerful tools such as Detectron2 and OpenCV. This process may seem complex, but with the right steps, you’ll be processing images in no time.

Understanding the Process

Imagine you are an artist working with clay. You have a block of clay that you want to mold into a beautiful sculpture. In this analogy, the clay represents your image, and the sculpting tools represent the technologies we are using. The first step involves identifying the areas you want to keep – the objects – similar to how you’d trace an outline of your sculpture. This is where Detectron2 comes into play, detecting the objects in your images. Then, just like chiseling away at the unnecessary parts of the clay, we use OpenCV to remove the background and extract the desired objects. Ultimately, what remains is the beautifully sculpted image, ready for presentation!

Installation Steps

Let’s start by installing the necessary tools.

Step 1: Install Detectron2

  • For **Linux** systems, the installation process is straightforward.
  • For **Windows 10**, install the Windows subsystem for Linux. You can find instructions here.
  • On **OS X**, refer to the Anaconda instructions here, but note that it’s currently not compatible with our demo script.

Step 2: Clone the Repository

Open the terminal and run the following commands:

git clone https://github.com/DDemmer1/ai-background-remove.git
cd ai-background-remove

Step 3: Create a Virtual Environment

To avoid messing with your system Python installation, create a virtual environment:

  • If you have **Conda**, use:
    conda create --name detectron2
    conda activate detectron2
  • For **vanilla Python**, use:
    python -m venv detectron2
    source detectron2/bin/activate
    pip install -r detectron2/requirements.txt

Step 4: Install Detectron2

Choose one of the following options to install Detectron2:

  • For CPU support only:
    pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/index.html
  • For CUDA 10.1:
    pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/index.html

Step 5: Create a Jupyter Kernel

Set up a Jupyter kernel for your virtual environment:

ipython kernel install --user --name=detectron2

Running the Pretrained Model

Ready to go? Follow these steps:

Step 1: Prepare Your Images

Copy the images you want to process into the ai-background-remove/input folder.

Step 2: Run the Detection Script

Use the following command (modify the Python version if necessary):

python detectron2/demo.py --config-file detectron2/lib/python3.7/site-packages/detectron2/model_zoo/configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml --input input/* --output output --opts MODEL.DEVICE cpu MODEL.WEIGHTS detectron2:COCO-PanopticSegmentation/panoptic_fpn_R_101_3x_139514519/model_final_cafdb1.pkl

Step 3: Check the Output

Detected objects will be marked, and a visualization will be saved in your output folder.

Object Extraction

Using Jupyter Notebook

Launch Jupyter Notebook by typing:

jupyter notebook

Open **bg-remove.ipynb**, make sure the **detectron2 kernel** is selected, and run the cells. If you want to analyze your input corpus, open **deep_watching.ipynb** and ensure you use the **detectron2 kernel**.

Troubleshooting Tips

If you encounter any issues during installation or execution, consider the following tips:

  • Ensure that your Python version is compatible with Detectron2 (ideally 3.7).
  • Double-check that you have activated your virtual environment before running any scripts.
  • If images aren’t processing, verify that they are in the correct folder and the paths are correctly set.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox