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.
# 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())

View File

@ -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())