BodyBalanceEvaluation/backend/tests/azure_kinect_image_example.py

273 lines
9.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Azure Kinect SDK 1.4.0 图像获取示例
这个示例展示了如何使用pyKinectAzure库来获取Azure Kinect DK传感器的各种图像数据
- 彩色图像 (Color Image)
- 深度图像 (Depth Image)
- 红外图像 (Infrared Image)
- 彩色深度图像 (Colored Depth Image)
要求:
- Azure Kinect SDK 1.4.0
- pykinect_azure库
- OpenCV (cv2)
- numpy
使用方法:
1. 确保Azure Kinect DK设备已连接
2. 运行脚本: python azure_kinect_image_example.py
3. 按 'q' 键退出程序
4. 按 '1' 键切换到彩色图像模式
5. 按 '2' 键切换到深度图像模式
6. 按 '3' 键切换到红外图像模式
7. 按 '4' 键切换到彩色深度图像模式
8. 按 's' 键保存当前图像
"""
import cv2
import numpy as np
import os
from datetime import datetime
try:
import pykinect_azure as pykinect
except ImportError:
print("错误: 无法导入pykinect_azure库")
print("请使用以下命令安装: pip install pykinect_azure")
exit(1)
class AzureKinectImageCapture:
"""Azure Kinect图像捕获类"""
def __init__(self):
self.device = None
self.current_mode = 1 # 1:彩色, 2:深度, 3:红外, 4:彩色深度
self.save_counter = 0
# 创建保存图像的目录
self.save_dir = "captured_images"
if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)
def initialize_device(self):
"""初始化Azure Kinect设备"""
try:
# 初始化库,解决"Compatible Azure Kinect SDK not found"兼容性问题
#
# 方法1: 自动检测(可能遇到版本兼容性问题)
# pykinect.initialize_libraries()
#
# 方法2: 手动指定路径(推荐,解决版本兼容性问题)
# 可选的SDK路径按优先级排序
import platform
import os
sdk_paths = []
if platform.system() == "Windows":
# Orbbec SDK K4A Wrapper推荐
sdk_paths.append(r"D:\OrbbecSDK_K4A_Wrapper_v1.10.3_windows_202408091749\bin\k4a.dll")
else:
# Linux路径
sdk_paths.extend([
"/usr/lib/x86_64-linux-gnu/libk4a.so",
"/usr/local/lib/libk4a.so"
])
# 尝试使用可用的SDK路径
sdk_initialized = False
for sdk_path in sdk_paths:
if os.path.exists(sdk_path):
try:
print(f"尝试使用SDK路径: {sdk_path}")
pykinect.initialize_libraries(module_k4a_path=sdk_path)
print(f"✓ 成功使用SDK: {sdk_path}")
sdk_initialized = True
break
except Exception as e:
print(f"✗ SDK路径失败: {sdk_path} - {e}")
continue
# 如果所有手动路径都失败,尝试自动检测
if not sdk_initialized:
try:
print("尝试自动检测SDK...")
pykinect.initialize_libraries()
print("✓ 自动检测SDK成功")
sdk_initialized = True
except Exception as e:
print(f"✗ 自动检测失败: {e}")
if not sdk_initialized:
raise Exception("无法初始化Azure Kinect SDK。请检查SDK安装或手动指定正确的路径。")
# 配置设备参数
device_config = pykinect.default_configuration
device_config.color_resolution = pykinect.K4A_COLOR_RESOLUTION_1080P # 1080p彩色分辨率
device_config.depth_mode = pykinect.K4A_DEPTH_MODE_WFOV_2X2BINNED # 宽视场深度模式
device_config.camera_fps = pykinect.K4A_FRAMES_PER_SECOND_30 # 30 FPS
device_config.synchronized_images_only = True # 同步图像
print("设备配置:")
print(device_config)
# 启动设备
self.device = pykinect.start_device(config=device_config)
print("Azure Kinect设备初始化成功!")
return True
except Exception as e:
print(f"设备初始化失败: {e}")
print("请确保:")
print("1. Azure Kinect DK设备已正确连接")
print("2. Azure Kinect SDK 1.4.0已正确安装")
print("3. 设备驱动程序已安装")
return False
def get_color_image(self):
"""获取彩色图像"""
capture = self.device.update()
ret, color_image = capture.get_color_image()
return ret, color_image
def get_depth_image(self):
"""获取原始深度图像"""
capture = self.device.update()
ret, depth_image = capture.get_depth_image()
return ret, depth_image
def get_colored_depth_image(self):
"""获取彩色深度图像"""
capture = self.device.update()
ret, colored_depth_image = capture.get_colored_depth_image()
return ret, colored_depth_image
def get_infrared_image(self):
"""获取红外图像"""
capture = self.device.update()
ret, ir_image = capture.get_ir_image()
return ret, ir_image
def save_image(self, image, mode_name):
"""保存图像到文件"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{mode_name}_{timestamp}_{self.save_counter:03d}.png"
filepath = os.path.join(self.save_dir, filename)
cv2.imwrite(filepath, image)
print(f"图像已保存: {filepath}")
self.save_counter += 1
def display_instructions(self):
"""显示操作说明"""
instructions = [
"=== Azure Kinect 图像捕获示例 ===",
"操作说明:",
"1 - 切换到彩色图像模式",
"2 - 切换到深度图像模式",
"3 - 切换到红外图像模式",
"4 - 切换到彩色深度图像模式",
"s - 保存当前图像",
"q - 退出程序",
"================================="
]
for instruction in instructions:
print(instruction)
def run(self):
"""运行主循环"""
if not self.initialize_device():
return
self.display_instructions()
# 创建窗口
cv2.namedWindow('Azure Kinect Image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Azure Kinect Image', 1280, 720)
mode_names = {
1: "Color Image",
2: "Depth Image",
3: "Infrared Image",
4: "Colored Depth Image"
}
print(f"\n当前模式: {mode_names[self.current_mode]}")
while True:
try:
# 根据当前模式获取相应图像
if self.current_mode == 1:
ret, image = self.get_color_image()
elif self.current_mode == 2:
ret, image = self.get_depth_image()
if ret:
# 将深度图像转换为可视化格式
image = cv2.convertScaleAbs(image, alpha=0.05)
elif self.current_mode == 3:
ret, image = self.get_infrared_image()
if ret:
# 将红外图像转换为可视化格式
image = cv2.convertScaleAbs(image, alpha=0.5)
elif self.current_mode == 4:
ret, image = self.get_colored_depth_image()
if not ret:
continue
# 在图像上添加模式信息
mode_text = f"Mode: {mode_names[self.current_mode]}"
cv2.putText(image, mode_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
1, (0, 255, 0), 2, cv2.LINE_AA)
# 显示图像
cv2.imshow('Azure Kinect Image', image)
# 处理按键
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
print("退出程序...")
break
elif key == ord('1'):
self.current_mode = 1
print(f"切换到: {mode_names[self.current_mode]}")
elif key == ord('2'):
self.current_mode = 2
print(f"切换到: {mode_names[self.current_mode]}")
elif key == ord('3'):
self.current_mode = 3
print(f"切换到: {mode_names[self.current_mode]}")
elif key == ord('4'):
self.current_mode = 4
print(f"切换到: {mode_names[self.current_mode]}")
elif key == ord('s'):
self.save_image(image, mode_names[self.current_mode].replace(" ", "_"))
except KeyboardInterrupt:
print("\n程序被用户中断")
break
except Exception as e:
print(f"运行时错误: {e}")
break
# 清理资源
cv2.destroyAllWindows()
print("程序结束")
def main():
"""主函数"""
print("Azure Kinect SDK 1.4.0 图像获取示例")
print("作者: AI Assistant")
print("版本: 1.0\n")
# 创建并运行图像捕获实例
capture = AzureKinectImageCapture()
capture.run()
if __name__ == "__main__":
main()