关于HoloLens 2设备提供了研究者模式,便于开发者访问传感器的原始数据,进行科研开发,本博客根据github项目在线获得图像数据,但是目前达不到实时传输的效果,延迟很高,期望以后的深入学习可以解决。
一、实验准备
1、实验条件
- Windows 10 SDK 10.0.18362.0
- HoloLens 2 设备
- Unity 2020.3.31f1c1配置好MRTK混合现实开发包
- Visual Studio 2019
- Python3.9(opencv-python)
2、下载Github工程
HoloLens2-Unity-ResearchModeStreamer
3、打开HoloLens 2研究者模式
二、配置过程
1、配置VS环境
设置项目属性为release 和ARM64
Release+ARM64

2、注释工程项目中Eigen库

2、生成解决方案


3、更换Lib库
lib库里放了Opencv4.X的库,根据需要把附加依赖项直接换成opencv_world341.lib,这个文件包括项目所需所有lib

三、配置Unity项目
1、新建Unity的3D项目

2、将项目切换到Universal windows patform平台

3、新建路径

4、将前面编译好的文件拷贝到新建的Plugins文件夹下
E → HoloLens2-Unity-ResearchModeStreamer-master → HL2RmStreamUnityPlugin → ARM64 → Release → HL2RmStreamUnityPlugin
5、新建一个StreamerHL2.cs
复制下面代码到脚本里面

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices; // 引入用于与本地DLL进行交互的命名空间,支持平台调用服务(P/Invoke)
public class StreamerHL2 : MonoBehaviour
{
/// <summary>
/// 从HL2RmStreamUnityPlugin.dll导入Initialize函数
/// </summary>
/// <param name="EntryPoint">指定DLL中的函数名称为"Initialize"</param>
/// <param name="CallingConvention">指定调用约定为StdCall,确保与C++ DLL的正确交互</param>
[DllImport("HL2RmStreamUnityPlugin", EntryPoint = "Initialize", CallingConvention = CallingConvention.StdCall)]
public static extern void InitializeDll(); // 声明外部DLL函数,对应DLL中的Initialize函数
/// <summary>
/// Unity MonoBehaviour的Start方法,在游戏对象初始化时调用一次
/// 主要作用:初始化HoloLens 2研究模式数据流
/// </summary>
void Start()
{
// 注释掉的备选初始化方法,可能用于调试或不同初始化流程
//StartDll();
// 调用DLL初始化函数,启动HoloLens 2研究模式
// 这会初始化彩色相机、深度相机等传感器数据流
InitializeDll();
}
/// <summary>
/// Unity MonoBehaviour的Update方法,每帧调用一次
/// 当前为空实现,预留用于:
/// - 持续获取相机数据帧
/// - 处理传感器数据
/// - 更新数据流状态
/// - 错误处理和数据验证
/// </summary>
void Update()
{
// 预留用于实时数据流处理
// 可在此处添加帧数据获取、数据传输或状态监控逻辑
}
}
6、将脚本随便挂在在一个对象上,我挂载的是主相机

7、设置Unity 项目的兼容性
勾选
InternetClient, InternetClientServer, PrivateNetworkClientServer, WebCam, SpatialPerception 5个位置

8、编译unity工程

9、配置package文件中的兼容性

10、将如下字段添加到 Package 中
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"

11、在Package这一行中,找到"IgnorableNamespaces"的属性,添加“rescap”字段

12、在 Capabilityes添加如下字段
<rescap:Capability Name="perceptionSensorsExperimental" />

Package.appxmanifest文件全揽,RMSTest可更换文件名
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap2="http://schemas.microsoft.com/appx/manifest/uap/windows10/2" xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4" xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10" xmlns:mobile="http://schemas.microsoft.com/appx/manifest/mobile/windows10" IgnorableNamespaces="uap uap2 uap3 uap4 mp mobile iot rescap" xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10">
<Identity Name="Template3D" Publisher="CN=DefaultCompany" Version="1.0.0.0" />
<mp:PhoneIdentity PhoneProductId="edd907fa-2c4f-49b2-89a0-9fa371043c33" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>RMSTest</DisplayName>
<PublisherDisplayName>DefaultCompany</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.10240.0" MaxVersionTested="10.0.19041.0" />
</Dependencies>
<Resources>
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="Template3D.App">
<uap:VisualElements DisplayName="RMSTest" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="Template_3D" BackgroundColor="transparent">
<uap:DefaultTile ShortName="RMSTest" Wide310x150Logo="Assets\Wide310x150Logo.png" />
<uap:SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="#FFFFFF" />
<uap:InitialRotationPreference>
<uap:Rotation Preference="landscape" />
<uap:Rotation Preference="landscapeFlipped" />
<uap:Rotation Preference="portrait" />
<uap:Rotation Preference="portraitFlipped" />
</uap:InitialRotationPreference>
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<rescap:Capability Name="perceptionSensorsExperimental" />
<Capability Name="internetClient" />
<Capability Name="internetClientServer" />
<Capability Name="privateNetworkClientServer" />
<uap2:Capability Name="spatialPerception" />
<DeviceCapability Name="webcam" />
</Capabilities>
</Package>
四、VS编译与部署
1、打开下载好的github工程中的hololens2_simpleclient.py文件
E: → HoloLens2-Unity-ResearchModeStreamer-master → py
2、修改局域网IP

五、连接运行
1、将PC与HoloLens 2连接至同一局域网
2、vs部署HoloLens 2项目
D→MixedRealityLearning→RMSTest→build
3、选择项目配置
在项目属性页Release和ARM64下,选择调试,计算机名为HoloLens的IP

4、部署到HoloLens 2上
打开APP
5、运行hololens2_simpleclient.py文件

6、查看结果
(1)HoloLens 2彩色数据流

(2)HoloLens 2深度数据流


本文介绍如何使用HoloLens2的研究者模式获取原始传感器数据,并通过Unity和Python实现图像数据流的传输。尽管当前方案存在高延迟问题,但仍为科研开发提供了宝贵的基础。
13万+





