Skip to content

Commit ad81122

Browse files
ZENBOOK14ZENBOOK14
ZENBOOK14
authored and
ZENBOOK14
committed
v1.0.5 update OpenCVForUnity version to 2.6.4. update DlibFaceLandmarkDetector version to 1.4.1.
1 parent 3dac8f5 commit ad81122

File tree

15 files changed

+1626
-291
lines changed

15 files changed

+1626
-291
lines changed

Assets/NrealLightWithDlibFaceLandmarkDetectorExample/Materials/holographicImageBlendGrayscale_material.mat

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
%TAG !u! tag:unity3d.com,2011:
33
--- !u!21 &2100000
44
Material:
5-
serializedVersion: 6
5+
serializedVersion: 8
66
m_ObjectHideFlags: 0
77
m_CorrespondingSourceObject: {fileID: 0}
88
m_PrefabInstance: {fileID: 0}
99
m_PrefabAsset: {fileID: 0}
1010
m_Name: holographicImageBlendGrayscale_material
1111
m_Shader: {fileID: 4800000, guid: bfd38a7c0f219d04cbb85cccd67e6c24, type: 3}
12-
m_ShaderKeywords:
12+
m_ValidKeywords: []
13+
m_InvalidKeywords: []
1314
m_LightmapFlags: 5
1415
m_EnableInstancingVariants: 0
1516
m_DoubleSidedGI: 0
@@ -23,6 +24,7 @@ Material:
2324
m_Texture: {fileID: 0}
2425
m_Scale: {x: 1, y: 1}
2526
m_Offset: {x: 0, y: 0}
27+
m_Ints: []
2628
m_Floats:
2729
- _VignetteScale: 0
2830
m_Colors:

Assets/NrealLightWithDlibFaceLandmarkDetectorExample/NrealARHeadExample/NrealARHeadExample.cs

Lines changed: 158 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
using DlibFaceLandmarkDetector;
44
using NrealLightWithOpenCVForUnity.UnityUtils.Helper;
5+
using NrealLightWithDlibFaceLandmarkDetectorExample.RectangleTrack;
56
using NRKernal;
67
using OpenCVForUnity.Calib3dModule;
78
using OpenCVForUnity.CoreModule;
89
using OpenCVForUnity.ImgprocModule;
910
using OpenCVForUnity.ObjdetectModule;
10-
using OpenCVForUnity.RectangleTrack;
1111
using OpenCVForUnity.UnityUtils;
1212
using OpenCVForUnity.UnityUtils.Helper;
1313
using System;
@@ -16,14 +16,15 @@
1616
using UnityEngine.SceneManagement;
1717
using UnityEngine.UI;
1818
using Rect = OpenCVForUnity.CoreModule.Rect;
19+
using System.Threading;
1920

