Skip to content

Commit

Permalink
Store image
Browse files Browse the repository at this point in the history
  • Loading branch information
laurynas committed Mar 16, 2024
1 parent 03150da commit 7edda27
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.git
samples/
data/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

data/*
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ FROM python:3.12-slim
WORKDIR /app

ADD requirements.txt /app

RUN pip install --no-cache-dir -r requirements.txt

ADD . /app
Expand Down
25 changes: 21 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import os
from PIL import Image
from src.digitizer import Digitizer
from src.route_converters import IdentifierConverter
from io import BytesIO
from flask import Flask, request

MODEL_FILE = 'models/yolov8-detect-20240229.onnx'
model = 'models/yolov8-detect-20240229.onnx'
data_dir = 'data/'

digitizer = Digitizer(model)

digitizer = Digitizer(MODEL_FILE)
app = Flask(__name__)
app.url_map.converters['identifier'] = IdentifierConverter

@app.route('/digitize', methods=['POST'])
def digitize():
data = request.get_data()
image = Image.open(BytesIO(data))
result = digitizer.detect_string(image)
return result
return digitizer.detect_string(image)

@app.route('/meter/<identifier:meter_id>', methods=['POST'])
def update_meter(meter_id):
data = request.get_data()
image = Image.open(BytesIO(data))
image.save(os.path.join(data_dir, f'{meter_id}.jpg'))
return digitizer.detect_string(image)

@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 open(image_path, 'rb').read(), 200, {'Content-Type': 'image/jpeg'}
Empty file added data/.keep
Empty file.
6 changes: 6 additions & 0 deletions src/route_converters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from werkzeug.routing import BaseConverter

class IdentifierConverter(BaseConverter):
def __init__(self, url_map, *items):
super(IdentifierConverter, self).__init__(url_map)
self.regex = r"[0-9a-zA-Z]{1,20}"

0 comments on commit 7edda27

Please sign in to comment.