diff --git a/app.py b/app.py index 772b332..83e3049 100755 --- a/app.py +++ b/app.py @@ -1,7 +1,10 @@ #!/usr/bin/env python from flask import Flask, redirect, request, render_template, send_from_directory -from . import db +import os +from os.path import join +import uuid +from . import db, config, scanner app = Flask(__name__) @@ -17,7 +20,8 @@ def hello_world(): def create_object(): conn = db.get() with conn: - db.Object.create(request.form.get('name'), conn) + object = db.Object.create(request.form.get('name'), conn) + os.makedirs(join(config.DATA_DIR, str(object.id), 'previews')) return redirect('/') @@ -28,6 +32,24 @@ def object(id: int): return render_template('object.html', object=object) +@app.route("/calibrate/") +def calibrate(id: int): + conn = db.get() + object = db.Object.get_from_id(id, conn) + return render_template('calibrate.html', object=object) + + +@app.route("/api/preview/") +def preview(id: int): + conn = db.get() + db.Object.get_from_id(id, conn) + capture_uuid = uuid.uuid4() + if scanner.capture(join(config.DATA_DIR, str(id), 'previews', str(capture_uuid) + '.jpg')): + return str(capture_uuid) + else: + return "Impossible de capturer l'image.", 500 + + @app.route("/calibration/") def calibration(id: int): conn = db.get() diff --git a/config.py b/config.py new file mode 100644 index 0000000..b16d621 --- /dev/null +++ b/config.py @@ -0,0 +1,2 @@ +DATA_DIR = 'data' + diff --git a/db.py b/db.py index c1a7313..6b94240 100755 --- a/db.py +++ b/db.py @@ -64,7 +64,7 @@ class Object: 'INSERT INTO object(name, calibrated) VALUES (?, ?) RETURNING ' + Object.select_args() + ';', [name, 0] ) - return response.fetchone() + return Object.from_row(response.fetchone()) def save(self, db: sqlite3.Connection): cur = db.cursor() diff --git a/scanner.py b/scanner.py new file mode 100644 index 0000000..00fa2e5 --- /dev/null +++ b/scanner.py @@ -0,0 +1,10 @@ +import cv2 + + +def capture(output_path: str) -> bool: + cam = cv2.VideoCapture(0) + s, img = cam.read() + if s: + cv2.imwrite(output_path, img) + cam.release() + return s diff --git a/templates/calibrate.html b/templates/calibrate.html new file mode 100644 index 0000000..9b37ce6 --- /dev/null +++ b/templates/calibrate.html @@ -0,0 +1,49 @@ +{% extends "base.html" %} + +{% block content %} +
+
+

Étalonnage

+

Placez la mire devant le scanner puis appuyez sur le bouton pour prévisualiser ou étalonner le scanner.

+
+
+ +
+
+ +
+
+ +
+ +
+
+
+{% endblock content %} + +{% block extrajs %} + +{% endblock extrajs %} diff --git a/templates/object.html b/templates/object.html index 8216640..d2cb98f 100644 --- a/templates/object.html +++ b/templates/object.html @@ -24,6 +24,9 @@ {% endif %} + {% if object.calibrated == 0 %} + Étalonner le scanner + {% endif %} {% endblock content %}