Without Tensorflow: Web app with Object detection api in Heroku and openCV
Its hard to serve an object detection model in the public web and also money and speed matters.
https://objectdetectiontut.herokuapp.com/
Most common solutions are Amazon Web Services (AWS) — Cloud Computing Services , Google Cloud ,but its not easy to implement at least for free.
Here comes Heroku: Cloud Application Platform ,Its is very easy to use ,and most popular for small web app and free account 😵, no tension about server configuration also supports all popular web technologies.
So there is no TensorFlow means no Tensorflow Object Detection API ,So are we going to use pytorch, cafee ? Or back to old days with template matching or HOG with openCV
No??? WE must use Deep Learning , Because it is cool, But makes machines hot , So we have to put it in others machines known as CLOUD
So we will be using openCV DNN (version = 3.4.1 and up must ) module which will take care of the TensorFlow models. OpenCV DNN
Note: Now only ssd_mobilenet_v1_coco and inception models are supported from Tensorflow Model Zoo , list can be found here https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API
To use a tensorflow model you have to generate a config file from it ,this instruction is officially given.
Generate a config file
Use tf_text_graph_ssd.py script to generate a text graph representation. If your model has different values of num_classes
, min_scale
, max_scale
, num_layers
or aspect_ratios
comparing to origin configuration files, specify it in the script arguments.
We will be using their ssd model which is given in the link. Its has two part model weights and config .We will use frozen_graph and config
The Web Part
We will be using python flask for web part
I have made a base code so that it can be easily implemented in the heroku, with any ssd mobilenet based models.
code LINK
Mainly the project has 4 parts
- Model files
- Flask codes
- ObjectDetection codes
- Heroku configs
As we are using openCv there is some OS level dependencies in heroku so it need additional buildpack , base buildpack is python LINK and additional is apt buildpack LINK
apt installations are included in the Aptfile and python libraries are included in requirements.txt
There will be a image upload form and it will return a object detected image.Thats simple [While working on local machine large image may not work but it will work on heroku]
The output will be
Code walkthrough
Flask image upload and detected outputs are done thorough runtime memory which is in app.py file, it also handles web requests from index.html file which is in templates.
our main concern is ObjectDetector.py
[code snippets are taken from https://www.pyimagesearch.com/ and modified according to need, one of the best resources on computer vision by Adrian Rosebrock]
classNames is dictionary containing objects labels trained in this model to detect.Models and config files are loaded from directory, as it is tensorflow model cv.dnn.readNetFromTensorflow is used to read them.
Then the images are converted from numpy array to opencv images and input as blob with some parameter tuning according to the model. then the input is forwarded to the network to get detection result, it a huge ammount of number ,only needed values are parsed according to threshold (0.5 is given change according to your need), detection results gives label id , bouding box coordinates and confidence or possibility level for the object. These results are drawn to the uploaded image and converted to bytes and returned to flask ,to show result.
Running on Heroku:
Register an account in https://www.heroku.com/
Then create an app I am giving name objectdetectiontut
Then goto deploy [you can choose any method according to your preference but I feel comfortable with github as my code is already uploaded]
Connect to your github account and search for your repo and connect
As we mentioned earlier we need two buildpacks python buildpack will be automatically added but apt buildpack should be manually added or the app will give you error , you call see that in the logs
To add buildpacks you have to go to the settings and add buildpacks paste there this
https://github.com/heroku/heroku-buildpack-apt
Then goto deploy and deploy branch ,[you can add automatic deploy also for future update].
wait for some time !!!!!
Wow our project is up in the cloud and its pretty fast [without upload time]
https://objectdetectiontut.herokuapp.com/
code LINK
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Find me:
Github: https://github.com/rdeepc