2021
namespace NrealLightWithDlibFaceLandmarkDetectorExample
2122
{
2223
/// <summary>
2324
/// Nreal AR Head Example
2425
/// An example of AR head projection using OpenCVForUnity and DlibLandmarkDetector on Hololens.
2526
/// </summary>
26-
[RequireComponent(typeof(NRCamTextureToMatHelper), typeof(ImageOptimizationHelper))]
27+
[RequireComponent(typeof(NRCamTexture2MatHelper), typeof(ImageOptimizationHelper))]
2728
public class NrealARHeadExample : MonoBehaviour
2829
{
2930
[SerializeField, HeaderAttribute("Preview")]
@@ -262,7 +263,7 @@ public class NrealARHeadExample : MonoBehaviour
262263
/// <summary>
263264
/// The webcam texture to mat helper.
264265
/// </summary>
265-
NRCamTextureToMatHelper webCamTextureToMatHelper;
266+
NRCamTexture2MatHelper webCamTextureToMatHelper;
266267

267268
/// <summary>
268269
/// The image optimization helper.
@@ -364,7 +365,7 @@ bool hasUpdatedDetectionResult
364365
/// </summary>
365366
Camera mainCamera;
366367

367-
368+
/*
368369
// Use this for initialization
369370
protected void Start()
370371
{
@@ -379,8 +380,8 @@ protected void Start()
379380
enableLerpFilterToggle.isOn = enableLerpFilter;
380381
381382
imageOptimizationHelper = gameObject.GetComponent<ImageOptimizationHelper>();
382-
webCamTextureToMatHelper = gameObject.GetComponent<NRCamTextureToMatHelper>();
383-
webCamTextureToMatHelper.outputColorFormat = WebCamTextureToMatHelper.ColorFormat.GRAY;
383+
webCamTextureToMatHelper = gameObject.GetComponent<NRCamTexture2MatHelper>();
384+
webCamTextureToMatHelper.outputColorFormat = Source2MatHelperColorFormat.GRAY;
384385
webCamTextureToMatHelper.Initialize();
385386
386387
rectangleTracker = new RectangleTracker();
@@ -446,6 +447,132 @@ protected void Start()
446447
opticalFlowFilter = new OFPointsFilter((int)faceLandmarkDetector.GetShapePredictorNumParts());
447448
opticalFlowFilter.diffCheckSensitivity /= imageOptimizationHelper.downscaleRatio;
448449
}
450+
*/
451+
452+
453+
//////
454+
string cascade_filepath;
455+
string cascade4Thread_filepath;
456+
string dlibShapePredictor_filepath;
457+
string dlibShapePredictor4Thread_filepath;
458+
459+
/// <summary>
460+
/// The CancellationTokenSource.
461+
/// </summary>
462+
CancellationTokenSource cts = new CancellationTokenSource();
463+
464+
// Use this for initialization
465+
async void Start()
466+
{
467+
displayCameraPreviewToggle.isOn = displayCameraPreview;
468+
enableDownScaleToggle.isOn = enableDownScale;
469+
useSeparateDetectionToggle.isOn = useSeparateDetection;
470+
useOpenCVDetectorToggle.isOn = useOpenCVDetector;
471+
displayAxesToggle.isOn = displayAxes;
472+
displayHeadToggle.isOn = displayHead;
473+
displayEffectsToggle.isOn = displayEffects;
474+
enableOpticalFlowFilterToggle.isOn = enableOpticalFlowFilter;
475+
enableLerpFilterToggle.isOn = enableLerpFilter;
476+
477+
imageOptimizationHelper = gameObject.GetComponent<ImageOptimizationHelper>();
478+
webCamTextureToMatHelper = gameObject.GetComponent<NRCamTexture2MatHelper>();
479+
480+
rectangleTracker = new RectangleTracker();
481+
482+
483+
// Asynchronously retrieves the readable file path from the StreamingAssets directory.
484+
Debug.Log("Preparing file access...");
485+
486+
cascade_filepath = await DlibFaceLandmarkDetector.UnityUtils.Utils.getFilePathAsyncTask("OpenCVForUnity/objdetect/lbpcascade_frontalface.xml", cancellationToken: cts.Token);
487+
cascade4Thread_filepath = await DlibFaceLandmarkDetector.UnityUtils.Utils.getFilePathAsyncTask("OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml", cancellationToken: cts.Token);
488+
dlibShapePredictorFileName = NrealLightWithDlibFaceLandmarkDetectorExample.dlibShapePredictorFileName;
489+
dlibShapePredictor_filepath = await DlibFaceLandmarkDetector.UnityUtils.Utils.getFilePathAsyncTask(dlibShapePredictorFileName, cancellationToken: cts.Token);
490+
dlibShapePredictor4Thread_filepath = await DlibFaceLandmarkDetector.UnityUtils.Utils.getFilePathAsyncTask("DlibFaceLandmarkDetector/sp_human_face_6.dat", cancellationToken: cts.Token);
491+
492+
Debug.Log("Preparing file access complete!");
493+
494+
Run();
495+
}
496+
497+
// Use this for initialization
498+
void Run()
499+
{
500+
cascade = new CascadeClassifier();
501+
cascade.load(cascade_filepath);
502+
if (cascade.empty())
503+
{
504+
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
505+
}
506+
507+
cascade4Thread = new CascadeClassifier();
508+
cascade4Thread.load(cascade4Thread_filepath);
509+
if (cascade4Thread.empty())
510+
{
511+
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
512+
}
513+
514+
if (string.IsNullOrEmpty(dlibShapePredictor_filepath))
515+
{
516+
Debug.LogError("shape predictor file does not exist. Please copy from “DlibFaceLandmarkDetector/StreamingAssets/DlibFaceLandmarkDetector/” to “Assets/StreamingAssets/DlibFaceLandmarkDetector/” folder. ");
517+
}
518+
faceLandmarkDetector = new FaceLandmarkDetector(dlibShapePredictor_filepath);
519+
520+
if (string.IsNullOrEmpty(dlibShapePredictor4Thread_filepath))
521+
{
522+
Debug.LogError("shape predictor file does not exist. Please copy from “DlibFaceLandmarkDetector/StreamingAssets/DlibFaceLandmarkDetector/” to “Assets/StreamingAssets/DlibFaceLandmarkDetector/” folder. ");
523+
}
524+
faceLandmarkDetector4Thread = new FaceLandmarkDetector(dlibShapePredictor4Thread_filepath);
525+
526+
527+
// set 3d face object points. (right-handed coordinates system)
528+
objectPoints68 = new MatOfPoint3f(
529+
new Point3(-34, 90, 83),//l eye (Interpupillary breadth)
530+
new Point3(34, 90, 83),//r eye (Interpupillary breadth)
531+
new Point3(0.0, 50, 117),//nose (Tip)
532+
new Point3(0.0, 32, 97),//nose (Subnasale)
533+
new Point3(-79, 90, 10),//l ear (Bitragion breadth)
534+
new Point3(79, 90, 10)//r ear (Bitragion breadth)
535+
);
536+
537+
objectPoints17 = new MatOfPoint3f(
538+
new Point3(-34, 90, 83),//l eye (Interpupillary breadth)
539+
new Point3(34, 90, 83),//r eye (Interpupillary breadth)
540+
new Point3(0.0, 50, 117),//nose (Tip)
541+
new Point3(0.0, 32, 97),//nose (Subnasale)
542+
new Point3(-79, 90, 10),//l ear (Bitragion breadth)
543+
new Point3(79, 90, 10)//r ear (Bitragion breadth)
544+
);
545+
546+
objectPoints6 = new MatOfPoint3f(
547+
new Point3(-34, 90, 83),//l eye (Interpupillary breadth)
548+
new Point3(34, 90, 83),//r eye (Interpupillary breadth)
549+
new Point3(0.0, 50, 117),//nose (Tip)
550+
new Point3(0.0, 32, 97)//nose (Subnasale)
551+
);
552+
553+
objectPoints5 = new MatOfPoint3f(
554+
new Point3(-23, 90, 83),//l eye (Inner corner of the eye)
555+
new Point3(23, 90, 83),//r eye (Inner corner of the eye)
556+
new Point3(-50, 90, 80),//l eye (Tail of the eye)
557+
new Point3(50, 90, 80),//r eye (Tail of the eye)
558+
new Point3(0.0, 32, 97)//nose (Subnasale)
559+
);
560+
561+
// adjust object points to the scale of real world space.
562+
AjustPointScale(objectPoints68, 0.001);
563+
AjustPointScale(objectPoints17, 0.001);
564+
AjustPointScale(objectPoints6, 0.001);
565+
AjustPointScale(objectPoints5, 0.001);
566+
567+
imagePoints = new MatOfPoint2f();
568+
569+
opticalFlowFilter = new OFPointsFilter((int)faceLandmarkDetector.GetShapePredictorNumParts());
570+
opticalFlowFilter.diffCheckSensitivity /= imageOptimizationHelper.downscaleRatio;
571+
572+
webCamTextureToMatHelper.outputColorFormat = Source2MatHelperColorFormat.GRAY;
573+
webCamTextureToMatHelper.Initialize();
574+
}
575+
//////
449576

450577
/// <summary>
451578
/// Raises the web cam texture to mat helper initialized event.
@@ -559,7 +686,7 @@ public void OnWebCamTextureToMatHelperInitialized()
559686

560687
mouthParticleSystem = mouth.GetComponentsInChildren<ParticleSystem>(true);
561688

562-
689+
/*
563690
//grayMat = new Mat();
564691
cascade = new CascadeClassifier();
565692
cascade.load(Utils.getFilePath("OpenCVForUnity/objdetect/lbpcascade_frontalface.xml"));
@@ -581,6 +708,11 @@ public void OnWebCamTextureToMatHelperInitialized()
581708
Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/objdetect/” to “Assets/StreamingAssets/OpenCVForUnity/objdetect/” folder. ");
582709
}
583710
#endif
711+
*/
712+
713+
/////
714+
grayMat4Thread = new Mat();
715+
/////
584716
}
585717

586718
/// <summary>
@@ -604,14 +736,14 @@ public void OnWebCamTextureToMatHelperDisposed()
604736
texture = null;
605737
}
606738

