Skip to content

Commit b3bd683

Browse files
committed
v1.0.9 update OpenCVForUnity version to 2.6.4.
1 parent acb6ef1 commit b3bd683

File tree

137 files changed

+505
-287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+505
-287
lines changed
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace HoloLensWithOpenCVForUnityExample
2222
/// An example of marker based AR using OpenCVForUnity on Hololens.
2323
/// Referring to https://github.com/opencv/opencv_contrib/blob/master/modules/aruco/samples/detect_markers.cpp.
2424
/// </summary>
25-
[RequireComponent(typeof(HLCameraStreamToMatHelper), typeof(ImageOptimizationHelper))]
25+
[RequireComponent(typeof(HLCameraStream2MatHelper), typeof(ImageOptimizationHelper))]
2626
public class HLArUcoExample : MonoBehaviour
2727
{
2828
[HeaderAttribute("Preview")]
@@ -149,7 +149,7 @@ public class HLArUcoExample : MonoBehaviour
149149
/// <summary>
150150
/// The webcam texture to mat helper.
151151
/// </summary>
152-
HLCameraStreamToMatHelper webCamTextureToMatHelper;
152+
HLCameraStream2MatHelper webCamTextureToMatHelper;
153153

154154
/// <summary>
155155
/// The image optimization helper.
@@ -253,11 +253,11 @@ protected void Start()
253253
enableLerpFilterToggle.isOn = enableLerpFilter;
254254

255255
imageOptimizationHelper = gameObject.GetComponent<ImageOptimizationHelper>();
256-
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStreamToMatHelper>();
256+
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStream2MatHelper>();
257257
#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
258258
webCamTextureToMatHelper.frameMatAcquired += OnFrameMatAcquired;
259259
#endif
260-
webCamTextureToMatHelper.outputColorFormat = WebCamTextureToMatHelper.ColorFormat.GRAY;
260+
webCamTextureToMatHelper.outputColorFormat = Source2MatHelperColorFormat.GRAY;
261261
webCamTextureToMatHelper.Initialize();
262262
}
263263

@@ -506,12 +506,13 @@ public void OnWebCamTextureToMatHelperDisposed()
506506
}
507507

508508
/// <summary>
509-
/// Raises the web cam texture to mat helper error occurred event.
509+
/// Raises the webcam texture to mat helper error occurred event.
510510
/// </summary>
511511
/// <param name="errorCode">Error code.</param>
512-
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
512+
/// <param name="message">Message.</param>
513+
public void OnWebCamTextureToMatHelperErrorOccurred(Source2MatHelperErrorCode errorCode, string message)
513514
{
514-
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
515+
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode + ":" + message);
515516
}
516517

517518
#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
namespace HoloLensWithOpenCVForUnityExample
1414
{
1515
/// <summary>
16-
/// HLCameraStreamToMatHelper Example
16+
/// HLCameraStream2MatHelper Example
1717
/// An example of image processing (comic filter) using OpenCVForUnity on Hololens.
1818
/// Referring to http://dev.classmethod.jp/smartphone/opencv-manga-2/.
1919
/// </summary>
20-
[RequireComponent(typeof(HLCameraStreamToMatHelper))]
21-
public class HLCameraStreamToMatHelperExample : MonoBehaviour
20+
[RequireComponent(typeof(HLCameraStream2MatHelper))]
21+
public class HLCameraStream2MatHelperExample : MonoBehaviour
2222
{
2323
/// <summary>
2424
/// The rotate 90 degree toggle.
@@ -73,7 +73,7 @@ public class HLCameraStreamToMatHelperExample : MonoBehaviour
7373
/// <summary>
7474
/// The web cam texture to mat helper.
7575
/// </summary>
76-
HLCameraStreamToMatHelper webCamTextureToMatHelper;
76+
HLCameraStream2MatHelper webCamTextureToMatHelper;
7777

7878
readonly static Queue<Action> ExecuteOnMainThread = new Queue<Action>();
7979

@@ -89,11 +89,11 @@ public class HLCameraStreamToMatHelperExample : MonoBehaviour
8989
// Use this for initialization
9090
protected void Start()
9191
{
92-
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStreamToMatHelper>();
92+
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStream2MatHelper>();
9393
#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
9494
webCamTextureToMatHelper.frameMatAcquired += OnFrameMatAcquired;
9595
#endif
96-
webCamTextureToMatHelper.outputColorFormat = WebCamTextureToMatHelper.ColorFormat.BGRA;
96+
webCamTextureToMatHelper.outputColorFormat = Source2MatHelperColorFormat.BGRA;
9797
webCamTextureToMatHelper.Initialize();
9898

9999
// Update GUI state
@@ -184,12 +184,13 @@ public void OnWebCamTextureToMatHelperDisposed()
184184
}
185185

186186
/// <summary>
187-
/// Raises the web cam texture to mat helper error occurred event.
187+
/// Raises the webcam texture to mat helper error occurred event.
188188
/// </summary>
189189
/// <param name="errorCode">Error code.</param>
190-
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
190+
/// <param name="message">Message.</param>
191+
public void OnWebCamTextureToMatHelperErrorOccurred(Source2MatHelperErrorCode errorCode, string message)
191192
{
192-
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
193+
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode + ":" + message);
193194
}
194195

195196
#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
@@ -278,8 +279,8 @@ void Update()
278279

279280
DebugUtils.TrackTick();
280281

281-
// For BGRA or BGR format, use the fastMatToTexture2D method.
282-
Utils.fastMatToTexture2D(bgraMat, texture);
282+
// For BGRA or BGR format, use the matToTexture2DRaw method.
283+
Utils.matToTexture2DRaw(bgraMat, texture);
283284
}
284285

285286
if (webCamTextureToMatHelper.IsPlaying())
Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace HoloLensWithOpenCVForUnityExample
2121
/// An example of detecting face using OpenCVForUnity on Hololens.
2222
/// Referring to https://github.com/Itseez/opencv/blob/master/modules/objdetect/src/detection_based_tracker.cpp.
2323
/// </summary>
24-
[RequireComponent(typeof(HLCameraStreamToMatHelper), typeof(ImageOptimizationHelper))]
24+
[RequireComponent(typeof(HLCameraStream2MatHelper), typeof(ImageOptimizationHelper))]
2525
public class HLFaceDetectionExample : MonoBehaviour
2626
{
2727
/// <summary>
@@ -67,7 +67,7 @@ public class HLFaceDetectionExample : MonoBehaviour
6767
/// <summary>
6868
/// The webcam texture to mat helper.
6969
/// </summary>
70-
HLCameraStreamToMatHelper webCamTextureToMatHelper;
70+
HLCameraStream2MatHelper webCamTextureToMatHelper;
7171

7272
/// <summary>
7373
/// The image optimization helper.
@@ -177,22 +177,72 @@ bool isDetectingInFrameArrivedThread
177177
public Text debugStr;
178178

179179

180+
string cascade_filepath;
181+
string cascade4Thread_filepath;
182+
183+
/// <summary>
184+
/// The CancellationTokenSource.
185+
/// </summary>
186+
CancellationTokenSource cts = new CancellationTokenSource();
187+
180188
// Use this for initialization
181-
protected void Start()
189+
async void Start()
182190
{
183191
enableDownScaleToggle.isOn = enableDownScale;
184192
useSeparateDetectionToggle.isOn = useSeparateDetection;
185193
displayCameraImageToggle.isOn = displayCameraImage;
186194

187195
imageOptimizationHelper = gameObject.GetComponent<ImageOptimizationHelper>();
188-
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStreamToMatHelper>();
196+
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStream2MatHelper>();
189197
#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
190198
webCamTextureToMatHelper.frameMatAcquired += OnFrameMatAcquired;
191199
#endif
192-
webCamTextureToMatHelper.outputColorFormat = WebCamTextureToMatHelper.ColorFormat.GRAY;
193-
webCamTextureToMatHelper.Initialize();
194200

195201
rectangleTracker = new RectangleTracker();
202+
203+
// Asynchronously retrieves the readable file path from the StreamingAssets directory.
204+
if (debugStr != null)
205+
{
206+
debugStr.text = "Preparing file access...";
207+
}
208+
209+
cascade_filepath = await Utils.getFilePathAsyncTask("OpenCVForUnity/objdetect/lbpcascade_frontalface.xml", cancellationToken: cts.Token);
210+
//cascade4Thread_filepath = await Utils.getFilePathAsyncTask("OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml", cancellationToken: cts.Token);
211+
cascade4Thread_filepath = await Utils.getFilePathAsyncTask("OpenCVForUnity/objdetect/lbpcascade_frontalface.xml", cancellationToken: cts.Token);
212+
213+
if (debugStr != null)
214+
{
215+
debugStr.text = "";
216+
}
217+
218+
Run();
219+
}
220+
221+
// Use this for initialization
222+
void Run()
223+
{
224+
cascade = new CascadeClassifier();
225+
cascade.load(cascade_filepath);
226+
#if !WINDOWS_UWP || UNITY_EDITOR
227+
// "empty" method is not working on the UWP platform.
228+
if (cascade.empty())
229+
{
230+
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
231+
}
232+
#endif
233+
234+
cascade4Thread = new CascadeClassifier();
235+
cascade4Thread.load(cascade4Thread_filepath);
236+
#if !WINDOWS_UWP || UNITY_EDITOR
237+
// "empty" method is not working on the UWP platform.
238+
if (cascade4Thread.empty())
239+
{
240+
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
241+
}
242+
#endif
243+
244+
webCamTextureToMatHelper.outputColorFormat = Source2MatHelperColorFormat.GRAY;
245+
webCamTextureToMatHelper.Initialize();
196246
}
197247

198248
/// <summary>
@@ -248,28 +298,7 @@ public void OnWebCamTextureToMatHelperInitialized()
248298

249299
quad_renderer.sharedMaterial.SetFloat("_VignetteScale", 0.0f);
250300

251-
252-
cascade = new CascadeClassifier();
253-
cascade.load(Utils.getFilePath("OpenCVForUnity/objdetect/lbpcascade_frontalface.xml"));
254-
#if !WINDOWS_UWP || UNITY_EDITOR
255-
// "empty" method is not working on the UWP platform.
256-
if (cascade.empty())
257-
{
258-
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
259-
}
260-
#endif
261-
262301
grayMat4Thread = new Mat();
263-
cascade4Thread = new CascadeClassifier();
264-
//cascade4Thread.load(Utils.getFilePath("OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml"));
265-
cascade4Thread.load(Utils.getFilePath("OpenCVForUnity/objdetect/lbpcascade_frontalface.xml"));
266-
#if !WINDOWS_UWP || UNITY_EDITOR
267-
// "empty" method is not working on the UWP platform.
268-
if (cascade4Thread.empty())
269-
{
270-
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
271-
}
272-
#endif
273302
}
274303

