So right now we can’t yield from snapshot so coros are useless. Anyway, until double buffering is implemented you can either:
- use timers like the following example ( I get 20FPS for VGA and 10FPS for HD)
import sensor, image, time, ustruct
from pyb import USB_VCP, Timer
usb = USB_VCP()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.HD)
img = None
ready = False
size = 0
def timer_callback(timer):
global img
global ready
global size
# DO NOT allocate memory in this handler.
if (ready == True):
usb.send(size)
usb.send(img)
ready = False
tim = Timer(2, freq=100)
tim.callback(timer_callback)
while(True):
fb = sensor.snapshot()
if (usb.recv(4, timeout=10) == b'snap'):
while (ready):
time.sleep_ms(1)
img = fb.compressed(quality=50)
size = ustruct.pack("<L", img.size())
ready = True
- take a look at the pyopenmv.py module and pyopenmv_fb.py in the tools dir, these pretty much implement the same protocol used by the IDE: