Background removal is a popular image processing technique that helps in extracting the foreground of an image while removing the unwanted background. It is widely used in many industries, including photography, e-commerce, advertising, and more. With advancements in machine learning and computer vision, this task has become easier and more accurate. In this article, we’ll explore how to perform background removal on Brainy Pi and Rembg library.
Install Dependancies
To start with, we need to install the Rembg library, which is a popular Python library for background removal. We can install it using pip with the following command:
pip install rembg
Once we have installed the library, we can start using it for background removal. In the next step, we need to load the image and remove the background using the Rembg library. We can achieve this with the following code:
from rembg import remove from PIL import Image input_path = '/content/man.jpg' # input image path output_path = '/content/output.png' # output image path input = Image.open(input_path) # load image output = remove(input) # remove background output.save(output_path) # save image
Here, we first import the required libraries, i.e., Rembg and PIL (Python Imaging Library). Then, we specify the path of the input and output image files. Next, we load the input image using the PIL library and pass it to the remove()
function of the Rembg library to remove the background. Finally, we save the resulting image to the output path.
Deploy application on Brainy Pi
Now, let’s move on to deploying this code on the Brainy Pi. We first need to clone the project repository using the following command:
Run the following command on brainypi’s terminal
git clone https://github.com/brainypi/BrainyPi-AI-Examples.git
cd BrainyPi-AI-Examples/Other/background-removal
Once we have cloned the repository, we can run the background removal script using the following command:
python bgremove.py