59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
import cv2
|
|
import os
|
|
from os.path import join
|
|
import shutil
|
|
import time
|
|
import gphoto2 as gp
|
|
from gpiozero import PWMLED
|
|
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)
|
|
gpio= PWMLED(17)
|
|
gpio.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()
|
|
gpio.value = count/len(config.LEDS_UUIDS)
|
|
capture(img)
|
|
# For debug purposes
|
|
#shutil.copyfile(join('data-keep/small', led + '.jpg'), img)
|
|
gpio.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}')
|
|
yield led
|