Wednesday, July 29, 2020

How to extract mask values in Image and applying on white background using machine learning

How to extract mask values in Image and applying on white background using machine learning.

In this blog we are implementing How to extract mask values in Image and applying on white background using Pre-trained Deep Labs machine learning Model.



In this blog we are using Deeplabs pretrained model for extract mask values from images 

DeepLab is one of the most promising techniques for semantic image segmentation.

If you want to learn more about deep labs so you can Click here

I will cover only implementation part. you can easily use my code for your real time project.
just replacing the path of model.

If you want to learn how to create annotations for deeplab training, and also how to train deeplab model for custom dataset. so i will covert in my next blog with details description.

In this blog Also i have provide my flask api script so you can easily integrate your project for semantic Image segmentation. 

And first you need to download deeplabs pre-trained model click here. after download.. the download Folder contained 3 files but we need only one frozen_inference_graph.pb we need only these .pb files.

After this you have to clone my project on github.

cmd :- https://github.com/Manishsinghrajput98/extract_mask_from_images_using_ml.git

cmd :- cd extract_mask_from_images_using_ml

And again one more time because my repository contains more projects so you select extract mask_from_images_using_ml folder 

cmd :- cd extract_mask_from_images_using_ml

Project file,folder structure like



After this you have to copy/past your download model file which is name is frozen_inference_graph.pb  Past in model folder (clone project)

Create virtual environment for this. I have used python 3.6  

cmd :- virtualenv --python=python3.6 myvenv

After this you need  to activate the virtual environment

cmd :- source local/bin/myvenv

cmd :- pip install -r requirements.txt

I have already collect testing images in input folder.if you want to try on your images so copy in the input folder .

Now we are ready for script execution. our extract_mask_from_images_using_ml folder contained two python script one is flask api, and second is without flask api. you should try both files.

If you want to test flask api script so we will need postman software. also you can design front-end for this and call your flask api. it will returns result on front-end (html page)

If you want to learn how to use postman api tools, how to download, login-signup or parameter setting so i will cover in future blog. if you need.

Because friends in AI industry all most people creating model api for showing there model result in web Application. without api you can not show your ml model result in front-end (html page).
So you need to learn how to create api for this. I have covered in my all blog post you can used it 
Because i have provide flask api in my all blogs. So you can easily use these api in your real time Project. and I am not a expert. I am pass out in  2019 Batch with information Technology and Engineering. I am sharing my past 1 years experience in AI

Also you can try on my without_flask_api script for testing purpose.

So now we are test in random images.

Also you can see my input image in input folder.



After run script 

cmd :- python without_flask_api.py --input input/Test1.jpg

your terminal like 



After this you received output image in output folder

like 


and try more images 

cmd :- python without_flask_api.py --input input/2.jpg

input image 


You received output image in your output folder 


And also you can try your images but it return this type of images only which is in coco dataset name list Because deeplabs pre-trained model train on coco dataset.

Now we will come to the flask api for this and implement this

I have already mentioned you need postman, or any other front-end page etc so you can call this api

First we need to run server file. and also you can run this files on server aws machine or etc for Deployment 

cmd :- python with_flask_api.py --port 8080 

(you can change port, the default port is 8080) you can try without port. I have mentioned in my code it will run by default port 8080

After run this script in your terminal.

Your terminal like 



And after this you need to open your postman tool

Postman tools take address of your api, and api parameter

Address:- http://localhost:8080/start

Parameter :- 

{
"image_path":"/home/rajput/Desktop/extract_mask_from_images_using_ml/input/6.jpg"
}

You must change the path according your image after this you need to hit api to click send button on postman tools



If you received this type of massage in your postman. your api call successful

Also you can check your terminal 

 
After this you open your output-flask-api folder. it will store output image



In this image one side is input image and one side is output image.

Similar you can try all images in our input image.


And server terminal like 



This is the complete Flask api for deploy in your ml model in any machine like Google cloud, AWS etc.

If you have any doubt so please comment 

Thanks 

2 comments:

If you have any doubts. Please let me know