Skip to content

Commit

Permalink
v1.0.9 update OpenCVForUnity version to 2.6.4.
Browse files Browse the repository at this point in the history
  • Loading branch information
EnoxSoftware committed Nov 25, 2024
1 parent acb6ef1 commit b3bd683
Show file tree
Hide file tree
Showing 137 changed files with 505 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace HoloLensWithOpenCVForUnityExample
/// An example of marker based AR using OpenCVForUnity on Hololens.
/// Referring to https://github.com/opencv/opencv_contrib/blob/master/modules/aruco/samples/detect_markers.cpp.
/// </summary>
[RequireComponent(typeof(HLCameraStreamToMatHelper), typeof(ImageOptimizationHelper))]
[RequireComponent(typeof(HLCameraStream2MatHelper), typeof(ImageOptimizationHelper))]
public class HLArUcoExample : MonoBehaviour
{
[HeaderAttribute("Preview")]
Expand Down Expand Up @@ -149,7 +149,7 @@ public class HLArUcoExample : MonoBehaviour
/// <summary>
/// The webcam texture to mat helper.
/// </summary>
HLCameraStreamToMatHelper webCamTextureToMatHelper;
HLCameraStream2MatHelper webCamTextureToMatHelper;

/// <summary>
/// The image optimization helper.
Expand Down Expand Up @@ -253,11 +253,11 @@ protected void Start()
enableLerpFilterToggle.isOn = enableLerpFilter;

imageOptimizationHelper = gameObject.GetComponent<ImageOptimizationHelper>();
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStreamToMatHelper>();
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStream2MatHelper>();
#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
webCamTextureToMatHelper.frameMatAcquired += OnFrameMatAcquired;
#endif
webCamTextureToMatHelper.outputColorFormat = WebCamTextureToMatHelper.ColorFormat.GRAY;
webCamTextureToMatHelper.outputColorFormat = Source2MatHelperColorFormat.GRAY;
webCamTextureToMatHelper.Initialize();
}

Expand Down Expand Up @@ -506,12 +506,13 @@ public void OnWebCamTextureToMatHelperDisposed()
}

/// <summary>
/// Raises the web cam texture to mat helper error occurred event.
/// Raises the webcam texture to mat helper error occurred event.
/// </summary>
/// <param name="errorCode">Error code.</param>
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
/// <param name="message">Message.</param>
public void OnWebCamTextureToMatHelperErrorOccurred(Source2MatHelperErrorCode errorCode, string message)
{
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode + ":" + message);
}

#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
namespace HoloLensWithOpenCVForUnityExample
{
/// <summary>
/// HLCameraStreamToMatHelper Example
/// HLCameraStream2MatHelper Example
/// An example of image processing (comic filter) using OpenCVForUnity on Hololens.
/// Referring to http://dev.classmethod.jp/smartphone/opencv-manga-2/.
/// </summary>
[RequireComponent(typeof(HLCameraStreamToMatHelper))]
public class HLCameraStreamToMatHelperExample : MonoBehaviour
[RequireComponent(typeof(HLCameraStream2MatHelper))]
public class HLCameraStream2MatHelperExample : MonoBehaviour
{
/// <summary>
/// The rotate 90 degree toggle.
Expand Down Expand Up @@ -73,7 +73,7 @@ public class HLCameraStreamToMatHelperExample : MonoBehaviour
/// <summary>
/// The web cam texture to mat helper.
/// </summary>
HLCameraStreamToMatHelper webCamTextureToMatHelper;
HLCameraStream2MatHelper webCamTextureToMatHelper;

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

Expand All @@ -89,11 +89,11 @@ public class HLCameraStreamToMatHelperExample : MonoBehaviour
// Use this for initialization
protected void Start()
{
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStreamToMatHelper>();
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStream2MatHelper>();
#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
webCamTextureToMatHelper.frameMatAcquired += OnFrameMatAcquired;
#endif
webCamTextureToMatHelper.outputColorFormat = WebCamTextureToMatHelper.ColorFormat.BGRA;
webCamTextureToMatHelper.outputColorFormat = Source2MatHelperColorFormat.BGRA;
webCamTextureToMatHelper.Initialize();

// Update GUI state
Expand Down Expand Up @@ -184,12 +184,13 @@ public void OnWebCamTextureToMatHelperDisposed()
}

/// <summary>
/// Raises the web cam texture to mat helper error occurred event.
/// Raises the webcam texture to mat helper error occurred event.
/// </summary>
/// <param name="errorCode">Error code.</param>
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
/// <param name="message">Message.</param>
public void OnWebCamTextureToMatHelperErrorOccurred(Source2MatHelperErrorCode errorCode, string message)
{
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode + ":" + message);
}

#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
Expand Down Expand Up @@ -278,8 +279,8 @@ void Update()

DebugUtils.TrackTick();

// For BGRA or BGR format, use the fastMatToTexture2D method.
Utils.fastMatToTexture2D(bgraMat, texture);
// For BGRA or BGR format, use the matToTexture2DRaw method.
Utils.matToTexture2DRaw(bgraMat, texture);
}

