Python での Azure Kinect DK の使用はまだ非常に空白です。冒頭のブログでは、Open3D を使用して、Azure Kinect DK カメラによって読み取られた情報のリアルタイム点群視覚化と、データの対話型ストレージの実行を含む、Open3D を使用して Kinect DK を開発したメモが記録されています。画像点群ボタン。
公式コードを解釈してみましょう。
視覚化: azure_kinect_viewer.py
メイン機能
import argparse
import open3d as o3d
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Azure kinect mkv recorder.')
parser.add_argument('--config', type=str, help='input json kinect config')
parser.add_argument('--list',
action='store_true',
help='list available azure kinect sensors')
parser.add_argument('--device',
type=int,
default=0,
help='input kinect device id')
parser.add_argument('-a',
'--align_depth_to_color',
action='store_true',
help='enable align depth image to color')
args = parser.parse_args()
if args.list:
o3d.io.AzureKinectSensor.list_devices()
exit()
if args.config is not None:
config = o3d.io.read_azure_kinect_sensor_config(args.config)
else:
config = o3d.io.AzureKinectSensorConfig()
device = args.device
if device < 0 or device > 255:
print('Unsupported device id, fall back to 0')
device = 0
v = ViewerWithCallback(config, device, args.align_depth_to_color)
v.run()
最初のコードはargparseモジュールの設定ですが、argparseモジュールについてはこちらの記事「argparseモジュールの詳細説明」を参照してください。
主なパラメータは次の 4 つです。
- config: 設定ファイル。自分で設定を構成するには、このリンクを参照してパラメータを調整できます:列挙
- list: デバイス接続があるかどうかを確認します。
- device: デバイス番号、デフォルトは 0 です。
- align_ Depth_to_color: 深度を色に合わせます (デフォルトでは位置合わせされていません。コマンド ラインに -a を追加して開きます)。一般に、深度画像の範囲は比較的広く、深度を色に合わせた後、RGB 画像領域に対応する深度画像が表示されます。切り取って整列させたもの。
ビデオ読み込み表示クラス ViewerWithCallback (RGB画像と深度画像を格納するためのコールバック関数を追加)
class ViewerWithCallback:
def __init__(self, config, device, align_depth_to_color):
self.flag_exit = False
self.align_depth_to_color = align_depth_to_color
self.sensor = o3d.io.AzureKinectSensor(config) # 配置相机参数
if not self.sensor.connect(device):
raise RuntimeError('Failed to connect to sensor')
def escape_callback(self, vis):
self.flag_exit = True
return False
def save_callback(self, vis): # 提取RGB图像和深度图像并保存,按's'键保存
num = np.random.randint(100)
o3d.io.write_image('color'+str(num)+'.png', self.rgbd.color, quality=- 1)
o3d.io.write_image('depth'+str(num)+'.png', self.rgbd.depth, quality=- 1)
return False
def run(self):
glfw_key_escape = 256
glfw_key_save = 83
vis = o3d.visualization.VisualizerWithKeyCallback() # 定义可视化类
vis.register_key_callback(glfw_key_escape, self.escape_callback) # 注册退出回调函数
vis.register_key_callback(glfw_key_save, self.save_callback) # 注册保存回调函数
vis.create_window('viewer', 1920, 540) # 创建窗口
print("Sensor initialized. Press [ESC] to exit.")
vis_geometry_added = False
while not self.flag_exit:
self.rgbd = self.sensor.capture_frame(self.align_depth_to_color) # 采集图像
print(self.align_depth_to_color)
if self.rgbd is None:
continue
if not vis_geometry_added:
vis.add_geometry(self.rgbd)
vis_geometry_added = True
# vis.capture_screen_image('b.png', do_render=False)
vis.update_geometry(self.rgbd)
vis.poll_events()
vis.update_renderer()
ビデオを録画する: azure_kinect_recorder.py
import argparse
import datetime
import open3d as o3d
class RecorderWithCallback:
def __init__(self, config, device, filename, align_depth_to_color):
# Global flags
self.flag_exit = False
self.flag_record = False
self.filename = filename
self.align_depth_to_color = align_depth_to_color
self.recorder = o3d.io.AzureKinectRecorder(config, device)
if not self.recorder.init_sensor():
raise RuntimeError('Failed to connect to sensor')
def escape_callback(self, vis):
self.flag_exit = True
if self.recorder.is_record_created():
print('Recording finished.')
else:
print('Nothing has been recorded.')
return False
def space_callback(self, vis): # 空格键用于录制交互
if self.flag_record: # 停止
print('Recording paused. '
'Press [Space] to continue. '
'Press [ESC] to save and exit.')
self.flag_record = False
elif not self.recorder.is_record_created(): # 开始
if self.recorder.open_record(self.filename):
print('Recording started. '
'Press [SPACE] to pause. '
'Press [ESC] to save and exit.')
self.flag_record = True
else: #继续
print('Recording resumed, video may be discontinuous. '
'Press [SPACE] to pause. '
'Press [ESC] to save and exit.')
self.flag_record = True
return False
def run(self):
glfw_key_escape = 256
glfw_key_space = 32
vis = o3d.visualization.VisualizerWithKeyCallback()
vis.register_key_callback(glfw_key_escape, self.escape_callback)
vis.register_key_callback(glfw_key_space, self.space_callback)
vis.create_window('recorder', 1920, 540)
print("Recorder initialized. Press [SPACE] to start. "
"Press [ESC] to save and exit.")
vis_geometry_added = False
while not self.flag_exit:
rgbd = self.recorder.record_frame(self.flag_record,
self.align_depth_to_color) # 录制函数,flag_record通过空格键控制
if rgbd is None:
continue
if not vis_geometry_added:
vis.add_geometry(rgbd)
vis_geometry_added = True
vis.update_geometry(rgbd)
vis.poll_events()
vis.update_renderer()
self.recorder.close_record() # 停止录制,flag_exit通过Esc键控制
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Azure kinect mkv recorder.')
parser.add_argument('--config', type=str, help='input json kinect config')
parser.add_argument('--output', type=str, help='output mkv filename')
parser.add_argument('--list',
action='store_true',
help='list available azure kinect sensors')
parser.add_argument('--device',
type=int,
default=0,
help='input kinect device id')
parser.add_argument('-a',
'--align_depth_to_color',
action='store_true',
help='enable align depth image to color')
# '-a'的'-'代表可选,'store_true'默认False,命令行开启后为True
args = parser.parse_args()
if args.list:
o3d.io.AzureKinectSensor.list_devices()
exit()
if args.config is not None:
config = o3d.io.read_azure_kinect_sensor_config(args.config)
else:
config = o3d.io.AzureKinectSensorConfig()
if args.output is not None:
filename = args.output
else:
filename = '{date:%Y-%m-%d-%H-%M-%S}.mkv'.format(
date=datetime.datetime.now())
print('Prepare writing to {}'.format(filename))
device = args.device
if device < 0 or device > 255:
print('Unsupported device id, fall back to 0')
device = 0
r = RecorderWithCallback(config, device, filename,
args.align_depth_to_color)
r.run()
点群のリアルタイム視覚化
pyKinectAzure は、Azure Kinect DK によって開発された Python 3 ライブラリであり、さまざまな Kinect 点群および画像表示ルーチンが含まれています (GitHub リンク: https://github.com/ibaiGorordo/pyKinectAzure )。
pyKinectAzure ルーチンで与えられたコードは画像が一部トリミングされているため、あまり使いにくいため、pyKinectAzure をベースにいくつかの修正を加え、点群をリアルタイムで表示し、RGB 画像を保存できるルーチンを作成しました。 、深度画像と点群、元のライブラリにいくつかの変更が加えられているため、実行するにはコードを GitHub にダウンロードする必要があります: https://github.com/struggler176393/Kinect_pointcloud。ダウンロード後、メイン ディレクトリは Kinect_pointcloud の下にあり、カメラを接続し、VSCode で example/save_pointcloud.py ファイルを実行して点群をリアルタイムで表示します。再構築された点群は、カメラのメイン ポイントを原点とします。ビジュアルインターフェースの原点に追加します 座標系を変更し、RGBをXYZ軸に対応させ、図中のz軸矢印の方向を反転し、実際のデータのz方向の値を変更しますは 0 より大きく、デフォルトの単位はメートルです。
効果は次の図に示すとおりです。
save_pointcloud.py:
import sys
sys.path.insert(1, './')
import argparse
import open3d as o3d
import numpy as np
import pykinect_azure as pykinect
from pykinect_azure.utils import Open3dVisualizer
import cv2
from datetime import datetime
class SaverWithCallback:
def __init__(self, config, device, align_depth_to_color):
self.flag_exit = False
self.align_depth_to_color = align_depth_to_color
self.open3dVisualizer = Open3dVisualizer()
self.sensor = o3d.io.AzureKinectSensor(config)
if not self.sensor.connect(device):
raise RuntimeError('Failed to connect to sensor')
def escape_callback(self, vis):
self.flag_exit = True
return False
def save_callback(self, vis):
timeStr = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
o3d.io.write_image('color_'+timeStr+'.png', self.rgbd.color, quality=- 1)
o3d.io.write_image('depth_'+timeStr+'.png', self.rgbd.depth, quality=- 1)
o3d.io.write_point_cloud('point_cloud_'+timeStr+'.ply', self.point_cloud)
return False
def run(self):
while not self.flag_exit:
glfw_key_escape = 256
glfw_key_save = 83
self.open3dVisualizer.vis.register_key_callback(glfw_key_escape, self.escape_callback)
self.open3dVisualizer.vis.register_key_callback(glfw_key_save, self.save_callback)
self.rgbd = self.sensor.capture_frame(self.align_depth_to_color)
if self.rgbd is None:
continue
color_image = self.rgbd.color
depth_image = self.rgbd.depth
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
color_image, depth_image,
convert_rgb_to_intensity=False)
intrinsic = o3d.camera.PinholeCameraIntrinsic(
o3d.camera.PinholeCameraIntrinsicParameters.PrimeSenseDefault )
intrinsic.set_intrinsics(
width=512, height=512, fx=251.730835, fy=251.734344, cx=259.099640, cy=262.528076)
self.point_cloud = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, intrinsic)
self.open3dVisualizer(self.point_cloud.points,self.point_cloud.colors)
cv2.imshow('Transformed color', cv2.cvtColor(np.asarray(color_image), cv2.COLOR_BGRA2RGB))
# Press q key to stop
if cv2.waitKey(1) == ord('q'):
break
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Azure kinect mkv recorder.')
parser.add_argument('--config', type=str, default='./myconfig.json', help='input json kinect config')
parser.add_argument('--list',
action='store_true',
help='list available azure kinect sensors')
parser.add_argument('--device',
type=int,
default=0,
help='input kinect device id')
parser.add_argument('-a',
'--align_depth_to_color',
action='store_false',
help='enable align depth image to color')
args = parser.parse_args()
if args.list:
o3d.io.AzureKinectSensor.list_devices()
exit()
if args.config is not None:
config = o3d.io.read_azure_kinect_sensor_config(args.config)
else:
config = o3d.io.AzureKinectSensorConfig()
device = args.device
if device < 0 or device > 255:
print('Unsupported device id, fall back to 0')
device = 0
v = SaverWithCallback(config, device, args.align_depth_to_color)
v.run()
ここで、myconfig.json ファイルは Kinect_pointcloud ディレクトリにあり、リアルタイム パフォーマンスの観点から 1280 * 720 のフレーム レートが選択されていることに注意してください。カメラの内部構成は、カメラの内部構成と密接に関連しています。 myconfig.json ファイル。json ファイルの構成が完了したら、この記事を参照して取得できます。公式キャリブレーション データ: 「Open3D は、RGB イメージと深度イメージ (ピット レコード) から RGB-D 点群を生成します」、このチュートリアルは Windows の Visual Studio でコンパイルされていますが、私は Ubuntu でコンパイルしました。小さなバグですが、k4a::k4a ライブラリがないことが示されています。CmakeList.txt ファイルに移動して変更してtarget_link_libraries(calibration_info PRIVATE k4a::k4a)
くださいtarget_link_libraries(calibration_info PRIVATE k4a)
。取得した深度画像はカラー画像と位置合わせされているため、点群再構成にはカラーカメラの内部参照が選択され、main.cpp の 76 行目が に変更され、変更されたパラメータがコンパイルされて内部座標が取得さauto calib = calibration.depth_camera_calibration;
れauto calib = calibration.color_camera_calibration;
ますdeviceConfig
。参照:
mkdir build
cd build
cmake ..
make
./calibration_info
OK、目的の効果がおおよそ得られたので、マルチフレーム再構成ルーチンを実行したいと思いますが、結局のところ、単一フレーム点群再構成の精度は少し低いです。