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 

How to create Flask Machine Learning API for Inception-v3 Image Classification model

How to create Flask Machine Learning API for Inception-v3 Image Classification model.

In this blog we are implement flask api for inception-v3 classification deep learning 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 inceptionv3_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 Trained Model 


After download you need to copy your downloaded model in our clone project. in contained two files one is label files (train.txt) and second is model files (train.pb).

I have train two categories for demo purpose you can use this model.

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

After this you need to copy/past our download model in our clone project inceptionv3_ml_flask_api logs 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 :- inception_flask_ml.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




                                                                                Fig 1



                                                                             Fig 2

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 write text on images Save in our project output folder also you can try this api on videos with FFMPEG (need to some changes)..
Just copy your model path,label name  txt, and past these api.

If you have any doubt so please comment 
Thanks 


How To Create Flask API For YOLO Deep Learning Model.

How To Create Flask API For YOLO Deep Learning  Model.

In this blog we are implement flask api for yolo deep learning model



According to wikipedia 

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

cmd :- cd yolo_ml_flask_api

You can create virtual environment for this 

cmd :- virtual 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 pre-trained model i have providing. you can download easily.

Download Pre-Trained Model 

cmd :- wget https://pjreddie.com/media/files/yolov3.weights

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

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
After this. we are ready to test our api on Postman.

Step by step how to use Click Here

The project structure looks like 

 

cmd :- python yolo_ml_flask_api.py --port 8080

looks like


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 

{
"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



Also you can see the images. i have write the detected images in our project.



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 detected images save in our project also you can try this api on videos with FFMPEG (need to some changes)..
just copy your model path,cfg file, class name txt, and past these api.

If you have any doubt so please comment 
Thanks 

Wednesday, July 15, 2020

How to Extract Text From Images

This Blogs will show you how to extract text from a PDF or an image with Tesseract OCR.

Optical Character Recognition(OCR) is the process of electronically extracting text from images or any Documents like PDF and reusing it in a variety of ways such as full text searches.



OCR technology is used to convert virtually any kind of images containing written text (typed, Handwritten or printed) into machine-readable text data

Pytesseract recognize and read the text present in images. It can read all image types png, jpeg, jpg etc. 
It’s widely used to process everything from scanned documents or you can read more Click here



one more point we need to smooth our input images using some opencv functions methods Click here

Noise is random variation of brightness or colour in an image, that can make the text of the image more difficult to read. Certain types of noise cannot be removed by Tesseract in the binarisation step, which can cause accuracy rates to drop

Converting image to grayscale 

    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Applying dilation and erosion to remove the noise (you may play with the kernel size depending on your image)

   kernel = np.ones((2, 2), np.uint8)
   img = cv2.dilate(img, kernel, iterations=1)
   img = cv2.erode(img, kernel, iterations=1)
Don't worry guys. i will be providing full code so you can easily integrate to your text extraction projects. or also create flask api using this blog Click here

You need to clone my project on Github 

before this process we will create virtual environment 

cmd :- virtualenv local

cmd :- source local/bin/activate
 
cmd :- git clone 

cmd :- cd text_extract

cmd :-  pip install -r req.txt

Note :- During this installation if you face such type of error so you can follow this command.



cmd :- sudo apt install tesseract-ocr

cmd :- sudo apt install libtesseract-dev

Now we will run this project.

This is the simple script for simple input image. 

cmd :- python simple.py --input input_image.png



Now we will use some complex images like color images with apply some opencv functions.

cmd :- after_smoth.py --input 1.jpg



In this images i have extract scores on football score board you can also try different score board but you need to play some opencv function according to your input images.

Thanks.  












Sunday, July 12, 2020

Yolo model trained using Deepfashion2 Dataset

Hello Friends in this blog i will be implement Yolo model training process using DeepFashion2 Dataset.



If want to study about DeepFashion2 so you can Click here.

I want to show only practical not theory.

First we need to Download Deepfashion2 Dataset and after this we need to convert coco format. 

I have been  implemented  step by step conversion and training process on mask rcnn model Click here

After you convert coco format. now we need to convert coco to Yolo model format.

And after this process we ready to train our Yolo model using Deepfashion2 Dataset.

But one more point guys. I have a some Hardware resources problem. so i will using only 200 images For demo purpose. If you have high specification hardware so you can use all Deepfashion2 Dataset ( train - 1,91,961, validation - 32,153 ) and also you can use Google Colab for This. i Have use my local system for show entire process.

Note :- If you want to decrease the size of your Deepfashion2 Dataset. in this Python script you need to Change line number 114 before convert  coco format conversion.

Now start the conversion coco to yolo.

First we need to clone Click here

or

cmd :- git clone  https://github.com/Manishsinghrajput98/deepfashion2coco_to_yolo_.git

cmd :- cd deepfashion2coco_to_yolo_

you can create virtual environment 

cmd :-  virtualenv local

cmd:-  source local/bin/activate

Or also you can ues both foder of deepfashion2 dataset train and validation. If you use both folder so you need to execute python script for two times and after this process you have to combine both Folder.

cmd :- pip install -r requirements.txt

the run python script 

cmd :-  python3 example.py --datasets COCO --img_path /home/rajput/Desktop/deepfashion2coco_to_yolo_/Dataset_Deepfashion2/train/Images/ --label /home/rajput/Desktop/deepfashion2coco_to_yolo_/Dataset_Deepfashion2/train/train.json --convert_output_path YOLO/ --img_type ".jpg" --manipast_path ./ --cls_list_file coco.names

set path according to your system. then run


And after this you create data folder and paste our YOLO (which is generated .txt files) folder content And Images folder content.


And now we setup will set Yolo3 project. first we need to install Darknet Click here

or 

cmd :- git clone https://github.com/pjreddie/darknet.git

cmd :- cd darknet

cmd :- make

cmd :- cd..

After this you clone my project on github 

cmd :- https://github.com/Manishsinghrajput98/yolo_training.git
 
cmd :- cd yolo_training

And after this we need to install pre-trained yolo v3 model Click here

or 

open your terminal 

cmd :- wget https://pjreddie.com/media/files/darknet53.conv.74
 
Then copy all the content of your clone yolo_training folder also downloaded pre-trained model files And, data folder (which is content images and txt files) to darknet folder 

After that you run one python script to generate train.txt and test.txt files.

cmd :- python process.py data  

Data folder which is indicate as a arguments (content images and txt files)

after that the folder looks like 



Finally we are ready for yolo model training. one more point you have to change the path of the train.txt and test.txt , backup folder, and .cfg files which is mention on tast.data files

After that we 

cmd :- ./darknet detector train /home/rajput/Desktop/darknet/Task/task.data /home/rajput/Desktop/darknet/Task/task.cfg /home/rajput/Desktop/darknet/Task/darknet53.conv.74

if you get this type of error 

/bin/bash: ./darknet: Is a directory.

so you add darknet keyword (There is a darknet file inside the darknet directory which you need to use and you need to give that darknet file's path (not the directory's path))