275304
/// <summary>
@@ -295,15 +324,9 @@ public void OnWebCamTextureToMatHelperDisposed()
295324
hasUpdatedDetectionResult = false;
296325
isDetecting = false;
297326

298-
if (cascade != null)
299-
cascade.Dispose();
300-
301327
if (grayMat4Thread != null)
302328
grayMat4Thread.Dispose();
303329

304-
if (cascade4Thread != null)
305-
cascade4Thread.Dispose();
306-
307330
rectangleTracker.Reset();
308331

309332
if (debugStr != null)
@@ -314,12 +337,13 @@ public void OnWebCamTextureToMatHelperDisposed()
314337
}
315338

316339
/// <summary>
317-
/// Raises the web cam texture to mat helper error occurred event.
340+
/// Raises the webcam texture to mat helper error occurred event.
318341
/// </summary>
319342
/// <param name="errorCode">Error code.</param>
320-
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
343+
/// <param name="message">Message.</param>
344+
public void OnWebCamTextureToMatHelperErrorOccurred(Source2MatHelperErrorCode errorCode, string message)
321345
{
322-
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
346+
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode + ":" + message);
323347
}
324348

325349
#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
@@ -795,6 +819,15 @@ void OnDestroy()
795819

796820
if (rectangleTracker != null)
797821
rectangleTracker.Dispose();
822+
823+
if (cascade != null)
824+
cascade.Dispose();
825+
826+
if (cascade4Thread != null)
827+
cascade4Thread.Dispose();
828+
829+
if (cts != null)
830+
cts.Dispose();
798831
}
799832

