Snapshot lag in loop

Hi!

I have an H7 attached to a Pan/Tilt module and am trying to take a series of pictures as the camera incrementally pans. As the servo increments every x degrees, it seems that the image being displayed in the IDE is from 2 positions back. I tried to add a while loop to wait for sensor.snapshot to return something, thinking maybe that wasn’t it, but the syntax is wrong. I’m not sure that’s what’s going on, so I figured I’d ask. I posted the code below. Thanks!

import sensor, image,time
from pyb import Servo

s1 = Servo(1) # P7
s2 = Servo(2) # P8
s1.pulse_width(1300)
s2.pulse_width(700)
sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.

 while(True):
 
 	for i in range(5):
        	time.sleep(2000)
        	clock.tick()
        	s2.pulse_width(700 + 425*i)
        	while(sensor.snapshot()=False):
            		pass
        	img = sensor.snapshot()

Yeah, it’s the time.sleep() call. The snapshot method transfers the last frame from the camera to the PC when it’s called and then takes a new pic.

Please use the sensor.flush() method to get the frame transfer started immediately after sensor.snapshot().

Thanks! That helped. I changed the code to

for i in range(5):
        clock.tick()
        s2.pulse_width(700 + 425*i)   
        time.sleep(500)
        img = sensor.snapshot()   
        sensor.flush()
        #time.sleep(1500)

It still seems to get “behind” in that the the servo has occasionally moved onto the next position before the IDE displays the image, regardless of how much time it sleeps. Is there a way to block the code while the image is processed, so that the servo doesn’t advance until the image is available. Ultimately, I want to scan an area continuously looking for a circle, so I need to know at which servo position the circle is. If they are randomly out of sync, that will be hard to accommodate. I know this may behave better when it’s running on it’s own and the IDE won’t be involved, but it will be very hard to debug at that point.

Yeah, do print(img.compressed_for_ide(), end=’’)

This forces the image out of the camera. However, it will affect your FPS heavily. The IDE is typically polling the camera so it’s a very light touch to grab the frame buffer. When you do the above the camera sends the frame buffer EVERY frame and will block waiting for the IDE to read data. That said, it will sync things.

Replace flush with the above.