cmd:- ./darknet/darknet detector train /home/rajput/Desktop/darknet/Task/task.data /home/rajput/Desktop/darknet/Task/task.cfg /home/rajput/Desktop/darknet/Task/darknet53.conv.74



if you want to use GPU so mention this keywords (-gpus 0) multiple GPU use (-gpus 0,1)

cmd:- ./darknet/darknet detector train /home/rajput/Desktop/darknet/Task/task.data /home/rajput/Desktop/darknet/Task/task.cfg /home/rajput/Desktop/darknet/Task/darknet53.conv.74 -gpus 0             ## change path according your system  
  
and resume training so use backup folder last weights

cmd :-  ./darknet/darknet detector train /home/rajput/Desktop/darknet/Task/task.data /home/rajput/Desktop/darknet/Task/task.cfg /home/rajput/Desktop/darknet/Task/backup/task.backup

## change path according your system  

you can edit .cfg files according your dataset, accuracy, system performance (like batch size, subdivisions etc ) 

and now test the model using this command 

cmd :- ./darknet detector test /home/rajput/Desktop/darknet/Task/task.cfg /home/rajput/Desktop/darknet/Task/task_30000.weights /home/rajput/Desktop/darknet/Task/0a0c3c07-3d52-418b-b589-9b670caae173.jpg -thresh 0.1        ## change path according your system  

Also you can Create Flask API for real time projects. and if you comment on this comment section so i will be providing full detection api for this so you can use real time projects.

Thanks.

Saturday, July 11, 2020

Do we need laptop for Python course in udemy?

Yes need laptop.


Because Practice is more important as compare to watch videos.

You can use smart phone for watching videos and for practice we need laptop or system.

Now python is everywhere like web development , data science , data analytics,
Artificial intelligence.

you can run step by step using this udemy tutorial.

You can get free Udemy course for Python, Machine Learning, Web Development  (Django, Flask ) Data Science  Click here .

Note :- You start learning  python programming language first you watch videos on smart phone. smart phone also provide python editor to run small programs. After this you need laptop with high specification if you want to switch on machine learning.

Thanks.
  

Image Alignment using Features Matching with real time flask api

Features matching or generally image matching, a part of many computer vision applications such as image registration, camera calibration and object recognition, is the task of establishing correspondences between two images of the same scene/object. 

