Blur or Change Background of Images using Machine Learning with Tensorflow

Dipesh Pal
Analytics Vidhya
Published in
3 min readJun 12, 2020

--

In this article, we will talk about how you can change the background of the images or how you can blur the background of the images. We will use Unet architecture to achieve this goal.

We have a dataset name OCHuman, which provide us bounding-box, humans pose and instance mask but that is not enough for our task because it gives images like this-

Dataset after applying Annotation (Labels)

We want only mask (black background and white humans) as labels and images (original) as features. So we have created our customs dataset which has images like this-

Custom Dataset

We will feed these images to the Unet model we have created and the model should able to predict similar kinds of masks.

If you want to know how I created these images and train the model read this medium article: https://medium.com/analytics-vidhya/humans-image-segmentation-with-unet-using-tensorflow-keras-fd6cb43b06e5

This above article will explain to you in detail about the dataset, training, code, and theory.

The architecture of the Unet we use-

You can see that we have Trainable params: 1,941,139. You can increase the number of trainable params which may help you to have better accuracy.

Results-

Prediction-

Prediction

Creating Blur or Change Background-

Blur Images

We have created the script to blur the images, let talk about how we are doing this.

First of all, you need to provide the original image then you need to pass this image to the model to predict the mask.

Then we also pass the original image to the cv2.blur() function.

Now, we have a total of three images, the first original non-blur image, the second predicted mask, and the third blur image.

Now to get our blur image we need to replace black pixels (from the mask) with the blur image pixels and white pixels (from the mask) with the original image. The resultant image is or blur-background image.

Similarly, as we replacing the black background you can replace the black background with your custom background images also.

You can download our pre-train model and just run the “start.py”.

The code and the model you can download from our GitHub repo.

You can read more on my Website: www.dipeshpal.com

You can know more about me: www.dipeshpal.in

You can watch my tech videos on YouTube: https://www.youtube.com/DIPESHPAL17

Linkedin: https://www.linkedin.com/in/dipesh-pal-a34952110/

Twitter: https://twitter.com/dipesh_pal17

GitHub: https://github.com/Dipeshpal

  1. GitHub Code: https://github.com/Dipeshpal/Image-Segmentation-with-Unet-using-Tensorflow-Keras (You can use this module to run on your system but I’ll recommend you to use Google Colab)
  2. Complete Tutorial Human Segmentation with UNet using Tensorflow: https://medium.com/analytics-vidhya/humans-image-segmentation-with-unet-using-tensorflow-keras-fd6cb43b06e5

--

--

Dipesh Pal
Analytics Vidhya

I'm Data Scientist, Developer, YouTuber, Photograper, and Blogger! www.dipeshpal.in