Skip to content

Commit

Permalink
Add base64 encoded image support
Browse files Browse the repository at this point in the history
  • Loading branch information
laurynas committed Apr 21, 2024
1 parent 5a89165 commit d01ec53
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import base64
from PIL import Image
from src.digitizer import Digitizer
from src.routing import IdentifierConverter
Expand All @@ -22,7 +23,7 @@ def digitize():
decimals = request.args.get('decimals', default=0, type=int)
threshold = request.args.get('threshold', default=0.7, type=float)

image = Image.open(BytesIO(request.get_data()))
image = __request_image()
value, _ = digitizer.detect(image, decimals, threshold)

if value is None:
Expand All @@ -36,7 +37,7 @@ def update_meter(meter_id):
threshold = request.args.get('threshold', default=0.7, type=float)
max_increase = request.args.get('max_increase', default=float('inf'), type=float)

image = Image.open(BytesIO(request.get_data()))
image = __request_image()
value, objects = digitizer.detect(image, decimals, threshold)

if value is None:
Expand Down Expand Up @@ -94,3 +95,12 @@ def reset_meter(meter_id):
storage.store(meter_id, value, blank_image, blank_image)

return 'Meter reset', 200

def __request_image():
image_data = request.get_data()

if 'base64' in image_data.decode('utf-8'):
image_data = image_data.decode('utf-8').split('base64,')[1]
image_data = base64.b64decode(image_data)

return Image.open(BytesIO(image_data))

0 comments on commit d01ec53

Please sign in to comment.