Skip to content

Instantly share code, notes, and snippets.

@Owen-Liuyuxuan
Created February 6, 2023 07:05
Show Gist options
  • Save Owen-Liuyuxuan/27f12e15788acba76053df84a28f2291 to your computer and use it in GitHub Desktop.
Save Owen-Liuyuxuan/27f12e15788acba76053df84a28f2291 to your computer and use it in GitHub Desktop.
A simple tutorial-like page to produce poses from KITTI raw dataset

A naive approach to prepare poses in KITTI raw for pythonic usage

The KITTI raw contains oxts measurements of the vehicle and can be used as poses for the raw sequence. The raw dev-kit contains a matlab/octave script for transforming oxts measurements to poses. In this script we provide some modification of the code and some scripts that personally get used to make this poses available to python, and it goes with the kitti_visualize repo and other repo we are working with.

The output will be a "pose.mat" under every {seq_dir}/oxts folder. We also provide python APIs to read this matrix.

All we will do here:

  • Add a modifled octave script to produce mat output on one sequence.
  • Add a python script to loop through all sequence in KITTI raw and call the octave script.
  • A simple way to read

First add the following matlab/octave script under devkit_raw_data/devkit/matlab named as "produce_poses_mat.m" which is modified from "run_demoVehiclePath.m"

function [pose,pose_mat]=produce_poses_mat (base_dir)
% KITTI RAW DATA DEVELOPMENT KIT
% 
% Plots OXTS poses of a sequence
%
% Input arguments:
% base_dir .... absolute path to sequence base directory (ends with _sync)

% clear and close everything
%clear all; close all; clc;
disp('======= KITTI DevKit Demo =======');

% sequence base directory
if nargin<1
  base_dir = '/data/kitti_raw/2011_09_26/2011_09_26_drive_0001_sync';
end
base_dir
% load oxts data
oxts = loadOxtsliteData(base_dir);

% transform to poses
pose = convertOxtsToPose(oxts);

pose_mat = zeros(length(pose), 4, 4);
for i=1:length(pose)
  pose_mat(i, :, :) = pose{i};
end
output_path = strcat(base_dir, '/oxts/pose.mat');
save(output_path, 'pose_mat', '-v6'); % magic paramters?

Then add another python3 script under the same folder (change base_dir) called "produce_poses_mat.py". Here we assume we are using octave (free run-time of m file). We will simply loop through all sequences and launch the octave script on every single sequence folder.

import os

base_dir = "/data/kitti_raw" ### CHANGE THIS TO YOUR KITTI_RAW PATH

date_list = os.listdir(base_dir)

for date in date_list:
    date_path = os.path.join(base_dir, date)
    if os.path.isdir(date_path): # some one may have different single_files in the base_dir, filter out them
        seq_list = os.listdir(date_path)
        for seq in seq_list:
            seq_path = os.path.join(date_path, seq)
            if os.path.isdir(seq_path):
                print("Processing: ", seq_path)
                os.system(f"octave produce_poses_mat.m {seq_path}")

Finally, we run

python3 produce_poses_mat.py

and it will produce pose.mat to every sequence of KITTI raw dataset.

Then in python, we can read the pose like

import scipy.io as sio

pose_dict = sio.loadmat(odometry_mat)
odom_array = pose_dict[[key for key in list(pose_dict.keys()) if not key.startswith('__')][0]]
# the only non "__" key, should be a N, 4, 4 numpy array where N equals to the number of images of the sequence.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment