Background removal of an image using OpenCV and Deep Learning.
Python -- > Python 3.6.5
OS --> Windows 10 (OS build-->17763.253) (Version-->1809)
GPU --> Nvidia Geforce GTX 1060 (6gb)
CPU --> Intel Core i7-8750 @ 2.20GHz
RAM --> 16gb
python person.py
specify path of file when it outputs "Enter path of file: "
uses 100 layer Tiramisu neural network which was based on the DensNet.
the pre-trained model is not so accurate and works best when a single person is present in the picture.
and as it is 100-layer Tiramisu model, re-training it would take days on my system.
pre-trained models can be downloaded at:-
https://drive.google.com/open?id=1zk4j3bT1F6TZQiDsMKmEi7xB2Q6fSuHA
https://drive.google.com/open?id=1B-3dTJzlzBJ9Pz0q5QY7AP3T5zGbhuQZ
python non-person.py
specify path of file when it outputs "Enter path of file: "
uses OpenCV
*3-input.jpg gave better result when deep learning was used with 2nd model than when 1st model or OpenCV were used*
1) Loaded the pre-trained model.
2) Resized the image to 224x224 as the model was taking input in the same resolution as seen from model.json file.
3) Removed the transparency channel before getting predictions and resize the prediction to its original height and width.
4) Pixel values above the threshold factor were converted back to 255 and below threshold were converted back to 0 to exclude out of range pixels.
5) Added back the transparency channel and converted the array back to image.
6) Saved the image in png format.
1) Grayed the image and applied canny edge detection, erosion and dilation.
2) Found the contours and filled all the contours.
3) Blurred the mask after smoothing it to make the contours smooth.
4) Converted the mask into 3-channel and blended it with foreground.
5) Added transparency channel to the image to make background transparent.
6) Saved the image in png format.
Contrast and brightness can be increased for better background removal using sunglasses_removal.py file
python sunglasses_removal.py
Put the dataset folder containing the training images in the same directory as this python file.
Final images will be saved in the same format as the dataset folder in a new trained directory
https://towardsdatascience.com/background-removal-with-deep-learning-c4f2104b3157
https://www.youtube.com/watch?v=8-3vl71TjDs
https://docs.opencv.org/3.4/db/d5c/tutorial_py_bg_subtraction.html
https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html