From 4d15de613f7787da8b7e27fccf4e48f05e9a2c2c Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 1 Oct 2024 17:44:16 +0200 Subject: [PATCH] scripts/examples: Update ToF examples. --- .../01-Camera/06-Time-of-Flight/tof_camera.py | 20 ++++++------ .../06-Time-of-Flight/tof_overlay.py | 32 ++++++++----------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/scripts/examples/01-Camera/06-Time-of-Flight/tof_camera.py b/scripts/examples/01-Camera/06-Time-of-Flight/tof_camera.py index efd71e109..f51091916 100644 --- a/scripts/examples/01-Camera/06-Time-of-Flight/tof_camera.py +++ b/scripts/examples/01-Camera/06-Time-of-Flight/tof_camera.py @@ -2,17 +2,13 @@ # Copyright (c) 2013-2023 OpenMV LLC. All rights reserved. # https://github.com/openmv/openmv/blob/master/LICENSE # -# Time of Flight overlay Demo -# -# This example shows off how to overlay a depth map onto -# OpenMV Cam's live video output from the main camera. +# Time of flight camera Demo. import image import time import tof IMAGE_SCALE = 10 # Scale image to 10x. -drawing_hint = image.BILINEAR # or image.BILINEAR or 0 (nearest neighbor) # Initialize the ToF sensor tof.init() # Auto-detects the connected sensor. @@ -25,15 +21,17 @@ while True: try: img = tof.snapshot( + vflip=True, + hmirror=True, x_scale=IMAGE_SCALE, y_scale=IMAGE_SCALE, - color_palette=tof.PALETTE_IRONBOW, - hint=drawing_hint, - copy_to_fb=True, + hint=image.BILINEAR, scale=(0, 4000), + copy_to_fb=True, + color_palette=tof.PALETTE_IRONBOW, ) - except OSError: - continue + img.flush() + except RuntimeError as e: + tof.reset() - # Print FPS. print(clock.fps()) diff --git a/scripts/examples/01-Camera/06-Time-of-Flight/tof_overlay.py b/scripts/examples/01-Camera/06-Time-of-Flight/tof_overlay.py index 9ec12993d..dd641a23c 100644 --- a/scripts/examples/01-Camera/06-Time-of-Flight/tof_overlay.py +++ b/scripts/examples/01-Camera/06-Time-of-Flight/tof_overlay.py @@ -2,10 +2,8 @@ # Copyright (c) 2013-2023 OpenMV LLC. All rights reserved. # https://github.com/openmv/openmv/blob/master/LICENSE # -# Time of Flight overlay Demo -# -# This example shows off how to overlay a depth map onto -# OpenMV Cam's live video output from the main camera. +# This example shows how to overlay a depth map onto frames +# captured from the main camera. import sensor import image import time @@ -13,8 +11,9 @@ import tof sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) -sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) -sensor.set_windowing((0, 0, 240, 240)) # Set window size to 240x240 +sensor.set_framesize(sensor.VGA) # Set frame size to QVGA (320x240) +sensor.set_framerate(30) +sensor.set_windowing((400, 400)) # Set window size to 240x240 # Initialize the ToF sensor tof.init() @@ -24,36 +23,31 @@ clock = time.clock() while True: clock.tick() - # Capture an image img = sensor.snapshot() - # Capture TOF data [depth map, min distance, max distance] try: - depth, dmin, dmax = tof.read_depth() - except OSError: + depth, dmin, dmax = tof.read_depth(vflip=True, hmirror=True) + except RuntimeError: + tof.reset() continue # Scale the image and belnd it with the framebuffer tof.draw_depth( img, depth, + x_scale=img.width() / 8, + y_scale=img.height() / 8, hint=image.BILINEAR, - alpha=200, + alpha=100, scale=(0, 4000), - color_palette=tof.PALETTE_IRONBOW, + color_palette=tof.PALETTE_RAINBOW, ) # Draw min and max distance. img.draw_string( - 8, 0, "Min distance: %d mm" % dmin, color=(255, 0, 0), mono_space=False + 0, 0, f"Distance min: {int(dmin):4d}mm max: {int(dmax):4d}mm", color=(255, 0, 0), mono_space=False ) - img.draw_string( - 8, 8, "Max distance: %d mm" % dmax, color=(255, 0, 0), mono_space=False - ) - - # Force high quality streaming - img.to_jpeg(quality=90) # Print FPS. print(clock.fps())