71 lines
1.8 KiB
Python
71 lines
1.8 KiB
Python
import cv2
|
|
import os
|
|
from os.path import join
|
|
import subprocess
|
|
import shutil
|
|
import time
|
|
import gphoto2 as gp
|
|
import gpiod
|
|
from . import config, leds
|
|
|
|
# Delay between to captures
|
|
DELAY = 0.5
|
|
|
|
def capture(camera, output_path: str) -> bool:
|
|
try:
|
|
print('Capturing image')
|
|
file_path = camera.capture(gp.GP_CAPTURE_IMAGE)
|
|
print('Camera file path: {0}/{1}'.format(file_path.folder, file_path.name))
|
|
target = output_path + '.cr2'
|
|
print('Copying image to', target)
|
|
|
|
camera_file = camera.file_get(file_path.folder, file_path.name, gp.GP_FILE_TYPE_RAW)
|
|
camera_file.save(target)
|
|
|
|
camera_file = camera.file_get(file_path.folder, file_path.name, gp.GP_FILE_TYPE_PREVIEW)
|
|
camera_file.save(output_path + '.jpg')
|
|
|
|
s = True
|
|
except:
|
|
print(f'Somethings wrong on gphoto2')
|
|
s = False
|
|
|
|
return s
|
|
|
|
|
|
def scan(output_dir: str):
|
|
camera = gp.Camera()
|
|
camera.init()
|
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
gpio_leds = leds.GpioLeds(config.LEDS_UUIDS)
|
|
|
|
with gpio_leds:
|
|
for count, led in enumerate(gpio_leds.leds):
|
|
print(f'Turn on {led}')
|
|
img = join(output_dir, f'{led}')
|
|
|
|
# Measure the time it takes to capture
|
|
start = time.time()
|
|
|
|
led.on()
|
|
capture(camera, img)
|
|
led.off()
|
|
|
|
# For debug purposes
|
|
# shutil.copyfile(join('data-keep/small', led + '.jpg'), 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}')
|
|
if count == len(config.LEDS_UUIDS) -1:
|
|
gpio_leds.off()
|
|
camera.exit()
|
|
|
|
yield led
|
|
|