Skip to content

Commit

Permalink
Handle empty reading
Browse files Browse the repository at this point in the history
  • Loading branch information
laurynas committed Mar 17, 2024
1 parent 127cead commit 9b3e552
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
@app.route('/digitize', methods=['POST'])
def digitize():
value, _, _ = _detect()

if value is None:
return 'No reading found', 400

json_data = json.dumps({'value': value})

return json_data, 200, {'Content-Type': 'application/json'}
Expand All @@ -27,6 +31,9 @@ def digitize():
def update_meter(meter_id):
value, objects, image = _detect()

if value is None:
return 'No reading found', 400

json_file = os.path.join(data_dir, f'{meter_id}.json')
max_increase = request.args.get('max_increase', default=float('inf'), type=float)

Expand Down Expand Up @@ -80,10 +87,15 @@ def reset_meter(meter_id):

def _detect():
decimals = request.args.get('decimals', default=0, type=int)
threshold = request.args.get('threshold', default=0.7, type=float)
data = request.get_data()
image = Image.open(BytesIO(data))
reading, objects = digitizer.detect(image)
value = float(reading) / (10 ** decimals)
reading, objects = digitizer.detect(image, threshold)

if len(reading):
value = float(reading) / (10 ** decimals)
else:
value = None

return value, objects, image

Expand Down

0 comments on commit 9b3e552

Please sign in to comment.