Skip to content

Instantly share code, notes, and snippets.

@jcjohnson
Created January 28, 2016 03:58
Show Gist options
  • Save jcjohnson/fb28a14d9157a21d430b to your computer and use it in GitHub Desktop.
Save jcjohnson/fb28a14d9157a21d430b to your computer and use it in GitHub Desktop.
import argparse, random, os, time, json
from PIL import Image
from io import BytesIO
import base64
from flask import Flask, request
from flask.ext.cors import CORS
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
CORS(app)
input_dir = 'inputs'
output_dir = 'outputs'
class DenseCap(Resource):
def post(self):
img_id = random.randint(1, 1000000)
img_name = os.path.join(input_dir, '%d.jpg' % img_id)
# Get the base64 image data out of the request.
# for some reason Flask doesn't parse this out at all for use, so we'll just
# do it manually. There is a prefix telling us that this is an image and the
# type of the image, then a comma, then the raw base64 data for the image.
# We just grab the part after the comma and decode it.
idx = request.data.find(',') + 1
img_data = request.data[idx:]
im = Image.open(BytesIO(base64.b64decode(img_data)))
im.save(img_name)
# request.files['image'].save(img_name)
json_name = os.path.join(output_dir, '%d.json' % img_id)
while not os.path.isfile(json_name):
time.sleep(0.05)
with open(json_name, 'r') as f:
ann = json.load(f)
return ann
api.add_resource(DenseCap, '/')
parser = argparse.ArgumentParser()
parser.add_argument('--with_ssl', action='store_true')
args = parser.parse_args()
if __name__ == '__main__':
# app.run(debug=True)
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
if args.with_ssl:
http_server = HTTPServer(WSGIContainer(app), ssl_options={
'certfile': 'ssl/server.crt',
'keyfile': 'ssl/server.key'
})
else:
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)
# We have to do a little weirdness to make the server actually die
# when we hit CTRL+C
try:
IOLoop.instance().start()
except KeyboardInterrupt:
IOLoop.instance().stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment