Fix bugs, better progress, allow custom device id

This commit is contained in:
Thomas Forgione 2024-07-09 09:59:46 +02:00
parent 1643eb636e
commit c97d06f7d8
3 changed files with 25 additions and 3 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
db.sqlite db.sqlite
.device
data data
node_modules node_modules
static/calibration-visualiser.* static/calibration-visualiser.*

View File

@ -1,11 +1,21 @@
import cv2 import cv2
import os import os
from os.path import join from os.path import join
import time
from . import config from . import config
# Delay between to captures
DELAY = 0.5
def capture(output_path: str) -> bool: 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() s, img = cam.read()
if s: if s:
cv2.imwrite(output_path, img) cv2.imwrite(output_path, img)
@ -18,6 +28,15 @@ def scan(output_dir: str):
for led in config.LEDS_UUIDS: for led in config.LEDS_UUIDS:
print(f'Turn on {led}') print(f'Turn on {led}')
img = join(output_dir, led + '.jpg') img = join(output_dir, led + '.jpg')
# Measure the time it takes to capture
start = time.time()
capture(img) 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}') print(f'Turn off {led}')
yield led yield led

View File

@ -65,12 +65,14 @@
}); });
calibrateButton.addEventListener('click', async () => { calibrateButton.addEventListener('click', async () => {
scanIndex++;
progress = 0; progress = 0;
progressBar.value = 0; progressBar.value = 0;
previewImage.style.display = "none"; previewImage.style.display = "none";
buttons.forEach(x => x.setAttribute('disabled', 'disabled')); buttons.forEach(x => x.setAttribute('disabled', 'disabled'));
calibrateButton.classList.add('is-loading'); calibrateButton.classList.add('is-loading');
progressBar.style.display = "block"; progressBar.style.display = "block";
grid.innerHTML = '';
let response = await fetch('/api/scan-for-calibration/{{ object.id }}'); let response = await fetch('/api/scan-for-calibration/{{ object.id }}');
let reader = response.body.pipeThrough(new TextDecoderStream()).getReader(); let reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
@ -89,6 +91,7 @@
let cell = document.createElement('div'); let cell = document.createElement('div');
cell.classList.add('cell'); cell.classList.add('cell');
let img = document.createElement('img'); let img = document.createElement('img');
img.classList.add('is-loading');
img.src = '/data/{{ object.id }}/calibration/' + uuid + '.jpg?v=' + scanIndex; img.src = '/data/{{ object.id }}/calibration/' + uuid + '.jpg?v=' + scanIndex;
cell.appendChild(img); cell.appendChild(img);
grid.appendChild(cell); grid.appendChild(cell);
@ -97,8 +100,7 @@
function refreshProgressBar() { function refreshProgressBar() {
if (progress !== progressBar.value) { if (progress !== progressBar.value) {
let diff = progress - progressBar.value; progressBar.value = Math.min(progressBar.value + 5, progress);
progressBar.value += Math.max(diff / 20, 1);
} }
requestAnimationFrame(refreshProgressBar); requestAnimationFrame(refreshProgressBar);
} }