A common approach to image matching consists of detecting a set of interest points each associated with image descriptors from image data. Once the features and their descriptors have been extracted from two or more images.


matching methods based on interest points depends on both the properties of the underlying interest points and the choice of associated image descriptors.


You can more read Click here.

Now come to the Project. first you need to clone my project Click here. or 

cmd :- git clone https://github.com/Manishsinghrajput98/image_alighnment.git

cmd :- cd image_alighnment

we are using python 3 for this you have to create virtual environment

cmd :- virtualenv test

cmd :- source test/bin/activate

cmd :- pip install -r requirements.txt 

cmd :- python main.py 

(default 3001 Port)  or with other port

cmd :-  python main.py --port 3002 

now you can see the terminal like this 


after this you need to call API 

Note : - Friends in software industry  everyone using API for anything detection,classification, and any back-end result show in front end. so we will using Flask API our every new blogs.

we are using Postman for hit our api.

Parameter of our api 

{
"image_path":"/home/rajput/Desktop/blogs_folder/image_alighnment/input.JPEG"
}

(you change this path according your image path)

Address of our api 

http://localhost:3002/start

if you dont have any idea about postman so you can Click here.

after you hit this api you can see.



you successful hit api. so now you can see your terminal


and result you can see your clone project. image name output.jpg this is our final output result.


and this is the input image and output image you can compare both images


There is many application of this project likes user registration. only you need a front-end so you can create a web application for this. if you need this type of projects so i will implement.

Note :- In this project we are using two images one is reference and second is input image which will hit on postman. you can change reference image and input image according your requirement.                                                                                                          
Thanks.

Thursday, July 9, 2020

Deepfashion2 Dataset implementation step by step with python code also providing my trained model

In this blog we will implement DeepFashion2 Dataset. we will use Mask Rcnn for train our own Fashion Model. I will providing all source code on in this blog.

It contains 13 popular clothing categories from both commercial shopping stores and consumers. 
'short_sleeved_shirt', 'long_sleeved_shirt', 'short_sleeved_outwear', 'long_sleeved_outwear', 'vest', 'sling', 'shorts', 'trousers', 'skirt', 'short_sleeved_dress', 'long_sleeved_dress', 'vest_dress', 'sling_dress'
this Dataset use for detection, segmentation etc. you can read more


Training images: train/image Training annotations: train/annos

Validation images: validation/image Validation annotations: validation/annos

Test images: test/image

Implementation

This is the information of Deepfashion2 Dataset. now i will show step by step  implementation with source code you can copy this code and paste on your Pycharm Python, or any ids.

First we need to download the Dataset of Deepfashion2.

Download 

you can click on this side Click here.. it takes time because the file size is large. you need to download 

1. train.zip (10 GB)

2. validation.zip (2 GB)

After download these files. you need to unzip this files but you need password for this.
You need fill in the form to get password for unzipping files Click here. after you fill this form.

You will  received Email from yyge13@gmail.com which is email content password for unzip the zip files. you can unzip the zip files one by one.

Note : - ( if you not receive password email you can mail me.)

After unzips the zip files you get images and corresponding Json files. 

In this blog we will use Mask_Rcnn to train custom model using Deepfashion2 Dataset 





and further any other Blogs we will use YOLO and Deeplabs model.

we need to convert this Dataset to coco format. we need Python script to convert coco format. 

In python script you must be insert the path of train unzip Dataset images folder (which is content images) and json folder (which is content json files corresponding image). also insert the path of empty train.json file.

This empty train.json file you can create manually or your choice you want to create. but we need to insert this 3 paths to convert coco format for training.

Unzip train folder :-

1). Image folder (which is given on unzip train folder).

2). Json folder ( which is given on train folder).

3). Empty json files ( create manually name likes train.json ).

and also we need same process for unzip validation folder. 

we need to execute python script one more time for convert validation folder to coco format.

Unzip validation folder :- 

1). Image folder (which is given on unzip validation folder).

2). Json folder ( which is given on validation folder).

3). Empty json files ( create manually name likes val.json ).

Remember :- 

(1). Please change Line Number 114 for ( you want to select number of images for training or you can set default values validation 32,153 and train 1,91,961 ).
(2). Line Number 118 and 119 Path for ( your unzip train and validation folder path according your system).
(3). Line Number 224 Path for ( your manually created train.json files and val.json files).

Source Code : -  In my Github Page Click here.

This python script must be done 2 times one for train unzip folder and second is unzip validation folder According your both folder path in your system with line number.

Finally you get 2 files after run deepfashion2coco.py files for 2 times one for train and second is validation 

You Get

1). train.json
2). val.json
 
After you get this files you successful done coco format conversion. 

