This example demonstrate how to get the distance (depth) value from camera for a specific point

Expect Output

Distance is      492 mm
Distance is      495 mm
Distance is      497 mm
Distance is      499 mm
Distance is      503 mm
Distance is      505 mm
Distance is      511 mm


  • Import openni package

from openni import openni2
  • Initialize SDK

  • Connect to camera

dev = openni2.Device.open_any()
  • Create depth stream and start it

depth_stream = dev.create_depth_stream()
  • Depth data is store in one dimension array in ordered, from top left to bottom right. We calculate the index of the center point of the frame according to the width and height first.

video_mode = depth_stream.get_video_mode()
middleIndex = int((video_mode.resolutionY + 1) * video_mode.resolutionX / 2)
  • Read frame from camera and retrieve the depth data of middleIndex

frame = depth_stream.read_frame()
pDepth = frame.get_buffer_as_uint16()
print(f'''Distance is {pDepth[middleIndex]} mm''')

Full code

Last updated