MITB Banner

Background Tuning Of Images With DeepLab V3 Using Pixellib

Edit the picture background based on image segmentation with pre-trained deep learning model in python

Share

If today, I say that you don’t need any software to edit your pictures and can edit by yourself with pre-trained deep learning model in python, how awesome would that be?

Let’s see how we can make it.

But before that one needs to understand what foreground and background are.

Source: Pinterest

Required Deep Learning Tools

Now we will try to understand the dependency and how can one download the pre-trained model.

To download the pre-trained model:

Open your browser and copy this URL there, press enter. https://github.com/ayoolaolafenwa/PixelLib/releases/download/1.1/deeplabv3_xception_tf_dim_ordering_tf_kernels.h5

Dependency: Pixellib

Installation: pip install pixellib

Code:

#importing packages
import pixellib
from pixellib.tune_bg import alter_bg
from matplotlib import pyplot as plt
import numpy as np
from PIL import Image
from IPython.display import Image as img
from pylab import rcParams
rcParams['figure.figsize'] = 10, 10 #it increases the size of plot
change_bg = alter_bg() #object creation
#here alter_bg() is a class
print(dir(change_bg)) #the functions it consists of
Output:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'blur_bg',
 'blur_camera',
 'blur_frame',
 'blur_video',
 'change_bg_img',
 'color_bg',
 'color_camera',
 'color_frame',
 'color_video',
 'gray_bg',
 'gray_camera',
 'gray_frame',
 'gray_video',
 'load_pascalvoc_model',
 'model',
 'segmentAsPascalvoc']

Loading Pre-Trained DeepLab V3

Here, we will load the pre-trained deep learning model that is DeepLab V3 for our task of background tuning.

#loading pre trained model
change_bg.load_pascalvoc_model("C:/Users/91884/Desktop/deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")

Loading Images

Now, as we are ready with the pre-trained model for background tuning, we will load the main image and the image of the required background. 

Main image:

file_name='C:/Users/91884/Pictures/demo.jpg'

plt.imshow(Image.open(file_name))

OUTPUT

Background image:

bg_file='C:/Users/91884/Pictures/background.jpg'

plt.imshow(Image.open(bg_file))

OUTPUT

Blur Background

First of all, we will blur the background of the main image.

change_bg.blur_bg(file_name,moderate=True,output_image_name='blur1.jpg')

plt.imshow(Image.open('blur1.jpg'))

OUTPUT

image, segmentation, deep learning, picture, deepleab v3, background

Grey Background

In the next step, we will make the background grey.

change_bg.gray_bg(file_name,output_image_name='gray.jpg')

plt.imshow(Image.open('gray.jpg'))

OUTPUT

image, segmentation, deep learning, picture, deepleab v3, background

Changing the background to a Solid Color

In this step, we will set the background of the main image to s solid colour.

change_bg.color_bg(file_name, colors = (225, 225, 225), output_image_name = "colored_bg.jpg")

plt.imshow(Image.open('colored_bg.jpg'))

OUTPUT

image, segmentation, deep learning, picture, deepleab v3, background

Changing the Background

Finally, we will change the background of the main image.

change_bg.change_bg_img(f_image_path = file_name,b_image_path = bg_file, output_image_name = "new_img.jpg")

plt.imshow(Image.open("new_img.jpg"))

OUTPUT

image, segmentation, deep learning, picture, deepleab v3, background

Conclusion

As we could see above, we were able to tune the background of the image very effectively. It required a very effort and could be achieved in a very few easy steps even when we were using deep learning. So using the pre-trained deep learning models yield effective results with less coding efforts. 

Hope you liked the article. Stay tuned for more.

You can follow me at the handles mentioned. 
The complete code of the above implementation is available at the AIM’s GitHub repository. Please visit this link to find the notebook of this code.

Share
Picture of Bhavishya Pandit

Bhavishya Pandit

Understanding and building fathomable approaches to problem statements is what I like the most. I love talking about conversations whose main plot is machine learning, computer vision, deep learning, data analysis and visualization. Apart from them, my interest also lies in listening to business podcasts, use cases and reading self help books.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India