You create dataset folder manually then create 2 more folder in dataset folder one is train and second is validation. then paste the train.json or train images  on train folder similar for validation folder.

we ready for train the model on GPU.   

your Dataset folder file structure look likes :
dataset
    1 ) train
          I) images
          II) train.json
    2) validation 
         I) images
         2)val.json         

First we need clone the projects on your system or any AWS machine. 

Clone Projects on my Github Repository Click here.

or cmd :- https://github.com/Manishsinghrajput98/Deepfashion2_Training.git

we are using python 3 for training. you create virtual environment using python 3

cmd :-  virtualenv -p python3 localenv

cmd :-  source localenv/bin/activate

now you install requirement.txt which mentioned on projects.

cmd :- cd Deepfashion2_Training

cmd :- pip install -r requirement.txt

Note :- If you get any types of error during installation. you can run this script and install package on Run time if you get error for no module found just install these package using pip.

I have train my model according this requirement.txt package. i know there are many update on Packages you need to follow updated packages. don't worry the updation is not reflect your training.
a
And also we need to Download pre-trained mask rcnn model train on  the COCO dataset. to download these model and past on clone project and also create logs folder. 

After download you get this files mask_rcnn_coco.h5 this files paste on your clone project and we need to create logs folder manually and also need to paste our dataset folder on clone projects 

if you have any problem to install pycocotools package on your virtual environment to solved issue or  Click here.

before doing above steps install cython  

cmd :-  pip install cython
cmd :- git clone https://github.com/pdollar/coco.git
cmd :- cd coco/PythonAPI
cmd :- make 
cmd :- sudo make install
cmd :- sudo python setup.py install

we ready to train model using Deepfashion2 Dataset.

After finish all work your clone projects files structure look likes 

Deepfashion2_Training
(1) dataset
    (I) train
        i.images
        ii.train.json
    (II) validation
        i.images
        ii.train.json
(2) source 
(3) tools
(4) lib
(5) main.py
(6) model_test.py
(7) requirement.txt


Befor training you need to change on main.py python file. Line Number 33, 40, 41, 42, 43, 212.

Line 33 :- GPU_COUNT = 2  (according to your system capability)

Line 40 :-  train_img_dir = "/home/asa/projects/datasets/train/image" (according to your system path which is our unzip train folder)

Line 41 :- train_json_path = "/home/asa/projects/match_rcnn/tools/train.json" (according to your system path which is our train.json which is generated to coco conversion)

Line 42 :- valid_img_dir = "/home/asa/projects/datasets/validation/image" (according to your system path which is our unzip validation folder)

Line 43 :- valid_json_path = "/home/asa/projects/match_rcnn/tools/valid.json" (according to your system path which is our val.json which is generated to coco conversion)

Line 212 :- Number of Epochs default 30 (you can change according your result or system capability) 

New model starting from pre-trained COCO weights
cmd :- python main.py train --weights=coco

Resume training on last trained model
cmd :- python main.py train --weight=last

Run this command on terminal with activate your virtual environment in Deepfashion2_Training folder 


show this type of images on your terminal you successful training start and after complete 1 Epochs your model save on logs folder (which you create manually)  

After train the model you get the model in your logs folder just paste the path of your last weights file and also images path which is you test and run on your terminal

Source code on my github page 

cmd :- python model_test.py 

your result show on images 



This is the simple script you need to used these script for images and save images on your system  and Also i am providing scripts to Test your model on random videos.

This script i have used on google colab. you can try on your system or colab.

Before this you need to mount to drive. it's not difficult. you just click mount drive button in google Colab. also select the GPU options in google colab.

For Images In Google Colab :- Replaced the path of your local system ,colab, drive. 

Also you need to upload your train model on drive and replace the path of your trained model in python scripts.

cmd :- !python deepfashion_images.py --i /content/00ac770f-055c-4f3f-9681-669926a263ef_91.jpg --o /content/output_test

--i : - Path of input images
 
--o :- Path of output images

After run this script on google colab. the input images after detected save on google colab in output folder. you can Also store Detected images on google drive just insert the path of output images in command line.

For Videos In Google Colab :-  Replaced the path of your local system ,colab, drive.

cmd :- !python deepfashion_videos.py --i /content/video_test.mp4

--i :- path of input videos

After run this script you will received the output video output.avi.

Note :- If you don't want to use google colab. you can try on your machine.

Official GitHub Repository of Deepafashion2 Dataset  Click here. or Click here

Also you can create Flask API for Mask-RCNN DeepFashion2 Machine Learning Model.
if you want to Flask API using python Click here
 
Short videos (Youtube


Note :- I have trained model with 100 Epochs. If you need my trained model. Click here

Results of My models with 100 Epochs.




Thanks if you have any doubt please comment.