Skip to content

ail-and-colleagues/simple_realsense

Repository files navigation

simple_realsense

preparement

pip install -r requirements.txt

capture_and_record.py

usage: capture_and_record.py [-h] -s SAVE_PER_SEC

capture and record image and depth_map

optional arguments:
  -h, --help            show this help message and exit
                        saving rate specified like frame/save_per_sec

realsenseから得たデータをキャプチャしつつ、指定したインターバルで記録する。例えば、python capture_and_record.py -s 5、あるいはpython capture_and_record.py --save_per_sec 5のように指定すると、1秒間に5回記録する。

記録はlogフォルダの中に作成された西暦-月-日_時-分フォルダに西暦-月-日_時-分-ミリ秒-{img / depth}.pngとして記録される。次図はその一例。

2022-10-04_17-12-12-287246-img.png 2022-10-04_17-12-12-287246-depth.png

depthを可視化する際にはJETなどの疑似カラー(カラーマップ)が用いられるが、128階調だと分解能が足りない。ここでは次のようにmatplotlibに用意されているカラーマップ"gist_rainbow"を10000階調で作成し、uint8化して得た重複のない1377階調を得ることにした。カラーマップの階調を増やす方法(あるいは階調の大きいカラーマップ)について引き続き調査中。

simple_realsense/utils.py

Lines 88 to 100 in d66de80

resol = 10000
rainbow = mpl.colormaps["gist_rainbow"].resampled(resol)
rainbow = [rainbow(i / resol) for i in range(resol)]
rainbow = (255.0 * np.array(rainbow)).astype(np.uint8)
_, idx = np.unique(rainbow, axis=0, return_index=True)
rainbow = rainbow[np.sort(idx)][:, :3]
color_num = rainbow.shape[0]
self.colormap = np.zeros([color_num + 1, 3], dtype=np.uint8)
# assign brack(0, 0, 0) to colormap[0].
# this indicates the depth is smaller than the min, larger than the max, or unmeasured.
self.colormap[0] = [0, 0, 0]
self.colormap[1:] = rainbow

画像が保存されるフォルダには併せて実行時の設定やrealsenseの内部パラメータ(intrinsics)を記録したconf.yamlも保存される。

rgbd_to_ply.py

usage: rgbd_to_ply.py [-h] -i IMAGE

convert image and depth_map to ply

optional arguments:
  -h, --help            show this help message and exit
  -i IMAGE, --image IMAGE
                        a color image path

capture_and_record.pyにて作成した画像と深度(のカラーマップ)から三次元点群を生成する。python rgbd_to_ply.py -i 西暦-月-日_時-分-ミリ秒-img.pngないしpython rgbd_to_ply.py --image 西暦-月-日_時-分-ミリ秒-img.pngのように画像データを指定すると、それに対応する~~-depth.pngconf.yamlから次図のような三次元点群を作成し、同フォルダに西暦-月-日_時-分-ミリ秒-{img / depth}.plyとして保存する。

2022-10-04 181939.png

bag_to_png_ply.py

usage: bag_to_png_ply.py [-h] -b BAG_FILE -i INTERVAL -o OUT_DIR

extract png and ply image from bag

optional arguments:
  -h, --help            show this help message and exit
  -b BAG_FILE, --bag_file BAG_FILE
                        bag_file or dir
  -i INTERVAL, --interval INTERVAL
                        saving interval
  -o OUT_DIR, --out_dir OUT_DIR
                        output directory

.bagとはロボット用のアプリケーション開発ライブラリであるROS(Robot Operating System、https://www.ros.org/ )にて内部的な(コンポーネント間の)通信に用いられるメッセージを保存するためのフォーマット。RealSense向けに公式に提供しているビューワーでは .bag形式でRGBとDepthを出力する機能が用意されている。 realsense viewerで録画した.bagファイルを指定したインターバルで.pngと.plyに書き出す。ただし、pyrealsense2で.bagを読み出す場合、デバイスを接続するのと同様にwait_for_frames()からは最新のフレームが返却される(i.e. cv2のread()のように次のフレームを返すわけではない)ため、実際のインターバルはPCの処理能力に寄り、現実的には -i 20 程度になると思われる。 書き出しディレクトリは-oで指定する(-o D://tempで他のドライブの指定可能)。ファイル名は {bag_file_name}_{frame_index}.png/.plyとなっている。

outputs

Releases

No releases published

Packages

No packages published

Languages