Blog

ProjectBlog: Building your own Machine Learning API Gateway — using Python/Flask Part I

Blog: Building your own Machine Learning API Gateway — using Python/Flask Part I


Go to the profile of oZoneDev

Part I — baby steps, decisions and the skeleton

For those who’ve been following my work, I’ve recently been doing machine learning based recognition for ZoneMinder and having a lot of fun along the way. Basically, my home security system now does better and relevant detections than many other commercial systems I’ve tried in the past.

Anyway, given that I’m reusing a lot of the object detection code in multiple places, I figured it might be really useful for me to be able to deploy a simple HTTP API gateway that different components can call, instead of having to embed the same code in different programs.

My goals:

  • I call something like http://mygateway/api/detect and pass it an image (how to pass an image is not important right now)
  • It would spit out nice data like so, for face/gender:
[{“type”: “face”, “confidence”: “52.87%”, “box”: [904, 1037, 1199, 1337], “gender”:
“man”, “gender_confidence”: “99.98%”}

Or so, for objects:

[{"type": "bear", "confidence": "99.40%", "box": [6, 184, 352, 436]}, {"type": "bear
", "confidence": "72.66%", "box": [615, 219, 659, 279]}]

and I could draw them out on video feeds like so:

You know, just like those AWS and/or Google APIs for image recognition, except they are in my local LAN and I’m not paying per image.

TL;DR

I get it, you wan’t to get going right away. In that case, head over to https://github.com/pliablepixels/mlapi and enjoy.

There are people in the world who find actual development daunting

While building APIs are easy for most developers, I’m not really a professional developer. My day job doesn’t involve coding at all (I am into Product Management and painting iconic visions of how technology will shape the future — yeah, I get paid to wave my hands, but I do try and make sense when I can). While it is my job to keep abreast of technologies in the work I do, talking about tech isn’t the same as doing the tech. Every coding project of mine has been a self-initiated learn from scratch effort. Plus given I don’t code in my day job, after I learn/develop something, I often forgot all about it after a few months. Completely.


So back to how to develop an API gateway: The tech

When it comes to web development, the choices are daunting. There are a million frameworks and a zillion opinions on which to adopt. My logic was simple:

  • Got to be simple to use
  • Reasonably idiot proof
  • Should be Python based (because most of the ML libs I use are also in Python)
  • Look if anyone else already did it

Did anyone else do it?

I first googled around and realized that for Python, there are two dominant frameworks. Flask and Django. I took a brief look at both and liked the fact that flask was compact and seemed easy. My kind of framework. I looked at Django and thought it was too cumbersome to use for my project. So mentally, I already wanted to try Flask. But then I decided, hey let’s look around to see if someone already did this API.

Lots of people have. I’ll admit, there was a part of me that wanted to learn and do it myself. But I found this post by Adrian that said “build your own API in 5 minutes”. Wow he did that in 5 minutes? It would take me at least a day. I read through it, but did not download the code. Plus as I wrote above, I was mentally mostly convinced to do this in flask. Then I also read in Adrian’s comments that his post was in 2015, and would not work with new updates without changes, so I figured thats enough of googling. Let me try it myself. I bet there would be other flask based API gateways around.

API gateway: Technology approaches

Right, so that was decided.

  • A micro web framework that plays well with python. What’s a micro framework? Basically something that’s minimal and lightweight.
  • Authentication — I keep reading all the cool kids do JWT. So I figured I’ll learn how to actually create a JWT based solution.
  • Database — really, all I need for this solution is a simple user database. It’s not going to be hosted for thousands of users. Just a few. Famous last words.

API gateway: Software

So the list of software:

  • Flask/Flask_restful for the API gateway
  • TinyDB for my simple DB needs. Oh, so this is just a JSON file. Hmm, in that case, lets go with bcrypt to encrypt the password. It’s a known, respected algorithm and there are posts abound on why bcrypt is preferred over SHAxxx for passwords. I’m not a security guru. I’ve always learned to lean on experts in specific areas than make my own choices. So obviously, I now needed a reliable library to do it for me. Some more googling, and I settled on passlib.
  • flask_jwt_extended for JWT based access tokens (I started with another flask library and realized later it was not maintained)
  • I already knew how to code for object detection — the core ML libs, given my recent other project was the ML server for ZoneMinder but I decided to use cvlib by Arun Ponnusamy — I looked at this library earlier and really liked it. Plus he had gender recognition built in. I may replace it with my own libraries that I developed earlier later (especially since I’ve already integrated Dlib’s face recognition, which I find better than the other approaches).

Let’s get started: The environment

I’ll be honest. I dislike the necessity of “virtual environments”. The entire notion, that for every project, I should create a virtual environment so library versions don’t collide and resolve potential incompatibilities is innately a terrible idea in my mind. Why should I have to install different versions of the same library? It’s such a waste of storage space.

However, that is reality. Unix/Linux/Python/Perl/Node (oh my, Node’s family of tools is a disaster here) all suffer from library version issues, not to mention the Python2 vs Python3 issue. Not to get into this more, except to say “Ya gotta do what ya gotta to”. You could just do all the next steps without creating a virtual environment, but when you start seeing all sorts of library not found, or version mismatch dependency errors, you’ll know you should have listened.

There are several tutorials on how to set up your own. I already had a virtual environment configured for machine learning projects due to my previous work, but Arun’s post on how to go about it in OSX is simple and easy. There are other tutorials for other host OSs. Just Google.

The ‘Hello World’ to get my API working

Right, so here I am sitting in my virtual environment, ready to get started. I ‘ve even made my own directly. Yay.

(ml) [pp@pp-MacBook-Pro:~/fiddle/mlapi]$

Right, so I know I am going to use “Flask”. A bit of googling, and I was told by the Smart People of The Internet © that I should use “Flask-restful”, okay, so:

pip install flask flask-restful

And then, literally, I just copied this example info a file called api.py and ran it

For those who are too lazy to click the link:

from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
if __name__ == '__main__':
app.run(debug=True)

And ran it:

python ./foo.py
* Serving Flask app "foo" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 241-590-371

Bazinga. I fired up a browser to http://127.0.0.1:5000 and boom. I had a JSON response.

{
"hello": "world"
}

I. HAD. AN. API. GATEWAY. That’s it. I can now go create Facebook.

Right, now that I figured out the basic construct of how the API works with flask, lets get into how to connect the wiring.

Part II coming soon….

Source: Artificial Intelligence on Medium

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
a

Display your work in a bold & confident manner. Sometimes it’s easy for your creativity to stand out from the crowd.

Social