diff --git a/README.md b/README.md old mode 100644 new mode 100755 index b91c095..caceb53 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ $ python3 coral-app.py --models-directory models ``` Then use curl to query: ``` -curl -X POST -F image=@images/test-image3.jpg 'http://localhost:5000/v1/vision/detection' +curl -X POST -F image=@images/test-image3.jpg -F min_confidence=.5 'http://localhost:5000/v1/vision/detection' {'predictions': [{'confidence': 0.953125, 'label': 'person', diff --git a/coral-app.py b/coral-app.py index 213b9cd..7202282 100644 --- a/coral-app.py +++ b/coral-app.py @@ -7,6 +7,7 @@ import io import os import logging +import time import flask from PIL import Image @@ -39,39 +40,42 @@ def predict(): data = {"success": False} if flask.request.method == "POST": + if flask.request.form.get('min_confidence'): + threshold=float(flask.request.form['min_confidence']) + else: + threshold=float(0.4) if flask.request.files.get("image"): image_file = flask.request.files["image"] image_bytes = image_file.read() image = Image.open(io.BytesIO(image_bytes)) - - size = common.input_size(interpreter) - image = image.convert("RGB").resize(size, Image.ANTIALIAS) - - # Run an inference - common.set_input(interpreter, image) - interpreter.invoke() _, scale = common.set_resized_input( interpreter, image.size, lambda size: image.resize(size, Image.ANTIALIAS)) - - threshold=0.4 + + # Run an inference + #start = time.perf_counter() + interpreter.invoke() + #inference_time = time.perf_counter() - start objs = detect.get_objects(interpreter, threshold, scale) - + #logging.debug('%.2f ms' % (inference_time * 1000)) if objs: data["success"] = True preds = [] for obj in objs: - preds.append( - { - "confidence": float(obj.score), - "label": labels[obj.id], - "y_min": int(obj.bbox[1]), - "x_min": int(obj.bbox[0]), - "y_max": int(obj.bbox[3]), - "x_max": int(obj.bbox[2]), - } - ) + if float(obj.score) >= float(threshold): + preds.append( + { + "confidence": float(obj.score), + "label": labels[obj.id], + "y_min": int(obj.bbox.ymin), + "x_min": int(obj.bbox.xmin), + "y_max": int(obj.bbox.ymax), + "x_max": int(obj.bbox.xmax), + } + ) data["predictions"] = preds + else: + logging.debug('No objects detected') # return the data dictionary as a JSON response return flask.jsonify(data) @@ -107,6 +111,6 @@ def predict(): global interpreter interpreter = edgetpu.make_interpreter(model_file) interpreter.allocate_tensors() - print("\n Initialised interpreter with model : {}".format(model_file)) + logging.debug("\n Initialised interpreter with model : {}".format(model_file)) - app.run(host="0.0.0.0", debug=True, port=args.port) \ No newline at end of file + app.run(host="0.0.0.0", debug=False, port=args.port) \ No newline at end of file