From 8676b2196985ade5749432889a0d19b67ecfa0c9 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 9 Jul 2024 16:48:14 +0200 Subject: [PATCH] Working --- app.py | 15 +++++++++++++-- calibration.py | 3 +-- scanner.py | 7 ++++++- templates/calibrate.html | 35 +++++++++++++++++++++++++++-------- ts/Engine.ts | 4 ++-- 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index dfbe5dc..e02e393 100755 --- a/app.py +++ b/app.py @@ -1,10 +1,11 @@ #!/usr/bin/env python from flask import Flask, redirect, request, render_template, send_from_directory +import json import os from os.path import join import uuid -from . import db, config, scanner +from . import db, config, scanner, calibration app = Flask(__name__) @@ -63,8 +64,18 @@ def scan_calibration(id: int): return app.response_class(generate(), mimetype='text/plain') +@app.route("/api/calibrate/") +def run_calibration(id: int): + conn = db.get() + db.Object.get_from_id(id, conn) + calibration_json = calibration.calibrate(join(config.DATA_DIR, str(id), 'calibration')) + with open(join(config.DATA_DIR, str(id), 'calibration.json'), 'w') as f: + json.dump(calibration_json, f, indent=4) + return 'ok' + + @app.route("/calibration/") -def calibration(id: int): +def calibration_page(id: int): conn = db.get() object = db.Object.get_from_id(id, conn) return render_template('calibration.html', object=object) diff --git a/calibration.py b/calibration.py index c1c3bb9..d5a4231 100755 --- a/calibration.py +++ b/calibration.py @@ -1,6 +1,5 @@ #!/usr/bin/env python -import io import json import functools import numpy as np @@ -8,7 +7,7 @@ import os import sys from PIL import Image -import utils +from . import utils # To extract a few images and resize them at 20% of their size: diff --git a/scanner.py b/scanner.py index 46bdaea..5cf7cc9 100644 --- a/scanner.py +++ b/scanner.py @@ -1,6 +1,7 @@ import cv2 import os from os.path import join +import shutil import time from . import config @@ -31,7 +32,11 @@ def scan(output_dir: str): # Measure the time it takes to capture start = time.time() - capture(img) + + # capture(img) + # For debug purposes + shutil.copyfile(join(config.DATA_DIR, 'small', led + '.jpg'), img) + delta = time.time() - start # Wait for at least one second between each capture diff --git a/templates/calibrate.html b/templates/calibrate.html index 8145423..2831001 100644 --- a/templates/calibrate.html +++ b/templates/calibrate.html @@ -12,15 +12,17 @@
- +
-
- +
+
+ +
@@ -28,6 +30,11 @@
+
{% endblock content %} @@ -37,13 +44,16 @@ let scanIndex = 0; let progress = 0; let previewButton = document.getElementById('preview-button'); - let calibrateButton = document.getElementById('calibrate-button'); + let scanButton = document.getElementById('scan-button'); let progressBar = document.getElementById('progress-bar'); let previewImage = document.getElementById('preview-image'); let grid = document.getElementById('grid'); - let buttons = [previewButton, calibrateButton]; + let buttons = [previewButton, scanButton]; let errorContainer = document.getElementById('error-container'); let errorContent = document.getElementById('error-content'); + let calibrateDiv = document.getElementById('calibrate'); + let calibrateButton = document.getElementById('calibrate-button'); + let calibrationInfo = document.getElementById('calibration-info'); previewButton.addEventListener('click', async () => { buttons.forEach(x => x.setAttribute('disabled', 'disabled')); @@ -64,13 +74,13 @@ buttons.forEach(x => x.removeAttribute('disabled')); }); - calibrateButton.addEventListener('click', async () => { + scanButton.addEventListener('click', async () => { scanIndex++; progress = 0; progressBar.value = 0; previewImage.style.display = "none"; buttons.forEach(x => x.setAttribute('disabled', 'disabled')); - calibrateButton.classList.add('is-loading'); + scanButton.classList.add('is-loading'); progressBar.style.display = "block"; grid.innerHTML = ''; @@ -82,7 +92,8 @@ if (done) { buttons.forEach(x => x.removeAttribute('disabled')); - calibrateButton.classList.remove('is-loading'); + scanButton.classList.remove('is-loading'); + calibrateDiv.style.display = "block"; break; } @@ -98,6 +109,14 @@ } }); + calibrateButton.addEventListener('click', async () => { + calibrateButton.classList.add('is-loading'); + + await fetch('/api/calibrate/{{ object.id }}'); + window.location.href = '/calibration/{{ object.id }}'; + + }); + function refreshProgressBar() { if (progress !== progressBar.value) { progressBar.value = Math.min(progressBar.value + 5, progress); diff --git a/ts/Engine.ts b/ts/Engine.ts index b3810f0..969aa2f 100644 --- a/ts/Engine.ts +++ b/ts/Engine.ts @@ -295,8 +295,8 @@ export class Engine { */ showImage(led: Led): void { if (led.on) { - this.selectedObject.innerText = led.name + ' (' + ( this.leds.currentLedIndex + 1) + '/' + this.leds.children.length + ')'; - this.ledView.src = '/data/' + this.objectId + '/' + led.name; + this.selectedObject.innerText = led.name.split('.')[0] + ' (' + ( this.leds.currentLedIndex + 1) + '/' + this.leds.children.length + ')'; + this.ledView.src = '/data/' + this.objectId + '/calibration/' + led.name; this.ledView.style.display = 'block'; } else { this.selectedObject.innerText = 'aucune';