Skip to content

Commit

Permalink
Merge pull request #75 from sstratoti/min_confidence
Browse files Browse the repository at this point in the history
Added min_confidence. Fixed bounding box issue.
  • Loading branch information
robmarkcole authored Oct 28, 2022
2 parents 526f4ef + 78918b6 commit cf437f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
48 changes: 26 additions & 22 deletions coral-app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io
import os
import logging
import time

import flask
from PIL import Image
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
app.run(host="0.0.0.0", debug=False, port=args.port)

0 comments on commit cf437f4

Please sign in to comment.