Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
71 lines (50 sloc) 1.91 KB
import object_detection_api
import os
from PIL import Image
from flask import Flask, request, Response
app = Flask(__name__)
# for CORS
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,POST') # Put any other methods you need here
return response
def index():
return Response('Tensor Flow object detection')
def local():
return Response(open('./static/local.html').read(), mimetype="text/html")
def remote():
return Response(open('./static/video.html').read(), mimetype="text/html")
def test():
PATH_TO_TEST_IMAGES_DIR = 'object_detection/test_images' # cwh
TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3)]
image =[0])
objects = object_detection_api.get_objects(image)
return objects
@app.route('/image', methods=['POST'])
def image():
image_file = request.files['image'] # get the image
# Set an image confidence threshold value to limit returned data
threshold = request.form.get('threshold')
if threshold is None:
threshold = 0.5
threshold = float(threshold)
# finally run the image through tensor flow object detection`
image_object =
objects = object_detection_api.get_objects(image_object, threshold)
return objects
except Exception as e:
print('POST /image error: %e' % e)
return e
if __name__ == '__main__':
# without SSL, host='')
# with SSL, host='', ssl_context=('ssl/server.crt', 'ssl/server.key'))