From c97d06f7d8f7587c333d689107e90297e2b802dc Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 9 Jul 2024 09:59:46 +0200 Subject: [PATCH] Fix bugs, better progress, allow custom device id --- .gitignore | 1 + scanner.py | 21 ++++++++++++++++++++- templates/calibrate.html | 6 ++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 0cdc4cb..2ea9cc8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ db.sqlite +.device data node_modules static/calibration-visualiser.* diff --git a/scanner.py b/scanner.py index d02d867..46bdaea 100644 --- a/scanner.py +++ b/scanner.py @@ -1,11 +1,21 @@ import cv2 import os from os.path import join +import time from . import config +# Delay between to captures +DELAY = 0.5 + def capture(output_path: str) -> bool: - cam = cv2.VideoCapture(0) + try: + with open('.device', 'r') as f: + device_id = int(f.read().rstrip()) + except: + device_id = 0 + + cam = cv2.VideoCapture(device_id) s, img = cam.read() if s: cv2.imwrite(output_path, img) @@ -18,6 +28,15 @@ def scan(output_dir: str): for led in config.LEDS_UUIDS: print(f'Turn on {led}') img = join(output_dir, led + '.jpg') + + # Measure the time it takes to capture + start = time.time() capture(img) + delta = time.time() - start + + # Wait for at least one second between each capture + if delta < DELAY: + time.sleep(DELAY - delta) + print(f'Turn off {led}') yield led diff --git a/templates/calibrate.html b/templates/calibrate.html index 325f412..8145423 100644 --- a/templates/calibrate.html +++ b/templates/calibrate.html @@ -65,12 +65,14 @@ }); calibrateButton.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'); progressBar.style.display = "block"; + grid.innerHTML = ''; let response = await fetch('/api/scan-for-calibration/{{ object.id }}'); let reader = response.body.pipeThrough(new TextDecoderStream()).getReader(); @@ -89,6 +91,7 @@ let cell = document.createElement('div'); cell.classList.add('cell'); let img = document.createElement('img'); + img.classList.add('is-loading'); img.src = '/data/{{ object.id }}/calibration/' + uuid + '.jpg?v=' + scanIndex; cell.appendChild(img); grid.appendChild(cell); @@ -97,8 +100,7 @@ function refreshProgressBar() { if (progress !== progressBar.value) { - let diff = progress - progressBar.value; - progressBar.value += Math.max(diff / 20, 1); + progressBar.value = Math.min(progressBar.value + 5, progress); } requestAnimationFrame(refreshProgressBar); }