lifecam/main.py

92 lines
2.0 KiB
Python

# Hello World Example
#
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
import sensor
import image
import pyb
import time
import uos
blue = pyb.LED(3)
green = pyb.LED(2)
sleep_cycle = 30
def reset():
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.VGA) # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000) # Wait for settings take effect.
# if the fs is <= 1M in size, it's probably flash and we don't want to
# write images.
def sd_is_mounted():
st = uos.statvfs('.')
sz = st[1] * st[2]
return sz > 1048576
def image_path(sess, imno):
return 'images/image-%02d-%04d.jpg' % (sess, imno)
def check_dir():
try:
uos.stat('images')
except OSError:
uos.mkdir('images')
def find_session():
sess = -1
for dir in uos.listdir('images'):
try:
_, imsess, _ = dir.split('-')
except ValueError:
continue
imsess = int(imsess)
if imsess > sess:
sess = imsess
return sess + 1
def process_image(img):
# nothing yet
return img
def snapshot(sess, imno):
path = image_path(sess, imno)
green.on()
img = sensor.snapshot()
green.off()
img.save(path)
print('wrote path', path)
return imno + 1
def cycle(cycle, sess, imno):
while cycle > 0:
if cycle % 5 == 0:
print('blue on')
blue.on()
if cycle % 5 == 4:
print('blue off')
blue.off()
time.sleep(1)
cycle -= 1
return snapshot(sess, imno)
def main():
reset()
if not sd_is_mounted():
return
check_dir()
sess = find_session()
imno = 0
print('session', sess)
while(True):
print('start cycle', imno)
imno = cycle(sleep_cycle, sess, imno)
if __name__ == '__main__':
main()