Take picture for test
This commit is contained in:
parent
8db80625be
commit
39885cd5b3
26
app.py
26
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/<id>")
|
||||
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/<id>")
|
||||
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/<id>")
|
||||
def calibration(id: int):
|
||||
conn = db.get()
|
||||
|
|
|
|||
2
db.py
2
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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<section class="section">
|
||||
<div class="container">
|
||||
<h1 class="title">Étalonnage</h1>
|
||||
<p>Placez la mire devant le scanner puis appuyez sur le bouton pour prévisualiser ou étalonner le scanner.</p>
|
||||
<div class="field is-grouped is-grouped-multiline">
|
||||
<div class="control">
|
||||
<button id="preview-button" class="button is-link">Prévisualiser</button>
|
||||
</div>
|
||||
<div class="control">
|
||||
<button class="button is-link">Étalonner</button>
|
||||
</div>
|
||||
</div>
|
||||
<article id="error-container" class="message is-danger" style="display: none;">
|
||||
<div id="error-content" class="message-body">
|
||||
</div>
|
||||
</article>
|
||||
<div>
|
||||
<img id="preview-image" style="display: none;">
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
{% endblock content %}
|
||||
|
||||
{% block extrajs %}
|
||||
<script>
|
||||
let previewButton = document.getElementById('preview-button');
|
||||
let errorContainer = document.getElementById('error-container');
|
||||
let errorContent = document.getElementById('error-content');
|
||||
previewButton.addEventListener('click', async () => {
|
||||
previewButton.classList.add('is-loading');
|
||||
let response = await fetch('/api/preview/{{ object.id }}');
|
||||
let uuid = await response.text();
|
||||
|
||||
if (response.status >= 200 && response.status < 400) {
|
||||
let img = document.getElementById('preview-image');
|
||||
img.src = "/data/{{ object.id }}/previews/" + uuid + ".jpg";
|
||||
img.style.display = "block";
|
||||
errorContainer.style.display = "none";
|
||||
} else {
|
||||
errorContainer.style.display = "block";
|
||||
errorContent.innerHTML = uuid;
|
||||
}
|
||||
previewButton.classList.remove('is-loading');
|
||||
});
|
||||
</script>
|
||||
{% endblock extrajs %}
|
||||
|
|
@ -24,6 +24,9 @@
|
|||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% if object.calibrated == 0 %}
|
||||
<a href="/calibrate/{{ object.id }}" class="button is-link">Étalonner le scanner</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</section>
|
||||
{% endblock content %}
|
||||
|
|
|
|||
Loading…
Reference in New Issue