800833
/// <summary>
Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.MixedReality.Toolkit.Input;
1010
using System.Collections;
1111
using System.Linq;
12+
using System.Threading;
1213

1314
#if UNITY_2018_2_OR_NEWER
1415
using UnityEngine.Windows.WebCam;
@@ -41,12 +42,31 @@ public class HLPhotoCaptureExample : MonoBehaviour
4142
private CascadeClassifier cascade;
4243
private MatOfRect faces;
4344

44-
private IEnumerator Start()
45+
/// <summary>
46+
/// The CancellationTokenSource.
47+
/// </summary>
48+
CancellationTokenSource cts = new CancellationTokenSource();
49+
50+
// Use this for initialization
51+
async void Start()
4552
{
4653
faces = new MatOfRect();
4754

55+
// Asynchronously retrieves the readable file path from the StreamingAssets directory.
56+
if (text != null)
57+
{
58+
text.text = "Preparing file access...";
59+
}
60+
61+
string cascade_filepath = await Utils.getFilePathAsyncTask("OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml", cancellationToken: cts.Token);
62+
63+
if (text != null)
64+
{
65+
text.text = "";
66+
}
67+
4868
cascade = new CascadeClassifier();
49-
cascade.load(Utils.getFilePath("OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml"));
69+
cascade.load(cascade_filepath);
5070

5171

5272
var resolutions = PhotoCapture.SupportedResolutions;
@@ -56,7 +76,7 @@ private IEnumerator Start()
5676
{
5777
text.text = "Resolutions not available. Did you provide web cam access?";
5878
}
59-
yield return null;
79+
return;
6080
}
6181

6282
cameraResolution = resolutions.OrderByDescending((res) => res.width * res.height).First();
@@ -84,6 +104,9 @@ private void OnDestroy()
84104

85105
if (cascade != null)
86106
cascade.Dispose();
107+
108+
if (cts != null)
109+
cts.Dispose();
87110
}
88111

89112
private void OnPhotoCaptureCreated(PhotoCapture captureObject)
@@ -174,8 +197,8 @@ private void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptur
174197
Mat bgraMat = new Mat(targetTexture.height, targetTexture.width, CvType.CV_8UC4);
175198
Mat grayMat = new Mat(bgraMat.rows(), bgraMat.cols(), CvType.CV_8UC1);
176199

177-
// For BGRA or BGR format, use the fastTexture2DToMat method.
178-
Utils.fastTexture2DToMat(targetTexture, bgraMat);
200+
// For BGRA or BGR format, use the texture2DToMatRaw method.
201+
Utils.texture2DToMatRaw(targetTexture, bgraMat);
179202

180203
Imgproc.cvtColor(bgraMat, grayMat, Imgproc.COLOR_BGRA2GRAY);
181204
Imgproc.equalizeHist(grayMat, grayMat);
@@ -196,8 +219,8 @@ private void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptur
196219

197220
Imgproc.putText(bgraMat, targetTexture.format + " W:" + bgraMat.width() + " H:" + bgraMat.height(), new Point(5, bgraMat.rows() - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1.5, new Scalar(255, 0, 0, 255), 2, Imgproc.LINE_AA, false);
198221

199-
// For BGRA or BGR format, use the fastMatToTexture2D method.
200-
Utils.fastMatToTexture2D(bgraMat, targetTexture);
222+
// For BGRA or BGR format, use the matToTexture2DRaw method.
223+
Utils.matToTexture2DRaw(bgraMat, targetTexture);
201224
bgraMat.Dispose();
202225
grayMat.Dispose();
203226

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public void OnHLPhotoCaptureExampleButtonClick()
8888
SceneManager.LoadScene("HLPhotoCaptureExample");
8989
}
9090

91-
public void OnHLCameraStreamToMatHelperExampleButtonClick()
91+
public void OnHLCameraStream2MatHelperExampleButtonClick()
9292
{
93-
SceneManager.LoadScene("HLCameraStreamToMatHelperExample");
93+
SceneManager.LoadScene("HLCameraStream2MatHelperExample");
9494
}
9595

9696
public void OnHLFaceDetectionExampleButtonClick()

0 commit comments

Comments
 (0)