607-
if (cascade != null)
608-
cascade.Dispose();
739+
//if (cascade != null)
740+
// cascade.Dispose();
609741

610742
if (grayMat4Thread != null)
611743
grayMat4Thread.Dispose();
612744

613-
if (cascade4Thread != null)
614-
cascade4Thread.Dispose();
745+
//if (cascade4Thread != null)
746+
// cascade4Thread.Dispose();
615747

616748
rectangleTracker.Reset();
617749

@@ -635,12 +767,13 @@ public void OnWebCamTextureToMatHelperDisposed()
635767
}
636768

637769
/// <summary>
638-
/// Raises the web cam texture to mat helper error occurred event.
770+
/// Raises the webcam texture to mat helper error occurred event.
639771
/// </summary>
640772
/// <param name="errorCode">Error code.</param>
641-
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
773+
/// <param name="message">Message.</param>
774+
public void OnWebCamTextureToMatHelperErrorOccurred(Source2MatHelperErrorCode errorCode, string message)
642775
{
643-
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
776+
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode + ":" + message);
644777
}
645778

646779
// Update is called once per frame
@@ -816,7 +949,7 @@ void Update()
816949

817950
if (displayCameraPreview)
818951
{
819-
Utils.fastMatToTexture2D(grayMat, texture);
952+
Utils.matToTexture2D(grayMat, texture);
820953
}
821954
}
822955
}
@@ -1243,6 +1376,15 @@ void OnDestroy()
12431376

12441377
if (opticalFlowFilter != null)
12451378
opticalFlowFilter.Dispose();
1379+
1380+
if (cascade != null)
1381+
cascade.Dispose();
1382+
1383+
if (cascade4Thread != null)
1384+
cascade4Thread.Dispose();
1385+
1386+
if (cts != null)
1387+
cts.Dispose();
12461388
}
12471389

12481390
/// <summary>
@@ -1282,7 +1424,7 @@ public void OnStopButtonClick()
12821424
/// </summary>
12831425
public void OnChangeCameraButtonClick()
12841426
{
1285-
webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.IsFrontFacing();
1427+
webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.requestedIsFrontFacing;
12861428
}
12871429

12881430
/// <summary>

0 commit comments

Comments
 (0)