nenuscanner/scanner.py

82 lines
2.0 KiB
Python

import cv2
import os
from os.path import join
import shutil
import time
import gphoto2 as gp
import gpiod
from . import config
# Delay between to captures
DELAY = 0.5
LED2_PIN = 17
LED3_PIN = 18
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_RAW)
camera_file.save(target)
camera.exit()
s = True
except:
print(f'Somethings wrong on gphoto2')
s = False
return s
def scan(output_dir: str):
os.makedirs(output_dir, exist_ok=True)
chip = gpiod.Chip('gpiochip0')
led2 = chip.get_line(LED2_PIN)
led2.request(consumer="LED", type=gpiod.LINE_REQ_DIR_OUT)
led3 = chip.get_line(LED3_PIN)
led3.request(consumer="LED", type=gpiod.LINE_REQ_DIR_OUT)
led3.set_value(0)
led3.set_value(0)
for count,led in enumerate(config.LEDS_UUIDS):
print(f'Turn on {count} {led}')
img = join(output_dir, led + '.jpg')
# Measure the time it takes to capture
start = time.time()
if count % 2 == 0:
led2.set_value(1)
else :
led3.set_value(1)
capture(img)
# For debug purposes
#shutil.copyfile(join('data-keep/small', led + '.jpg'), img)
led2.set_value(0)
led3.set_value(0)
delta = time.time() - start
# Wait for at least one second between each capture
if delta < DELAY:
time.sleep(DELAY - delta)
print(f'Turn off {count} {led}')
if count == len(config.LEDS_UUIDS) -1 :
led2.set_value(0)
led3.set_value(0)
led2.release()
led3.release()
yield led