scripts/examples: Update ToF examples.

This commit is contained in:
iabdalkader 2024-10-01 17:44:16 +02:00
parent ae4e3a1fb5
commit 4d15de613f
2 changed files with 22 additions and 30 deletions

View File

@ -2,17 +2,13 @@
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved. # Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE # https://github.com/openmv/openmv/blob/master/LICENSE
# #
# Time of Flight overlay Demo # Time of flight camera Demo.
#
# This example shows off how to overlay a depth map onto
# OpenMV Cam's live video output from the main camera.
import image import image
import time import time
import tof import tof
IMAGE_SCALE = 10 # Scale image to 10x. IMAGE_SCALE = 10 # Scale image to 10x.
drawing_hint = image.BILINEAR # or image.BILINEAR or 0 (nearest neighbor)
# Initialize the ToF sensor # Initialize the ToF sensor
tof.init() # Auto-detects the connected sensor. tof.init() # Auto-detects the connected sensor.
@ -25,15 +21,17 @@ while True:
try: try:
img = tof.snapshot( img = tof.snapshot(
vflip=True,
hmirror=True,
x_scale=IMAGE_SCALE, x_scale=IMAGE_SCALE,
y_scale=IMAGE_SCALE, y_scale=IMAGE_SCALE,
color_palette=tof.PALETTE_IRONBOW, hint=image.BILINEAR,
hint=drawing_hint,
copy_to_fb=True,
scale=(0, 4000), scale=(0, 4000),
copy_to_fb=True,
color_palette=tof.PALETTE_IRONBOW,
) )
except OSError: img.flush()
continue except RuntimeError as e:
tof.reset()
# Print FPS.
print(clock.fps()) print(clock.fps())

View File

@ -2,10 +2,8 @@
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved. # Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE # https://github.com/openmv/openmv/blob/master/LICENSE
# #
# Time of Flight overlay Demo # This example shows how to overlay a depth map onto frames
# # captured from the main camera.
# This example shows off how to overlay a depth map onto
# OpenMV Cam's live video output from the main camera.
import sensor import sensor
import image import image
import time import time
@ -13,8 +11,9 @@ import tof
sensor.reset() # Reset and initialize the sensor. sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) 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_framesize(sensor.VGA) # Set frame size to QVGA (320x240)
sensor.set_windowing((0, 0, 240, 240)) # Set window size to 240x240 sensor.set_framerate(30)
sensor.set_windowing((400, 400)) # Set window size to 240x240
# Initialize the ToF sensor # Initialize the ToF sensor
tof.init() tof.init()
@ -24,36 +23,31 @@ clock = time.clock()
while True: while True:
clock.tick() clock.tick()
# Capture an image # Capture an image
img = sensor.snapshot() img = sensor.snapshot()
# Capture TOF data [depth map, min distance, max distance] # Capture TOF data [depth map, min distance, max distance]
try: try:
depth, dmin, dmax = tof.read_depth() depth, dmin, dmax = tof.read_depth(vflip=True, hmirror=True)
except OSError: except RuntimeError:
tof.reset()
continue continue
# Scale the image and belnd it with the framebuffer # Scale the image and belnd it with the framebuffer
tof.draw_depth( tof.draw_depth(
img, img,
depth, depth,
x_scale=img.width() / 8,
y_scale=img.height() / 8,
hint=image.BILINEAR, hint=image.BILINEAR,
alpha=200, alpha=100,
scale=(0, 4000), scale=(0, 4000),
color_palette=tof.PALETTE_IRONBOW, color_palette=tof.PALETTE_RAINBOW,
) )
# Draw min and max distance. # Draw min and max distance.
img.draw_string( 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 FPS.
print(clock.fps()) print(clock.fps())