Skip to content

Commit bd2ae95

Browse files
committed
Fixed the HololensCameraStreamToMatHelper.
1 parent 2d92d06 commit bd2ae95

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

Assets/HoloLensWithOpenCVForUnityExample/Scripts/Utils/HololensCameraStreamToMatHelper.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace HoloLensWithOpenCVForUnityExample
2020

2121
/// <summary>
2222
/// Hololens camera stream to mat helper.
23-
/// v 1.0.0
23+
/// v 1.0.1
2424
///
2525
/// Combination of camera frame size and frame rate that can be acquired on Hololens. (width x height : framerate)
2626
/// 1280 x 720 : 30
@@ -63,7 +63,7 @@ public class HololensCameraStreamToMatHelper : WebCamTextureToMatHelper
6363
protected System.Object latestImageBytesLockObject = new System.Object ();
6464

6565
protected HoloLensCameraStream.VideoCapture videoCapture;
66-
protected CameraParameters cameraParams;
66+
protected HoloLensCameraStream.CameraParameters cameraParams;
6767

6868
protected Matrix4x4 _cameraToWorldMatrix = Matrix4x4.identity;
6969
protected Matrix4x4 cameraToWorldMatrix {
@@ -116,6 +116,14 @@ protected byte[] latestImageBytes {
116116
set { lock (lockObject)
117117
_hasInitDone = value; }
118118
}
119+
120+
protected bool _hasInitEventCompleted = false;
121+
protected bool hasInitEventCompleted {
122+
get { lock (lockObject)
123+
return _hasInitEventCompleted; }
124+
set { lock (lockObject)
125+
_hasInitEventCompleted = value; }
126+
}
119127

120128
protected virtual void LateUpdate ()
121129
{
@@ -159,7 +167,7 @@ protected virtual void OnFrameSampleAcquired(VideoCaptureSample sample)
159167
didUpdateThisFrame = true;
160168
didUpdateImageBufferInCurrentFrame = true;
161169

162-
if (hasInitDone && frameMatAcquired != null)
170+
if (hasInitEventCompleted && frameMatAcquired != null)
163171
{
164172
Mat mat = new Mat (cameraParams.cameraResolutionHeight, cameraParams.cameraResolutionWidth, CvType.CV_8UC4);
165173
OpenCVForUnity.Utils.copyToMat<byte> (latestImageBytes, mat);
@@ -181,15 +189,15 @@ protected virtual void OnFrameSampleAcquired(VideoCaptureSample sample)
181189
}
182190
}
183191

184-
protected virtual CameraParameters CreateCameraParams (HoloLensCameraStream.VideoCapture videoCapture)
192+
protected virtual HoloLensCameraStream.CameraParameters CreateCameraParams (HoloLensCameraStream.VideoCapture videoCapture)
185193
{
186194
int min1 = videoCapture.GetSupportedResolutions().Min (r => Mathf.Abs((r.width * r.height) - (_requestedWidth * _requestedHeight)));
187195
HoloLensCameraStream.Resolution resolution = videoCapture.GetSupportedResolutions().First (r => Mathf.Abs((r.width * r.height) - (_requestedWidth * _requestedHeight)) == min1);
188196

189197
float min2 = videoCapture.GetSupportedFrameRatesForResolution(resolution).Min (f => Mathf.Abs(f - _requestedFPS));
190198
float frameRate = videoCapture.GetSupportedFrameRatesForResolution(resolution).First (f => Mathf.Abs(f - _requestedFPS) == min2);
191199

192-
CameraParameters cameraParams = new CameraParameters();
200+
HoloLensCameraStream.CameraParameters cameraParams = new HoloLensCameraStream.CameraParameters();
193201
cameraParams.cameraResolutionHeight = resolution.height;
194202
cameraParams.cameraResolutionWidth = resolution.width;
195203
cameraParams.frameRate = Mathf.RoundToInt(frameRate);
@@ -279,7 +287,11 @@ protected override IEnumerator _Initialize ()
279287
} else {
280288

281289
//Fetch a pointer to Unity's spatial coordinate system if you need pixel mapping
290+
#if UNITY_2017_2_OR_NEWER
291+
spatialCoordinateSystemPtr = UnityEngine.XR.WSA.WorldManager.GetNativeISpatialCoordinateSystemPtr ();
292+
#else
282293
spatialCoordinateSystemPtr = UnityEngine.VR.WSA.WorldManager.GetNativeISpatialCoordinateSystemPtr ();
294+
#endif
283295

284296
HoloLensCameraStream.VideoCapture.CreateAync (videoCapture => {
285297

@@ -348,12 +360,13 @@ protected override IEnumerator _Initialize ()
348360
}
349361

350362
isInitWaiting = false;
363+
hasInitDone = true;
351364
initCoroutine = null;
352365

353366
if (onInitialized != null)
354367
onInitialized.Invoke ();
355368

356-
hasInitDone = true;
369+
hasInitEventCompleted = true;
357370

358371
break;
359372
} else {
@@ -390,6 +403,15 @@ protected override IEnumerator _Initialize ()
390403
}
391404
}
392405

406+
/// <summary>
407+
/// Indicates whether this instance has been initialized.
408+
/// </summary>
409+
/// <returns><c>true</c>, if this instance has been initialized, <c>false</c> otherwise.</returns>
410+
public override bool IsInitialized ()
411+
{
412+
return hasInitDone;
413+
}
414+
393415
/// <summary>
394416
/// Starts the webcam texture.
395417
/// </summary>
@@ -572,6 +594,7 @@ protected override void ReleaseResources ()
572594
{
573595
isInitWaiting = false;
574596
hasInitDone = false;
597+
hasInitEventCompleted = false;
575598

576599
latestImageBytes = null;
577600
didUpdateThisFrame = false;

Assets/HoloLensWithOpenCVForUnityExample/Scripts/Utils/WebCamTextureToMatHelper.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ namespace HoloLensWithOpenCVForUnityExample
99
{
1010
/// <summary>
1111
/// Webcam texture to mat helper.
12-
/// v 1.0.5
12+
/// v 1.0.6
1313
/// </summary>
1414
public class WebCamTextureToMatHelper : MonoBehaviour
1515
{
1616
/// <summary>
17-
/// Set the name of the device to use.
17+
/// Set the name of the device to use. (or device index)
1818
/// </summary>
19-
[SerializeField, FormerlySerializedAs("requestedDeviceName"), TooltipAttribute ("Set the name of the device to use.")]
19+
[SerializeField, FormerlySerializedAs("requestedDeviceName"), TooltipAttribute ("Set the name of the device to use. (or device index)")]
2020
protected string _requestedDeviceName = null;
2121
public string requestedDeviceName {
2222
get { return _requestedDeviceName; }
@@ -92,10 +92,11 @@ public bool flipHorizontal {
9292
get { return _flipHorizontal; }
9393
set { _flipHorizontal = value; }
9494
}
95-
95+
9696
/// <summary>
97-
/// The timeout frame count.
97+
/// The number of frames before the initialization process times out.
9898
/// </summary>
99+
[SerializeField, FormerlySerializedAs("timeoutFrameCount"), TooltipAttribute ("The number of frames before the initialization process times out.")]
99100
protected int _timeoutFrameCount = 300;
100101
public int timeoutFrameCount {
101102
get { return _timeoutFrameCount; }
@@ -275,10 +276,27 @@ protected virtual IEnumerator _Initialize ()
275276
isInitWaiting = true;
276277

277278
if (!String.IsNullOrEmpty (requestedDeviceName)) {
278-
webCamTexture = new WebCamTexture (requestedDeviceName, requestedWidth, requestedHeight, requestedFPS);
279-
} else {
279+
int requestedDeviceIndex = -1;
280+
if (Int32.TryParse (requestedDeviceName, out requestedDeviceIndex)) {
281+
if (requestedDeviceIndex >= 0 && requestedDeviceIndex < WebCamTexture.devices.Length) {
282+
webCamDevice = WebCamTexture.devices [requestedDeviceIndex];
283+
webCamTexture = new WebCamTexture (webCamDevice.name, requestedWidth, requestedHeight, requestedFPS);
284+
}
285+
} else {
286+
for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++) {
287+
if (WebCamTexture.devices [cameraIndex].name == requestedDeviceName) {
288+
webCamTexture = new WebCamTexture (requestedDeviceName, requestedWidth, requestedHeight, requestedFPS);
289+
}
290+
}
291+
}
292+
293+
if (webCamTexture == null)
294+
Debug.Log ("Cannot find webcam device " + requestedDeviceName + ".");
295+
}
296+
297+
if (webCamTexture == null) {
280298
// Checks how many and which cameras are available on the device
281-
for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++) {
299+
for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++) {
282300
if (WebCamTexture.devices [cameraIndex].isFrontFacing == requestedIsFrontFacing) {
283301

284302
webCamDevice = WebCamTexture.devices [cameraIndex];
@@ -334,7 +352,7 @@ protected virtual IEnumerator _Initialize ()
334352
#endif
335353
#endif
336354

337-
Debug.Log ("WebCamTextureToMatHelper:: " + "name:" + webCamTexture.name + " width:" + webCamTexture.width + " height:" + webCamTexture.height + " fps:" + webCamTexture.requestedFPS
355+
Debug.Log ("WebCamTextureToMatHelper:: " + "devicename:" + webCamTexture.deviceName + " name:" + webCamTexture.name + " width:" + webCamTexture.width + " height:" + webCamTexture.height + " fps:" + webCamTexture.requestedFPS
338356
+ " videoRotationAngle:" + webCamTexture.videoRotationAngle + " videoVerticallyMirrored:" + webCamTexture.videoVerticallyMirrored + " isFrongFacing:" + webCamDevice.isFrontFacing);
339357

340358
if (colors == null || colors.Length != webCamTexture.width * webCamTexture.height)

0 commit comments

Comments
 (0)