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
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
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
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
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.