nenuscanner/scanner.py

56 lines
1.3 KiB
Python

import cv2
import os
from os.path import join
import shutil
import time
import gphoto2 as gp
from . import config
# Delay between to captures
DELAY = 0.5
def capture(output_path: str) -> bool:
try:
camera = gp.Camera()
camera.init()
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
print('Copying image to', target)
camera_file = camera.file_get(
file_path.folder, file_path.name, gp.GP_FILE_TYPE_NORMAL)
camera_file.save(target)
s = True
except:
print(f'Somethings wrong on gphoto2')
s = False
finally:
camera.exit()
return s
def scan(output_dir: str):
os.makedirs(output_dir, exist_ok=True)
for led in config.LEDS_UUIDS:
print(f'Turn on {led}')
img = join(output_dir, led + '.jpg')
# Measure the time it takes to capture
start = time.time()
capture(img)
# 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}')
yield led