diff --git a/camera.py b/camera.py index 4e703e6..7b95389 100644 --- a/camera.py +++ b/camera.py @@ -4,8 +4,8 @@ from . import leds, config class Camera: - def capture(self, output_path: str) -> bool: - return False + def capture(self): + return None class RealCamera(Camera): @@ -19,19 +19,18 @@ class RealCamera(Camera): def __exit__(self, *args): self.inner.exit() - def capture(self, output_path: str) -> bool: + def capture(self): try: - 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) - raw = self.inner.file_get(file_path.folder, file_path.name, gp.GP_FILE_TYPE_RAW) - - preview.save(output_path + '.jpg') - raw.save(output_path + '.cr2') - return True - + return self.inner.capture(gp.GP_CAPTURE_IMAGE) except Exception as e: print('An error occured when capturing photo', e) - return False + return None + + def save(self, capture, output_file): + preview = self.inner.file_get(capture.folder, capture.name[:-3] + 'JPG', gp.GP_FILE_TYPE_NORMAL) + raw = self.inner.file_get(capture.folder, capture.name, gp.GP_FILE_TYPE_RAW) + preview.save(output_file + '.jpg') + raw.save(output_file + '.cr2') class DummyCamera(Camera): @@ -44,7 +43,7 @@ class DummyCamera(Camera): def __exit__(self, *args): pass - def capture(self, output_path: str) -> bool: + def capture(self): # Find which leds are turned on found = None all_on = False @@ -56,12 +55,14 @@ class DummyCamera(Camera): all_on = True if all_on: - shutil.copyfile('data-keep/small/all_on.jpg', output_path + '.jpg') + return 'data-keep/small/all_on.jpg' elif found is not None: - shutil.copyfile('data-keep/small/' + str(found) + '.jpg', output_path + '.jpg') + return 'data-keep/small/' + str(found) + '.jpg' else: - print('ALL_OFF') - shutil.copyfile('data-keep/small/all_off.jpg', output_path + '.jpg') + return 'data-keep/small/all_off.jpg' + + def save(self, capture, output_file): + shutil.copyfile(capture, output_file + '.jpg') camera = DummyCamera(leds.get()) if config.CAMERA == "dummy" else RealCamera() diff --git a/config.darkroom.py b/config.darkroom.py index dae0118..7e77861 100644 --- a/config.darkroom.py +++ b/config.darkroom.py @@ -7,6 +7,7 @@ OBJECT_DIR = join(DATA_DIR, 'objects') DATABASE_PATH = join(DATA_DIR, 'db.sqlite') AUTO_USE_LAST_CALIBRATION = True +DELAY = 0.0 GPIO_CHIP = 'gpiochip0' LEDS_UUIDS = [17, 18, 22, 23, 24, 27] CAMERA = 'real' diff --git a/config.local.py b/config.local.py index 5442bf4..f233900 100644 --- a/config.local.py +++ b/config.local.py @@ -7,6 +7,7 @@ OBJECT_DIR = join(DATA_DIR, 'objects') DATABASE_PATH = join(DATA_DIR, 'db.sqlite') AUTO_USE_LAST_CALIBRATION = False +DELAY = 0.5 GPIO_CHIP = None LEDS_UUIDS = [17, 18, 22, 23, 24, 27] CAMERA = 'dummy' diff --git a/scanner.py b/scanner.py index f70bc32..422efe8 100644 --- a/scanner.py +++ b/scanner.py @@ -1,21 +1,18 @@ import os from os.path import join import time -from . import leds, camera - -# Delay between to captures -DELAY = 0.5 +from . import leds, camera, config -def delay_capture(cam, output_path): +def delay_capture(cam): # Measure the time it takes to capture start = time.time() - output = cam.capture(output_path) + output = cam.capture() delta = time.time() - start # Wait for at least one second between each capture - if delta < DELAY: - time.sleep(DELAY - delta) + if delta < config.DELAY: + time.sleep(config.DELAY - delta) return output @@ -23,29 +20,30 @@ def delay_capture(cam, output_path): def scan(output_dir: str, on_and_off: bool = True): os.makedirs(output_dir, exist_ok=True) - with leds.get() as gpio_leds: - for count, led in enumerate(gpio_leds.leds): - with camera.get() as cam: + file_paths = [] + with camera.get() as cam: + with leds.get() as gpio_leds: + for count, led in enumerate(gpio_leds.leds): print(f'Turn on {led}') img = join(output_dir, f'{led}') led.on() - delay_capture(cam, img) + file_paths.append((str(led), delay_capture(cam))) led.off() print(f'Turn off {led}') - yield str(led) - # capture with all leds ON OFF - if on_and_off: - with camera.get() as cam: + # capture with all leds ON OFF + if on_and_off: gpio_leds.on() img = join(output_dir, 'all_on') - delay_capture(cam, img) - yield 'all_on' + file_paths.append(('all_on', delay_capture(cam))) - with camera.get() as cam: gpio_leds.off() img = join(output_dir, 'all_off') - delay_capture(cam, img) - yield 'all_off' + file_paths.append(('all_off', delay_capture(cam))) + + with camera.get() as cam: + for target, source in file_paths: + cam.save(source, join(output_dir, target)) + yield target