if (webCamTextureToMatHelper.IsPlaying())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace HoloLensWithOpenCVForUnityExample
/// An example of detecting face using OpenCVForUnity on Hololens.
/// Referring to https://github.com/Itseez/opencv/blob/master/modules/objdetect/src/detection_based_tracker.cpp.
/// </summary>
[RequireComponent(typeof(HLCameraStreamToMatHelper), typeof(ImageOptimizationHelper))]
[RequireComponent(typeof(HLCameraStream2MatHelper), typeof(ImageOptimizationHelper))]
public class HLFaceDetectionExample : MonoBehaviour
{
/// <summary>
Expand Down Expand Up @@ -67,7 +67,7 @@ public class HLFaceDetectionExample : MonoBehaviour
/// <summary>
/// The webcam texture to mat helper.
/// </summary>
HLCameraStreamToMatHelper webCamTextureToMatHelper;
HLCameraStream2MatHelper webCamTextureToMatHelper;

/// <summary>
/// The image optimization helper.
Expand Down Expand Up @@ -177,22 +177,72 @@ bool isDetectingInFrameArrivedThread
public Text debugStr;


string cascade_filepath;
string cascade4Thread_filepath;

/// <summary>
/// The CancellationTokenSource.
/// </summary>
CancellationTokenSource cts = new CancellationTokenSource();

// Use this for initialization
protected void Start()
async void Start()
{
enableDownScaleToggle.isOn = enableDownScale;
useSeparateDetectionToggle.isOn = useSeparateDetection;
displayCameraImageToggle.isOn = displayCameraImage;

imageOptimizationHelper = gameObject.GetComponent<ImageOptimizationHelper>();
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStreamToMatHelper>();
webCamTextureToMatHelper = gameObject.GetComponent<HLCameraStream2MatHelper>();
#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
webCamTextureToMatHelper.frameMatAcquired += OnFrameMatAcquired;
#endif
webCamTextureToMatHelper.outputColorFormat = WebCamTextureToMatHelper.ColorFormat.GRAY;
webCamTextureToMatHelper.Initialize();

rectangleTracker = new RectangleTracker();

// Asynchronously retrieves the readable file path from the StreamingAssets directory.
if (debugStr != null)
{
debugStr.text = "Preparing file access...";
}

cascade_filepath = await Utils.getFilePathAsyncTask("OpenCVForUnity/objdetect/lbpcascade_frontalface.xml", cancellationToken: cts.Token);
//cascade4Thread_filepath = await Utils.getFilePathAsyncTask("OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml", cancellationToken: cts.Token);
cascade4Thread_filepath = await Utils.getFilePathAsyncTask("OpenCVForUnity/objdetect/lbpcascade_frontalface.xml", cancellationToken: cts.Token);

if (debugStr != null)
{
debugStr.text = "";
}

Run();
}

// Use this for initialization
void Run()
{
cascade = new CascadeClassifier();
cascade.load(cascade_filepath);
#if !WINDOWS_UWP || UNITY_EDITOR
// "empty" method is not working on the UWP platform.
if (cascade.empty())
{
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
}
#endif

cascade4Thread = new CascadeClassifier();
cascade4Thread.load(cascade4Thread_filepath);
#if !WINDOWS_UWP || UNITY_EDITOR
// "empty" method is not working on the UWP platform.
if (cascade4Thread.empty())
{
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
}
#endif

webCamTextureToMatHelper.outputColorFormat = Source2MatHelperColorFormat.GRAY;
webCamTextureToMatHelper.Initialize();
}

/// <summary>
Expand Down Expand Up @@ -248,28 +298,7 @@ public void OnWebCamTextureToMatHelperInitialized()

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


cascade = new CascadeClassifier();
cascade.load(Utils.getFilePath("OpenCVForUnity/objdetect/lbpcascade_frontalface.xml"));
#if !WINDOWS_UWP || UNITY_EDITOR
// "empty" method is not working on the UWP platform.
if (cascade.empty())
{
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
}
#endif

grayMat4Thread = new Mat();
cascade4Thread = new CascadeClassifier();
//cascade4Thread.load(Utils.getFilePath("OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml"));
cascade4Thread.load(Utils.getFilePath("OpenCVForUnity/objdetect/lbpcascade_frontalface.xml"));
#if !WINDOWS_UWP || UNITY_EDITOR
// "empty" method is not working on the UWP platform.
if (cascade4Thread.empty())
{
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
}
#endif
}

