Monday, July 27, 2020

How to create Flask Machine Learning API for Mask RCNN Detection model

How to create Flask Machine Learning API for Machine Learning API for Mask RCNN Detection model.

In this blog we are implement flask api for Machine Learning API for Mask RCNN Detection model.

According to Click here

An application programming interface is a computing interface which defines interactions between multiple software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc.

we are create Flask api for this.


Flask is a web application framework written in Python. It has multiple modules that make it easier for a web developer to write applications without having to worry about the details like protocol management, thread management, etc.

Flask gives is a variety of choices for developing web applications and it gives us the necessary tools and libraries that allow us to build a web application.

Now come to the python code.

First we need to clone my project then download pre-trained machine learning model for detection

One more point guys we are using cpu for this. you can also Deploy your model on GPU or any other server machine. 

Source Code : git clone https://github.com/Manishsinghrajput98/ML_Flask_APIs.git

cmd : - cd ML_Flask_APIs

In clone project i have mentioned 3 requirement txt file according our need in project i have used python 3.6 

cmd :- cd mask_rcnn_ml_flask_api

You can create virtual environment for this 

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

After this you have to activate the virtual environment

cmd :- source local/bin/myvenv

cmd :- pip install -r requirements.txt

After this you need to download my trained model i have providing. you can download easily.

Download Pre-Trained Model 


After download pre-trained model you need to copy your downloaded model in our clone project

And in future you can use your training model just replace the path of model file 

After this you need to copy/past our download model in our clone project mask_rcnn_ml_flask_api folder 

And also we need a Postman software to test our API. you need to download postman and then sign up Login.Postman is a scalable API testing tool that quickly integrates into CI/CD pipeline
Now. we are ready to test our api on Postman.

Step by step how to use Click here

The project structure looks like 

now we need to run our python script for this.

cmd :- Flask_ml_api.py --port 8080

After this you need to open postman to hit our api 

The address of our server to past in postman 

Address :- http://localhost:8080/start    (This is my local system address. If you have any other server so past these address)


Also we need parameter of our api 

Parameter : - 

{
"image_path":"/home/rajput/Desktop/ml_flask_api/yolo/manish.png"
}

Change the path according to your system path


After this you can seen the result of our model in terminal also in postman


This is the images result with white background only mask covered 

Input image :- 


Output image :- 



Note :- Friends this is the complete Flask API with real time Project. you can use these API for your project. in Real time project we need a class name of images, this API also return class name and save images with white  background in output folder also you can try this api on videos with FFMPEG (need to some changes)..
Just copy your model path

If you have any doubt so please comment 
Thanks 

2 comments:

  1. Getting a value error while predicting through postman... any help???

    File "C:\Users\rr228216\source\venv\lib\site-packages\mrcnn\model.py", line 2781, in mold_image
    return images.astype(np.float32) - config.MEAN_PIXEL
    ValueError: operands could not be broadcast together with shapes (1024,1024,4) (3,)

    ReplyDelete

If you have any doubts. Please let me know