realsense使用ubuntu python读取深度图、彩色图和IMU数据

realsense使用ubuntu python读取深度图、彩色图和IMU数据

import pyrealsense2 as rs
import numpy as np
import cv2
import glob
import math
from tqdm import tqdm
import time
from collections import  namedtuple
import os

GyroBias = namedtuple('GyroBias', ['x', 'y', 'z'])

bias = GyroBias(0,0,0)

pipline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.gyro, rs.format.motion_xyz32f)
config.enable_stream(rs.stream.accel, rs.format.motion_xyz32f)

pipline.start(config)
align = rs.align(rs.stream.color)

save_dir = '/home/xmj/'+str(time.time())
os.mkdir(save_dir)
last_ts, dt = {}, {}
roll, pitch, yaw = 0, 0, 0
first_accel = True
for i in tqdm(range(300)):
    # real time read
    frames = pipline.wait_for_frames()
    for f in frames:
        p = f.get_profile()
        fnum = f.get_frame_number()
        ts = f.get_timestamp()
        t = str(p.stream_type())
        if t in last_ts:
            dt[t] = (ts - last_ts[t])/1000.
        else:
            dt[t] = 1/30
        last_ts[t] = ts

    gyro = frames.first(rs.stream.gyro).as_motion_frame().get_motion_data()
    gyro_arr = [gyro.x, gyro.y, gyro.z]
    accel = frames.first(rs.stream.accel).as_motion_frame().get_motion_data()
    accel_arr = [accel.x, accel.y, accel.z]
    # align
    aligned_frames = align.process(frames)

    # get split_frame
    aligned_depth_frame = aligned_frames.get_depth_frame()
    color_frame = aligned_frames.get_color_frame()
    # realtime save use rs to save ply file
    pc = rs.pointcloud()
    pc.map_to(color_frame)
    points = pc.calculate(aligned_depth_frame)
    parray = np.asanyarray(points.get_vertices())
    print('points', points, parray.shape)
    points.export_to_ply(f'{save_dir}/{i}_rs.ply', color_frame)
		# save IMU data
    open(f'{save_dir}/{i}_rs.txt', 'w').write(f"{gyro_arr[0]},{gyro_arr[1]},{gyro_arr[2]},{accel_arr[0]},{accel_arr[1]},{accel_arr[2]},{dt['stream.gyro']}")
    # Convert images to numpy arrays, use open3d to save ply file
    depth_image = np.asanyarray(aligned_depth_frame.get_data())
    # 480*640
    color_image = np.asanyarray(color_frame.get_data())
    np.savez(f'{save_dir}/{i}_rs.npz', data=depth_image)
    cv2.imwrite(f'{save_dir}/{i}_rs.png', color_image)



你可能感兴趣的:(pcl,深度学习,Python,python,ubuntu,realsense)