Skip to content

Commit

Permalink
Store readings
Browse files Browse the repository at this point in the history
  • Loading branch information
laurynas committed Mar 16, 2024
1 parent 7edda27 commit a0b86b6
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from src.route_converters import IdentifierConverter
from io import BytesIO
from flask import Flask, request
import json

model = 'models/yolov8-detect-20240229.onnx'
data_dir = 'data/'
Expand All @@ -21,14 +22,34 @@ def digitize():

@app.route('/meter/<identifier:meter_id>', methods=['POST'])
def update_meter(meter_id):
data = request.get_data()
json_file = os.path.join(data_dir, f'{meter_id}.json')
decimals = request.args.get('decimals', default=0, type=int)
max_increase = request.args.get('max_increase', default=float('inf'), type=float)

data = request.get_data()
image = Image.open(BytesIO(data))

reading = digitizer.detect_string(image)
value = float(reading) / (10 ** decimals)

if os.path.exists(json_file):
with open(json_file, 'r') as f:
data = json.load(f)
if value < data['value']:
return 'Reading is lower than previous', 400
if value - data['value'] > max_increase:
return 'Reading increased too much', 400

with open(json_file, 'w') as f:
json.dump({'value': value}, f)

image.save(os.path.join(data_dir, f'{meter_id}.jpg'))
return digitizer.detect_string(image)

return str(value), 200, {'Content-Type': 'text/plain'}

@app.route('/meter/<identifier:meter_id>/image', methods=['GET'])
def meter_image(meter_id):
image_path = os.path.join(data_dir, f'{meter_id}.jpg')
if not os.path.exists(image_path):
return 'Image not found', 404
return 'Meter not found', 404
return open(image_path, 'rb').read(), 200, {'Content-Type': 'image/jpeg'}

0 comments on commit a0b86b6

Please sign in to comment.