OpenMv function repeate every 5 minutes

Hi!
I plan to use the frame differencing to detect bird motion. So far, I follow the OpenMv example and the frame differencing is working. I want to update the background image every 5 minutes to decrease the effect of light/shadow change. Some time functions I found are pyb.millis(), timer(), and time.time(). They look like the delay function. Is there any suggestion or examples I can use as a reference?
Thank you

Um, you just use the pyb.ellapsed_millis() method to check if 5 seconds pass. Then update the FB in that case.

Hi! kwagyeman,
I follow your advice and build the test for pyb.ellapsed_millis(). It works if I use it directly. When I added it to the example of frame differencing, it does not refresh the image at every one second. Can you see what the problem is? I am not good at using the timer interrupt. Do you know how to handle the time overflow (after it passed 12.4 days)?

The code to test pyb.ellapsed_millis() individually:

import sensor, image, time, pyb

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

start = pyb.millis()
while (True):

if (pyb.elapsed_millis(start) == 1000):
    print('The current time is ', start)
    start = pyb.millis()

Add the pyb.elapsed_millis() to frame differencing example:
import sensor, image, pyb, os, time

TRIGGER_THRESHOLD = 5
grey_threshold = (0, 40)

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.QVGA) # or sensor.QQVGA (or others)
sensor.skip_frames(time = 2000) # Let new settings take affect.
#sensor.set_auto_whitebal(False) # Turn off white balance.
clock = time.clock() # Tracks FPS.

extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.GRAYSCALE)

print(“About to save background image…”)
sensor.skip_frames(time = 2000) # Give the user time to get ready.
extra_fb.replace(sensor.snapshot())
print(“Saved background image - Now frame differencing!”)

start = pyb.millis()

while(True):

if (pyb.elapsed_millis(start) == 1000):
    print("*************************************")
    print("Time to refresh the background image")
    extra_fb.replace(sensor.snapshot())
    start = pyb.millis()

clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.

# Replace the image with the "abs(NEW-OLD)" frame difference.
img.difference(extra_fb)

hist = img.get_histogram()
# This code below works by comparing the 99th percentile value (e.g. the
# non-outlier max value against the 90th percentile value (e.g. a non-max
# value. The difference between the two values will grow as the difference
# image seems more pixels change.
diff = hist.get_percentile(0.99).l_value() - hist.get_percentile(0.90).l_value()
triggered = diff > TRIGGER_THRESHOLD

blobs = img.find_blobs([grey_threshold], invert = True, merge=True, margin = 50)
if blobs:
    for b in blobs:
        img.draw_rectangle(b[0:4])
        img.draw_cross(b[5], b[6])

print(clock.fps(), triggered) # Note: Your OpenMV Cam runs about half as fast while
# connected to your computer. The FPS should increase once disconnected.

Thank you

Hi, please don’t post all your code, then use the code snippet butting to make your code readable.

You should not do an if comparison. Do a greater than or equal. When you reset start after the comparison passes this will prevent overflow.

1 Like