Skip to content

Instantly share code, notes, and snippets.

@snobu
Created May 20, 2018 11:12
Show Gist options
  • Select an option

  • Save snobu/3d3fce337cffd769a5a2b01d8f502051 to your computer and use it in GitHub Desktop.

Select an option

Save snobu/3d3fce337cffd769a5a2b01d8f502051 to your computer and use it in GitHub Desktop.
falcon-testing
#!/usr/bin/env python3
import falcon
from images import Resource
api = application = falcon.API()
images = Resource(None)
api.add_route('/images', images)
import io
import os
import uuid
import mimetypes
import json
import falcon
class Resource(object):
_CHUNK_SIZE_BYTES = 4096
# The resource object must now be initialized with a path used during POST
def __init__(self, storage_path):
#self._storage_path = 'storage_path'
self._storage_path = 'uploads'
# This is the method we implemented before
def on_get(self, req, resp):
# serve jpeg/png result with bounding boxes from ./results
# THIS IS PROBABLY COMPLTETELY WRONG as req.path is /images/something/ not the disk path
with io.open(req.path, 'rb') as predictions_image:
fileContent = predictions_image.read()
# should we settle on jpeg for darknet output?
# png looks better (boxes and text) but it's huge in size
resp.content_type = falcon.MEDIA_JPEG
resp.body = fileContent
resp.status = falcon.HTTP_200
def on_post(self, req, resp):
ext = mimetypes.guess_extension(req.content_type, strict=True)
# Stupid Python shiitake coding
if ext == '.jpe': ext = '.jpg'
# End shittake, yeah, fuggedaboutit
name = '{uuid}{ext}'.format(uuid=uuid.uuid4(), ext=ext)
image_path = os.path.join(self._storage_path, name)
with io.open(image_path, 'wb') as image_file:
while True:
chunk = req.stream.read(self._CHUNK_SIZE_BYTES)
if not chunk:
break
image_file.write(chunk)
# Here comes the detection step,
# a few lines of darknet import and path setup and a call to
# detect('uploads/fresh_upload.jpg')
# Fuggedaboutit.
resp.status = falcon.HTTP_201
resp.location = '/results/' + name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment