Recording, Streaming, and Conversion

Recording Sensor Data

It’s easy to record data to a pcap file from a sensor programmatically. Let’s try it on a configured sensor:

$ python3 -m ouster.sdk.examples.client $SENSOR_HOSTNAME record-pcap

This will capture the client.LidarPacket’s and client.ImuPacket’s data for 10 seconds and store the pcap file along with the metadata json file into the current directory.

The source code of an example below:

 1from ouster.sdk import sensor
 2import ouster.sdk.pcap as pcap
 3from datetime import datetime
 4from more_itertools import time_limited
 5# connect to sensor and record lidar/imu packets
 6with closing(sensor.SensorPacketSource(hostname, lidar_port=lidar_port, imu_port=imu_port,
 7                           buffer_time_sec=1.0)) as source:
 8
 9    # make a descriptive filename for metadata/pcap files
10    time_part = datetime.now().strftime("%Y%m%d_%H%M%S")
11    meta = source.sensor_info[0]
12    fname_base = f"{meta.prod_line}_{meta.sn}_{meta.config.lidar_mode}_{time_part}"
13
14    print(f"Saving sensor metadata to: {fname_base}.json")
15    with open(f"{fname_base}.json", "w") as f:
16        f.write(source.sensor_info[0].to_json_string())
17
18    print(f"Writing to: {fname_base}.pcap (Ctrl-C to stop early)")
19    source_it = time_limited(n_seconds, source)
20
21    def to_packet():
22        for idx, packet in source_it:
23            yield packet
24    n_packets = pcap.record(to_packet(), f"{fname_base}.pcap")
25
26    print(f"Captured {n_packets} packets")

Good! The resulting pcap and json files can be used with any examples in the examples.pcap module.

Streaming Live Data

Instead of working with a recorded dataset or a few captured frames of data, let’s see if we can get a live feed from your configured sensor:

$ python3 -m ouster.sdk.examples.client $SENSOR_HOSTNAME live-plot-reflectivity

This should give you a live feed from your sensor that looks like a black and white moving image. Try waving your hand or moving around to find yourself within the image!

So how did we do that?

 1# establish sensor connection
 2from ouster.sdk import sensor
 3with closing(sensor.SensorScanSource(hostname, lidar_port=lidar_port)) as stream:
 4    show = True
 5    while show:
 6        for scan, *_ in stream:
 7            if scan is None:
 8                continue
 9            # uncomment if you'd like to see frame id printed
10            # print("frame id: {} ".format(scan.frame_id))
11            reflectivity = core.destagger(stream.sensor_info[0],
12                                      scan.field(core.ChanField.REFLECTIVITY))
13            reflectivity = (reflectivity / np.max(reflectivity) * 255).astype(np.uint8)
14            cv2.imshow("scaled reflectivity", reflectivity)
15            key = cv2.waitKey(1) & 0xFF

To exit the visualization, you can use ESC.