/// <summary>
Expand All @@ -295,15 +324,9 @@ public void OnWebCamTextureToMatHelperDisposed()
hasUpdatedDetectionResult = false;
isDetecting = false;

if (cascade != null)
cascade.Dispose();

if (grayMat4Thread != null)
grayMat4Thread.Dispose();

if (cascade4Thread != null)
cascade4Thread.Dispose();

rectangleTracker.Reset();

if (debugStr != null)
Expand All @@ -314,12 +337,13 @@ public void OnWebCamTextureToMatHelperDisposed()
}

/// <summary>
/// Raises the web cam texture to mat helper error occurred event.
/// Raises the webcam texture to mat helper error occurred event.
/// </summary>
/// <param name="errorCode">Error code.</param>
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
/// <param name="message">Message.</param>
public void OnWebCamTextureToMatHelperErrorOccurred(Source2MatHelperErrorCode errorCode, string message)
{
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode + ":" + message);
}

#if WINDOWS_UWP && !DISABLE_HOLOLENSCAMSTREAM_API
Expand Down Expand Up @@ -795,6 +819,15 @@ void OnDestroy()

if (rectangleTracker != null)
rectangleTracker.Dispose();

if (cascade != null)
cascade.Dispose();

if (cascade4Thread != null)
cascade4Thread.Dispose();

if (cts != null)
cts.Dispose();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.MixedReality.Toolkit.Input;
using System.Collections;
using System.Linq;
using System.Threading;

#if UNITY_2018_2_OR_NEWER
using UnityEngine.Windows.WebCam;
Expand Down Expand Up @@ -41,12 +42,31 @@ public class HLPhotoCaptureExample : MonoBehaviour
private CascadeClassifier cascade;
private MatOfRect faces;

private IEnumerator Start()
/// <summary>
/// The CancellationTokenSource.
/// </summary>
CancellationTokenSource cts = new CancellationTokenSource();

// Use this for initialization
async void Start()
{
faces = new MatOfRect();

// Asynchronously retrieves the readable file path from the StreamingAssets directory.
if (text != null)
{
text.text = "Preparing file access...";
}

string cascade_filepath = await Utils.getFilePathAsyncTask("OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml", cancellationToken: cts.Token);

if (text != null)
{
text.text = "";
}

cascade = new CascadeClassifier();
cascade.load(Utils.getFilePath("OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml"));
cascade.load(cascade_filepath);


var resolutions = PhotoCapture.SupportedResolutions;
Expand All @@ -56,7 +76,7 @@ private IEnumerator Start()
{
text.text = "Resolutions not available. Did you provide web cam access?";
}
yield return null;
return;
}

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

if (cascade != null)
cascade.Dispose();

if (cts != null)
cts.Dispose();
}

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

// For BGRA or BGR format, use the fastTexture2DToMat method.
Utils.fastTexture2DToMat(targetTexture, bgraMat);
// For BGRA or BGR format, use the texture2DToMatRaw method.
Utils.texture2DToMatRaw(targetTexture, bgraMat);

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

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);

// For BGRA or BGR format, use the fastMatToTexture2D method.
Utils.fastMatToTexture2D(bgraMat, targetTexture);
// For BGRA or BGR format, use the matToTexture2DRaw method.
Utils.matToTexture2DRaw(bgraMat, targetTexture);
bgraMat.Dispose();
grayMat.Dispose();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public void OnHLPhotoCaptureExampleButtonClick()
SceneManager.LoadScene("HLPhotoCaptureExample");
}

public void OnHLCameraStreamToMatHelperExampleButtonClick()
public void OnHLCameraStream2MatHelperExampleButtonClick()
{
SceneManager.LoadScene("HLCameraStreamToMatHelperExample");
SceneManager.LoadScene("HLCameraStream2MatHelperExample");
}

public void OnHLFaceDetectionExampleButtonClick()
Expand Down
Loading

0 comments on commit b3bd683

Please sign in to comment.