nenuscanner/camera.py

72 lines
1.9 KiB
Python

import gphoto2 as gp
import shutil
from . import leds, config
class Camera:
def capture(self, output_path: str) -> bool:
return False
class RealCamera(Camera):
def __init__(self):
self.inner = gp.Camera()
def __enter__(self):
self.inner.init()
return self
def __exit__(self, *args):
self.inner.exit()
def capture(self, output_path: str) -> bool:
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
except Exception as e:
print('An error occured when capturing photo', e)
return False
class DummyCamera(Camera):
def __init__(self, leds: leds.DummyLeds):
self.leds = leds
def __enter__(self):
return self
def __exit__(self, *args):
pass
def capture(self, output_path: str) -> bool:
# Find which leds are turned on
found = None
all_on = False
for led in self.leds.leds:
if led.is_on:
if found is None:
found = led
else:
all_on = True
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')
else:
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