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 more_itertools import time_limited
 2# connect to sensor and record lidar/imu packets
 3config = client.SensorConfig()
 4config.udp_port_lidar = lidar_port
 5config.udp_port_imu = imu_port
 6with closing(client.SensorPacketSource([(hostname, config)],
 7                           buf_size=640).single_source(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.metadata
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.metadata.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    n_packets = pcap.record(source_it, f"{fname_base}.pcap")
21
22    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
 2with closing(sensor.SensorScanSource(hostname, lidar_port=lidar_port,
 3                                     complete=False)) as stream:
 4    show = True
 5    while show:
 6        for scan, *_ in stream:
 7            # uncomment if you'd like to see frame id printed
 8            # print("frame id: {} ".format(scan.frame_id))
 9            reflectivity = client.destagger(stream.metadata[0],
10                                      scan.field(client.ChanField.REFLECTIVITY))
11            reflectivity = (reflectivity / np.max(reflectivity) * 255).astype(np.uint8)
12            cv2.imshow("scaled reflectivity", reflectivity)
13            key = cv2.waitKey(1) & 0xFF

Notice that instead of taking a sample, we used Scans.stream(), which allows for a continuous live data stream. We close the stream when we are finished, hence the use of closing() in the highlighted line.

To exit the visualization, you can use ESC.