Active Hackathon

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

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.

THE BELAMY

Sign up for your weekly dose of what's up in emerging technology.

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.

More Great AIM Stories

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.

Our Upcoming Events

Conference, in-person (Bangalore)
Machine Learning Developers Summit (MLDS) 2023
19-20th Jan, 2023

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
21st Apr, 2023

Conference, in-person (Bangalore)
MachineCon 2023
23rd Jun, 2023

3 Ways to Join our Community

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Telegram Channel

Discover special offers, top stories, upcoming events, and more.

Subscribe to our newsletter

Get the latest updates from AIM