all_on and all_off
This commit is contained in:
parent
62b6bb2417
commit
727ab37b0b
|
|
@ -1,6 +1,6 @@
|
||||||
from flask import Flask, send_from_directory, session
|
from flask import Flask, send_from_directory, session
|
||||||
import os
|
import os
|
||||||
from . import db, config, routes, utils
|
from . import db, config, routes, utils, leds
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
@ -25,7 +25,7 @@ def inject():
|
||||||
conn = db.get()
|
conn = db.get()
|
||||||
return {
|
return {
|
||||||
'calibration': utils.get_calibration(conn),
|
'calibration': utils.get_calibration(conn),
|
||||||
'leds': config.GPIO_LEDS.leds,
|
'leds': leds.get().leds,
|
||||||
'CalibrationState': db.CalibrationState,
|
'CalibrationState': db.CalibrationState,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
45
camera.py
45
camera.py
|
|
@ -1,42 +1,47 @@
|
||||||
import gphoto2 as gp
|
import gphoto2 as gp
|
||||||
import shutil
|
import shutil
|
||||||
from . import leds
|
from . import leds, config
|
||||||
|
|
||||||
|
|
||||||
class Camera:
|
class Camera:
|
||||||
|
def capture(self, output_path: str) -> bool:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class RealCamera(Camera):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.inner = gp.Camera()
|
self.inner = gp.Camera()
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.inner.init()
|
self.inner.init()
|
||||||
|
return self
|
||||||
|
|
||||||
def __exit__(self):
|
def __exit__(self, *args):
|
||||||
self.inner.exit()
|
self.inner.exit()
|
||||||
|
|
||||||
def capture(self, output_path: str) -> bool:
|
def capture(self, output_path: str) -> bool:
|
||||||
try:
|
try:
|
||||||
with self:
|
file_path = self.inner.capture(gp.GP_CAPTURE_IMAGE)
|
||||||
file_path = self.inner.capture(gp.GP_CAPTURE_IMAGE)
|
preview = self.inner.file_get(file_path.folder, file_path.name[:-3] + '.JPG', gp.GP_FILE_TYPE_NORMAL)
|
||||||
preview = self.inner.file_get(file_path.folder, file_path.name[:-3] + '.JPG', gp.GP_FILE_TYPE_NORMAL)
|
raw = self.inner.file_get(file_path.folder, file_path.name, gp.GP_FILE_TYPE_RAW)
|
||||||
raw = self.inner.file_get(file_path.folder, file_path.name, gp.GP_FILE_TYPE_RAW)
|
|
||||||
|
|
||||||
preview.sve(output_path + '.jpg')
|
preview.sve(output_path + '.jpg')
|
||||||
raw.save(output_path + '.cr2')
|
raw.save(output_path + '.cr2')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('An error occured when capturing photo', e)
|
print('An error occured when capturing photo', e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
class DummyCamera:
|
class DummyCamera(Camera):
|
||||||
def __init__(self, leds: leds.DummyLeds):
|
def __init__(self, leds: leds.DummyLeds):
|
||||||
self.leds = leds
|
self.leds = leds
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
pass
|
return self
|
||||||
|
|
||||||
def __exit__(self):
|
def __exit__(self, *args):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def capture(self, output_path: str) -> bool:
|
def capture(self, output_path: str) -> bool:
|
||||||
|
|
@ -50,9 +55,17 @@ class DummyCamera:
|
||||||
else:
|
else:
|
||||||
all_on = True
|
all_on = True
|
||||||
|
|
||||||
if found:
|
if all_on:
|
||||||
|
shutil.copyfile('data-keep/small/all_on.jpg', output_path + '.jpg')
|
||||||
|
elif found is not None:
|
||||||
shutil.copyfile('data-keep/small/' + str(found) + '.jpg', output_path + '.jpg')
|
shutil.copyfile('data-keep/small/' + str(found) + '.jpg', output_path + '.jpg')
|
||||||
elif all_on:
|
|
||||||
shutil.copyfile('data-keep/small/LED17.jpg', output_path + '.jpg')
|
|
||||||
else:
|
else:
|
||||||
shutil.copyfile('data-keep/small/LED17.jpg', output_path + '.jpg')
|
print('ALL_OFF')
|
||||||
|
shutil.copyfile('data-keep/small/all_off.jpg', output_path + '.jpg')
|
||||||
|
|
||||||
|
|
||||||
|
camera = DummyCamera(leds.get()) if config.CAMERA == "dummy" else RealCamera()
|
||||||
|
|
||||||
|
|
||||||
|
def get():
|
||||||
|
return camera
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
from os.path import join
|
from os.path import join
|
||||||
from . import camera, leds
|
|
||||||
|
|
||||||
DATA_DIR = 'data'
|
DATA_DIR = 'data'
|
||||||
BACKUPS_DIR = 'data-backups'
|
BACKUPS_DIR = 'data-backups'
|
||||||
|
|
@ -10,5 +9,4 @@ DATABASE_PATH = join(DATA_DIR, 'db.sqlite')
|
||||||
AUTO_USE_LAST_CALIBRATION = True
|
AUTO_USE_LAST_CALIBRATION = True
|
||||||
GPIO_CHIP = 'gpiochip0'
|
GPIO_CHIP = 'gpiochip0'
|
||||||
LEDS_UUIDS = [17, 18, 22, 23, 24, 27]
|
LEDS_UUIDS = [17, 18, 22, 23, 24, 27]
|
||||||
GPIO_LEDS = leds.GpioLeds(LEDS_UUIDS)
|
CAMERA = 'real'
|
||||||
CAMERA = camera.Camera()
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
from os.path import join
|
from os.path import join
|
||||||
from . import camera, leds
|
|
||||||
|
|
||||||
DATA_DIR = 'data'
|
DATA_DIR = 'data'
|
||||||
BACKUPS_DIR = 'data-backups'
|
BACKUPS_DIR = 'data-backups'
|
||||||
|
|
@ -10,5 +9,4 @@ DATABASE_PATH = join(DATA_DIR, 'db.sqlite')
|
||||||
AUTO_USE_LAST_CALIBRATION = False
|
AUTO_USE_LAST_CALIBRATION = False
|
||||||
GPIO_CHIP = None
|
GPIO_CHIP = None
|
||||||
LEDS_UUIDS = [17, 18, 22, 23, 24, 27]
|
LEDS_UUIDS = [17, 18, 22, 23, 24, 27]
|
||||||
GPIO_LEDS = leds.DummyLeds(LEDS_UUIDS)
|
CAMERA = 'dummy'
|
||||||
CAMERA = camera.DummyCamera(GPIO_LEDS)
|
|
||||||
|
|
|
||||||
29
leds.py
29
leds.py
|
|
@ -1,5 +1,21 @@
|
||||||
import gpiod
|
import gpiod
|
||||||
|
|
||||||
|
from . import config
|
||||||
|
|
||||||
|
|
||||||
|
class Leds:
|
||||||
|
def __enter__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def off(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class GpioLed:
|
class GpioLed:
|
||||||
def __init__(self, gpio_pin: int):
|
def __init__(self, gpio_pin: int):
|
||||||
|
|
@ -26,7 +42,7 @@ class GpioLed:
|
||||||
return f'LED{self.gpio_pin:02}'
|
return f'LED{self.gpio_pin:02}'
|
||||||
|
|
||||||
|
|
||||||
class GpioLeds:
|
class GpioLeds(Leds):
|
||||||
def __init__(self, chip: str, gpio_pins: list[int]):
|
def __init__(self, chip: str, gpio_pins: list[int]):
|
||||||
self.chip = gpiod.Chip(chip)
|
self.chip = gpiod.Chip(chip)
|
||||||
self.leds = []
|
self.leds = []
|
||||||
|
|
@ -36,6 +52,7 @@ class GpioLeds:
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
for led in self.leds:
|
for led in self.leds:
|
||||||
led.enter(self.chip)
|
led.enter(self.chip)
|
||||||
|
return self
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
for led in self.leds:
|
for led in self.leds:
|
||||||
|
|
@ -71,7 +88,7 @@ class DummyLed:
|
||||||
return f'LED{self.gpio_pin:02}'
|
return f'LED{self.gpio_pin:02}'
|
||||||
|
|
||||||
|
|
||||||
class DummyLeds:
|
class DummyLeds(Leds):
|
||||||
def __init__(self, gpio_pins: list[int]):
|
def __init__(self, gpio_pins: list[int]):
|
||||||
self.leds = []
|
self.leds = []
|
||||||
for pin in gpio_pins:
|
for pin in gpio_pins:
|
||||||
|
|
@ -80,6 +97,7 @@ class DummyLeds:
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
for led in self.leds:
|
for led in self.leds:
|
||||||
led.enter()
|
led.enter()
|
||||||
|
return self
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
for led in self.leds:
|
for led in self.leds:
|
||||||
|
|
@ -92,3 +110,10 @@ class DummyLeds:
|
||||||
def on(self):
|
def on(self):
|
||||||
for led in self.leds:
|
for led in self.leds:
|
||||||
led.on()
|
led.on()
|
||||||
|
|
||||||
|
|
||||||
|
_leds = GpioLeds(config.GPIO_CHIP, config.LEDS_UUIDS) if config.GPIO_CHIP is not None else DummyLeds(config.LEDS_UUIDS)
|
||||||
|
|
||||||
|
|
||||||
|
def get() -> Leds:
|
||||||
|
return _leds
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ def scan_existing(id: int):
|
||||||
conn = db.get()
|
conn = db.get()
|
||||||
calibrated = session.get('calibration_id', None) is not None
|
calibrated = session.get('calibration_id', None) is not None
|
||||||
acquisition = db.Acquisition.get_from_id(id, conn)
|
acquisition = db.Acquisition.get_from_id(id, conn)
|
||||||
object = acquisition.object(conn)
|
object = acquisition.object(conn) if acquisition is not None else None
|
||||||
return render_template('scan.html', object=object, acquisition=acquisition, calibrated=calibrated)
|
return render_template('scan.html', object=object, acquisition=acquisition, calibrated=calibrated)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -48,7 +48,7 @@ def run(object_id: int):
|
||||||
|
|
||||||
def generate():
|
def generate():
|
||||||
yield str(acquisition.id)
|
yield str(acquisition.id)
|
||||||
length = len(config.LEDS_UUIDS)
|
length = len(config.LEDS_UUIDS) + 2 # with all_on and all_off
|
||||||
for index, led_uuid in enumerate(scanner.scan(join(config.OBJECT_DIR, str(object.id), str(acquisition.id)))):
|
for index, led_uuid in enumerate(scanner.scan(join(config.OBJECT_DIR, str(object.id), str(acquisition.id)))):
|
||||||
yield f"{led_uuid},{(index+1)/length}\n"
|
yield f"{led_uuid},{(index+1)/length}\n"
|
||||||
|
|
||||||
|
|
@ -74,7 +74,7 @@ def rescan(acquisition_id: int):
|
||||||
object = acquisition.object(conn)
|
object = acquisition.object(conn)
|
||||||
|
|
||||||
def generate():
|
def generate():
|
||||||
length = len(config.LEDS_UUIDS)
|
length = len(config.LEDS_UUIDS) + 2 # with all_on and all_off
|
||||||
for index, led_uuid in enumerate(scanner.scan(join(config.OBJECT_DIR, str(object.id), str(acquisition.id)))):
|
for index, led_uuid in enumerate(scanner.scan(join(config.OBJECT_DIR, str(object.id), str(acquisition.id)))):
|
||||||
yield f"{led_uuid},{(index+1)/length}\n"
|
yield f"{led_uuid},{(index+1)/length}\n"
|
||||||
|
|
||||||
|
|
@ -87,7 +87,7 @@ def validate(acquisition_id: int):
|
||||||
acquisition = db.Acquisition.get_from_id(acquisition_id, conn)
|
acquisition = db.Acquisition.get_from_id(acquisition_id, conn)
|
||||||
|
|
||||||
if acquisition is None:
|
if acquisition is None:
|
||||||
raise f"Aucune acquisition d'id {acquisition_id}"
|
raise Exception(f"Aucune acquisition d'id {acquisition_id}")
|
||||||
|
|
||||||
object = acquisition.object(conn)
|
object = acquisition.object(conn)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ def scan():
|
||||||
|
|
||||||
def generate():
|
def generate():
|
||||||
length = len(config.LEDS_UUIDS)
|
length = len(config.LEDS_UUIDS)
|
||||||
for index, led_uuid in enumerate(scanner.scan(join(config.CALIBRATION_DIR, calibration_id))):
|
for index, led_uuid in enumerate(scanner.scan(join(config.CALIBRATION_DIR, calibration_id), False)):
|
||||||
yield f"{led_uuid},{(index+1)/length}\n"
|
yield f"{led_uuid},{(index+1)/length}\n"
|
||||||
|
|
||||||
with conn:
|
with conn:
|
||||||
|
|
|
||||||
61
scanner.py
61
scanner.py
|
|
@ -1,42 +1,51 @@
|
||||||
import os
|
import os
|
||||||
from os.path import join
|
from os.path import join
|
||||||
import time
|
import time
|
||||||
from . import config
|
from . import leds, camera
|
||||||
|
|
||||||
# Delay between to captures
|
# Delay between to captures
|
||||||
DELAY = 0.5
|
DELAY = 0.5
|
||||||
|
|
||||||
|
|
||||||
def scan(output_dir: str):
|
def delay_capture(cam, output_path):
|
||||||
|
# Measure the time it takes to capture
|
||||||
|
start = time.time()
|
||||||
|
output = cam.capture(output_path)
|
||||||
|
delta = time.time() - start
|
||||||
|
|
||||||
|
# Wait for at least one second between each capture
|
||||||
|
if delta < DELAY:
|
||||||
|
time.sleep(DELAY - delta)
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
def scan(output_dir: str, on_and_off: bool = True):
|
||||||
os.makedirs(output_dir, exist_ok=True)
|
os.makedirs(output_dir, exist_ok=True)
|
||||||
|
|
||||||
with config.GPIO_LEDS:
|
with leds.get() as gpio_leds:
|
||||||
for count, led in enumerate(config.GPIO_LEDS.leds):
|
for count, led in enumerate(gpio_leds.leds):
|
||||||
print(f'Turn on {led}')
|
with camera.get() as cam:
|
||||||
img = join(output_dir, f'{led}')
|
print(f'Turn on {led}')
|
||||||
|
img = join(output_dir, f'{led}')
|
||||||
|
|
||||||
# Measure the time it takes to capture
|
led.on()
|
||||||
start = time.time()
|
delay_capture(cam, img)
|
||||||
|
led.off()
|
||||||
|
|
||||||
led.on()
|
print(f'Turn off {led}')
|
||||||
config.CAMERA.capture(img)
|
yield str(led)
|
||||||
led.off()
|
|
||||||
|
|
||||||
delta = time.time() - start
|
# capture with all leds ON OFF
|
||||||
|
if on_and_off:
|
||||||
# Wait for at least one second between each capture
|
with camera.get() as cam:
|
||||||
if delta < DELAY:
|
gpio_leds.on()
|
||||||
time.sleep(DELAY - delta)
|
|
||||||
|
|
||||||
print(f'Turn off {led}')
|
|
||||||
if count == len(config.LEDS_UUIDS) - 1:
|
|
||||||
# capture with all leds ON OFF
|
|
||||||
config.GPIO_LEDS.on()
|
|
||||||
img = join(output_dir, 'all_on')
|
img = join(output_dir, 'all_on')
|
||||||
config.CAMERA.capture(img)
|
delay_capture(cam, img)
|
||||||
|
yield 'all_on'
|
||||||
|
|
||||||
config.GPIO_LEDS.off()
|
with camera.get() as cam:
|
||||||
|
gpio_leds.off()
|
||||||
img = join(output_dir, 'all_off')
|
img = join(output_dir, 'all_off')
|
||||||
config.CAMERA.capture(img)
|
delay_capture(cam, img)
|
||||||
|
yield 'all_off'
|
||||||
yield led
|
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,22 @@
|
||||||
cell.appendChild(img);
|
cell.appendChild(img);
|
||||||
grid.appendChild(cell);
|
grid.appendChild(cell);
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
cell = document.createElement('div');
|
||||||
|
cell.classList.add('cell');
|
||||||
|
img = document.createElement('img');
|
||||||
|
img.classList.add('is-loading');
|
||||||
|
img.src = '/data/objects/{{ object.id }}/' + acquisitionId + '/all_on.jpg?v=0';
|
||||||
|
cell.appendChild(img);
|
||||||
|
grid.appendChild(cell);
|
||||||
|
|
||||||
|
cell = document.createElement('div');
|
||||||
|
cell.classList.add('cell');
|
||||||
|
img = document.createElement('img');
|
||||||
|
img.classList.add('is-loading');
|
||||||
|
img.src = '/data/objects/{{ object.id }}/' + acquisitionId + '/all_off.jpg?v=0';
|
||||||
|
cell.appendChild(img);
|
||||||
|
grid.appendChild(cell);
|
||||||
}
|
}
|
||||||
|
|
||||||
scanButton.addEventListener('click', async () => {
|
scanButton.addEventListener('click', async () => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue