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)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值