Let’s see how we can make it.
But before that one needs to understand what foreground and background are.
Required Deep Learning Tools
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
pip install pixellib
#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")
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.
First of all, we will blur the background of the main image.
In the next step, we will make the background grey.
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")
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")
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.
If you loved this story, do join our Telegram Community.
Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.
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.