9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

IntelのRealsense D435で録画と出力

Last updated at Posted at 2019-05-21

やりたいこと

3チャンネル(IR/Depth/RGB)エクスポートして再生する

やったこと

最初はopencvのframeをいちいちwriteしてaviとply形式で書き出ししていたが
エンコードに時間がかかるし処理が遅くてfpsがかなり下がってしまっていた。(このコードは問題のコードとして最後に上げている)
解決策としてマルチスレッド化を図ったがあまり早くならなかった。(コードの書き方が悪かったのかもしれない)
下記の参考URLの書き方ではbagファイルとして出力し、圧縮が少ない(ない?あまりbagファイルを知らない…)ため処理が早くこの問題を解決してくれた。(ただファイルサイズはかなり大きいのでこれをどうするかは後々考えていこうと思う)

環境

ホストOS:macOSserria(10.12.16)
virtualBox
ゲストOS:ubuntu16.04
python3.5.2
opencv-python==3.4.2.16

環境構築は以下の記事で書いているので置いておきます。
https://qiita.com/nataly510/items/034f946362e7a8ecb6a8

参考URL

http://mirai-tec.hatenablog.com/entry/2018/07/29/150902
大変助かりました。ありがとうございます。

出力コード

import pyrealsense2 as rs
import numpy as np
import cv2
import time
# Configure depth and color streams
config = rs.config()
config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, 30)
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_record_to_file('d435data.bag')
# Start streaming
pipeline = rs.pipeline()
pipeline.start(config)
start = time.time()
frame_no = 1
try:
    while True:
        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()
        color_frame = frames.get_color_frame()
        ir_frame = frames.get_infrared_frame()
        fps  = frame_no / (time.time() - start)  
        print(fps)
        frame_no = frame_no+1  
        if not ir_frame or not color_frame  :   
    	    ir_image = np.asanyarray(ir_frame .get_data())    
    	    color_image = np.asanyarray(color_frame.get_data()) 

finally:    
    pipeline.stop()     

## 再生コード

import pyrealsense2 as rs
import numpy as np
import cv2
# ストリーム(Color/Depth/Infrared)の設定
config = rs.config()
# ↓ ここでファイル名設定
config.enable_device_from_file('d435data.bag')
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, 30)

# ストリーミング開始
pipeline = rs.pipeline()
profile = pipeline.start(config)

try:
    while True:
        # フレーム待ち(Color & Depth)
        frames = pipeline.wait_for_frames()
        ir_frame = frames.get_infrared_frame()
        depth_frame = frames.get_depth_frame()
        color_frame = frames.get_color_frame()
        if not depth_frame or not color_frame or not ir_frame  :
            continue

        # Convert images to numpy arrays
        ir_image = np.asanyarray(ir_frame .get_data())
        depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())       
        # Show images
        cv2.namedWindow('ir_image', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('ir_image', ir_image2)
        cv2.namedWindow('color_image', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('color_image', color_image)
        cv2.namedWindow('depth_image', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('depth_image', depth_image)
        cv2.waitKey(1)
finally:

    # Stop streaming
    pipeline.stop()

## 問題があったコード

#License: Apache 2.0. See LICENSE file in root directory.
## Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.

###############################################
##      Open CV and Numpy integration        ##
###############################################

import pyrealsense2 as rs
import numpy as np
import cv2
import time

# Declare pointcloud object, for calculating pointclouds and texture mappings
pc = rs.pointcloud()
# We want the points object to be persistent so we can display the last cloud when a frame drops
points = rs.points()

# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# Start streaming
pipeline.start(config)
start = time.time()


# VideoWriter を作成する。
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
writer = cv2.VideoWriter('output_ir.avi', fourcc, 30, (640, 480))
writer2 = cv2.VideoWriter('output_color.avi', fourcc, 30, (640, 480))
frame_no = 1
try:
    while True:

        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()
        ir_frame = frames.get_infrared_frame()
        depth_frame = frames.get_depth_frame()
        color_frame = frames.get_color_frame()
        if not depth_frame or not color_frame or not ir_frame  :
            continue

        # Convert images to numpy arrays
        ir_image = np.asanyarray(ir_frame .get_data())
        depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())       
        # Tell pointcloud object to map to this color frame
        pc.map_to(color_frame)

        # Generate the pointcloud and texture mappings
        points = pc.calculate(depth_frame)
        fps  = frame_no / (time.time() - start)  
        print(fps)      
        frame_no = frame_no+1
        points.export_to_ply('frame_{:04d}.ply'.format(frame_no), color_frame )
        
       


        #export
        writer.write(ir_image) 
        writer2.write(color_image) 


        # Show images
        cv2.namedWindow('ir_image', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('ir_image', ir_image)
        cv2.namedWindow('color_image', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('color_image', color_image)
        cv2.namedWindow('depth_image', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('depth_image', depth_image)
        cv2.waitKey(1)

finally:

    # Stop streaming
    writer.release()
    pipeline.stop()
9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?