diff --git a/usr/examples/20-Frame-Differencing/in_memory_advanced_frame_differencing.py b/usr/examples/20-Frame-Differencing/in_memory_advanced_frame_differencing.py new file mode 100644 index 000000000..3b336fc81 --- /dev/null +++ b/usr/examples/20-Frame-Differencing/in_memory_advanced_frame_differencing.py @@ -0,0 +1,65 @@ +# Advanced Frame Differencing Example +# +# This example demonstrates using frame differencing with your OpenMV Cam. This +# example is advanced because it preforms a background update to deal with the +# backgound image changing overtime. + +import sensor, image, pyb, os, time + +TRIGGER_THRESHOLD = 5 + +BG_UPDATE_FRAMES = 50 # How many frames before blending. +BG_UPDATE_BLEND = 128 # How much to blend by... ([0-256]==[0.0-1.0]). + +sensor.reset() # Initialize the camera sensor. +sensor.set_pixformat(sensor.RGB565) # or sensor.RGB565 +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. + +# Take from the main frame buffer's RAM to allocate a second frame buffer. +# There's a lot more RAM in the frame buffer than in the MicroPython heap. +# However, after doing this you have a lot less RAM for some algorithms... +# So, be aware that it's a lot easier to get out of RAM issues now. However, +# frame differencing doesn't use a lot of the extra space in the frame buffer. +# But, things like AprilTags do and won't work if you do this... +extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.RGB565) + +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!") + +triggered = False + +frame_count = 0 +while(True): + clock.tick() # Track elapsed milliseconds between snapshots(). + img = sensor.snapshot() # Take a picture and return the image. + + frame_count += 1 + if (frame_count > BG_UPDATE_FRAMES) and not triggered: + frame_count = 0 + # Blend in new frame. We're doing 256-alpha here because we want to + # blend the new frame into the backgound. Not the background into the + # new frame which would be just alpha. Blend replaces each pixel by + # ((NEW*(alpha))+(OLD*(256-alpha)))/256. So, a low alpha results in + # low blending of the new image while a high alpha results in high + # blending of the new image. We need to reverse that for this update. + img.blend(extra_fb, alpha=(256-BG_UPDATE_BLEND)) + extra_fb.replace(img) + + # 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 + + print(clock.fps(), triggered) # Note: Your OpenMV Cam runs about half as fast while + # connected to your computer. The FPS should increase once disconnected. diff --git a/usr/examples/20-Frame-Differencing/in_memory_basic_frame_differencing.py b/usr/examples/20-Frame-Differencing/in_memory_basic_frame_differencing.py new file mode 100644 index 000000000..cbe0daed2 --- /dev/null +++ b/usr/examples/20-Frame-Differencing/in_memory_basic_frame_differencing.py @@ -0,0 +1,47 @@ +# In Memory Basic Frame Differencing Example +# +# This example demonstrates using frame differencing with your OpenMV Cam. It's +# called basic frame differencing because there's no background image update. +# So, as time passes the background image may change resulting in issues. + +import sensor, image, pyb, os, time + +TRIGGER_THRESHOLD = 5 + +sensor.reset() # Initialize the camera sensor. +sensor.set_pixformat(sensor.RGB565) # 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. + +# Take from the main frame buffer's RAM to allocate a second frame buffer. +# There's a lot more RAM in the frame buffer than in the MicroPython heap. +# However, after doing this you have a lot less RAM for some algorithms... +# So, be aware that it's a lot easier to get out of RAM issues now. However, +# frame differencing doesn't use a lot of the extra space in the frame buffer. +# But, things like AprilTags do and won't work if you do this... +extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.RGB565) + +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!") + +while(True): + 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 + + print(clock.fps(), triggered) # Note: Your OpenMV Cam runs about half as fast while + # connected to your computer. The FPS should increase once disconnected. diff --git a/usr/examples/20-Frame-Differencing/in_memory_structural_similarity.py b/usr/examples/20-Frame-Differencing/in_memory_structural_similarity.py new file mode 100644 index 000000000..18762ff81 --- /dev/null +++ b/usr/examples/20-Frame-Differencing/in_memory_structural_similarity.py @@ -0,0 +1,39 @@ +# Structural Similarity (SSIM) Example +# +# This example shows off how to use the SSIM algorithm on your OpenMV Cam +# to detect differences between two images. The SSIM algorithm compares +# 8x8 blocks of pixels between two images to determine a similarity +# score between two images. + +import sensor, image, pyb, os, time + +# The image has likely changed if the sim.min() is lower than this. +MIN_TRIGGER_THRESHOLD = -0.4 + +sensor.reset() # Initialize the camera sensor. +sensor.set_pixformat(sensor.RGB565) # 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. + +# Take from the main frame buffer's RAM to allocate a second frame buffer. +# There's a lot more RAM in the frame buffer than in the MicroPython heap. +# However, after doing this you have a lot less RAM for some algorithms... +# So, be aware that it's a lot easier to get out of RAM issues now. However, +# frame differencing doesn't use a lot of the extra space in the frame buffer. +# But, things like AprilTags do and won't work if you do this... +extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.RGB565) + +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!") + +while(True): + clock.tick() # Track elapsed milliseconds between snapshots(). + img = sensor.snapshot() # Take a picture and return the image. + sim = img.get_similarity(extra_fb) + change = "- Change -" if sim.min() < MIN_TRIGGER_THRESHOLD else "- No Change -" + + print(clock.fps(), change, sim) diff --git a/usr/examples/04-Image-Filters/advanced_frame_differencing.py b/usr/examples/20-Frame-Differencing/on_disk_advanced_frame_differencing.py similarity index 74% rename from usr/examples/04-Image-Filters/advanced_frame_differencing.py rename to usr/examples/20-Frame-Differencing/on_disk_advanced_frame_differencing.py index 5c478b177..73d1309b3 100644 --- a/usr/examples/04-Image-Filters/advanced_frame_differencing.py +++ b/usr/examples/20-Frame-Differencing/on_disk_advanced_frame_differencing.py @@ -8,11 +8,13 @@ import sensor, image, pyb, os, time +TRIGGER_THRESHOLD = 5 + BG_UPDATE_FRAMES = 50 # How many frames before blending. BG_UPDATE_BLEND = 128 # How much to blend by... ([0-256]==[0.0-1.0]). sensor.reset() # Initialize the camera sensor. -sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565 +sensor.set_pixformat(sensor.RGB565) # or sensor.RGB565 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. @@ -25,13 +27,15 @@ sensor.skip_frames(time = 2000) # Give the user time to get ready. sensor.snapshot().save("temp/bg.bmp") print("Saved background image - Now frame differencing!") +triggered = False + frame_count = 0 while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image. frame_count += 1 - if frame_count > BG_UPDATE_FRAMES: + if (frame_count > BG_UPDATE_FRAMES) and not triggered: frame_count = 0 # Blend in new frame. We're doing 256-alpha here because we want to # blend the new frame into the backgound. Not the background into the @@ -45,5 +49,13 @@ while(True): # Replace the image with the "abs(NEW-OLD)" frame difference. img.difference("temp/bg.bmp") - print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while + 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 + + print(clock.fps(), triggered) # Note: Your OpenMV Cam runs about half as fast while # connected to your computer. The FPS should increase once disconnected. diff --git a/usr/examples/04-Image-Filters/basic_frame_differencing.py b/usr/examples/20-Frame-Differencing/on_disk_basic_frame_differencing.py similarity index 70% rename from usr/examples/04-Image-Filters/basic_frame_differencing.py rename to usr/examples/20-Frame-Differencing/on_disk_basic_frame_differencing.py index 2cefc8aaf..2b0b775e9 100644 --- a/usr/examples/04-Image-Filters/basic_frame_differencing.py +++ b/usr/examples/20-Frame-Differencing/on_disk_basic_frame_differencing.py @@ -8,6 +8,8 @@ import sensor, image, pyb, os, time +TRIGGER_THRESHOLD = 5 + sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE sensor.set_framesize(sensor.QVGA) # or sensor.QQVGA (or others) @@ -29,5 +31,13 @@ while(True): # Replace the image with the "abs(NEW-OLD)" frame difference. img.difference("temp/bg.bmp") - print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while + 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 + + print(clock.fps(), triggered) # Note: Your OpenMV Cam runs about half as fast while # connected to your computer. The FPS should increase once disconnected. diff --git a/usr/examples/20-Frame-Differencing/structural-similarity.py b/usr/examples/20-Frame-Differencing/on_disk_structural_similarity.py similarity index 100% rename from usr/examples/20-Frame-Differencing/structural-similarity.py rename to usr/examples/20-Frame-Differencing/on_disk_structural_similarity.py