Software Archive
Read-only legacy content
17061 Discussions

Face Tracking Crashes Unity / Unity not responding

Shivang_D_
Beginner
693 Views

Hi,

I am trying to run Face tracking using following code but Unity is crashing without any error or response:

using UnityEngine;

using System;

using System.Collections;

using System.Collections.Generic;

using System.Threading;

public class FaceDetection : BaseAction {

	private PXCMSession session;
    private PXCMSenseManager senseManager;
    private PXCMFaceData faceData;
    private Thread update;
    private string alertMsg;


	void Start(){
		// Start SenseManager and configure the face module
            ConfigureRealSense();

        // Start the Update thread
            update = new Thread(new ThreadStart(Update));
            update.Start();
	}	
	
	// Use this for initialization
	void Update () {
		Int32 facesDetected = 0;
        Int32 faceH = 0;
        Int32 faceW = 0;
        Int32 faceX = 0;
        Int32 faceY = 0;
        float faceDepth = 0;

        // Start AcquireFrame-ReleaseFrame loop
        while (senseManager.AcquireFrame(true) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
        {
            // Acquire color image data
            PXCMCapture.Sample sample = senseManager.QuerySample();
            //Bitmap colorBitmap;
            PXCMImage.ImageData colorData;
            sample.color.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32, out colorData);
            //colorBitmap = colorData.ToBitmap(0, sample.color.info.width, sample.color.info.height);

            // Acquire face data
            if (faceData != null)
            {
                faceData.Update();
                facesDetected = faceData.QueryNumberOfDetectedFaces();

                if (facesDetected > 0)
                {
                    // Get the first face detected (index 0)
                    PXCMFaceData.Face face = faceData.QueryFaceByIndex(0);

                    // Retrieve face location data
                    PXCMFaceData.DetectionData faceDetectionData = face.QueryDetection();
                    if (faceDetectionData != null)
                    {
                        PXCMRectI32 faceRectangle;
                        faceDetectionData.QueryBoundingRect(out faceRectangle);
                        faceH = faceRectangle.h;
                        faceW = faceRectangle.w;
                        faceX = faceRectangle.x;
                        faceY = faceRectangle.y;

                        // Get average depth value of detected face
                        faceDetectionData.QueryFaceAverageDepth(out faceDepth);
                    }
                }
            }

            // Update UI
            //Render(colorBitmap, facesDetected, faceH, faceW, faceX, faceY, faceDepth);
            Debug.Log("Face height " + faceH);
            // Release the color frame
            //colorBitmap.Dispose();
            sample.color.ReleaseAccess(colorData);
            senseManager.ReleaseFrame();
        }
	}

/*	private void Render(Bitmap bitmap, Int32 count, Int32 h, Int32 w, Int32 x, Int32 y, float depth)
        {
            Debug.Log("object message");
        }
        */
        
    private void FaceAlertHandler(PXCMFaceData.AlertData alert){
        alertMsg = Convert.ToString(alert.label);
    }

	private void ConfigureRealSense()
        {
            PXCMFaceModule faceModule;
            PXCMFaceConfiguration faceConfig;

            // Start the SenseManager and session  
            session = PXCMSession.CreateInstance();
            senseManager = session.CreateSenseManager();

            // Enable the color stream
            senseManager.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_COLOR, 640, 480, 30);

            // Enable the face module
            senseManager.EnableFace();
            faceModule = senseManager.QueryFace();
            faceConfig = faceModule.CreateActiveConfiguration();

            // Configure for 3D face tracking
            faceConfig.SetTrackingMode(PXCMFaceConfiguration.TrackingModeType.FACE_MODE_COLOR_PLUS_DEPTH);

            // Known issue: Pose isEnabled must be set to false for R200 face tracking to work correctly
            faceConfig.pose.isEnabled = false;

            // Track faces based on their appearance in the scene
            faceConfig.strategy = PXCMFaceConfiguration.TrackingStrategyType.STRATEGY_APPEARANCE_TIME;
            
            // Set the module to track four faces in this example
            faceConfig.detection.maxTrackedFaces = 4;

            // Enable alert monitoring and subscribe to alert event hander
            faceConfig.EnableAllAlerts();
            faceConfig.SubscribeAlert(FaceAlertHandler);

            // Apply changes and initialize
            faceConfig.ApplyChanges();
            senseManager.Init();
            faceData = faceModule.CreateOutput();

            // Mirror the image
            senseManager.QueryCaptureManager().QueryDevice().SetMirrorMode(PXCMCapture.Device.MirrorMode.MIRROR_MODE_HORIZONTAL);

            // Release resources
            faceConfig.Dispose();
            faceModule.Dispose();
        }

        private void OnDestroy(){
            // Stop the Update thread
            update.Abort();

            // Dispose RealSense objects
            faceData.Dispose();
            senseManager.Dispose();
            session.Dispose();
        }

}

 The code I have modified from the following link:

https://software.intel.com/en-us/articles/intel-realsense-depth-camera-r200-code-sample-face-tracking

I am new at Unity and Intel RealSense and I am not aware how to structure the code. Any help with the above code or links for tutorial would be great.
 

Thanks,
Shivang

0 Kudos
2 Replies
Bryan_B_Intel1
Employee
693 Views

Hi Shivang,

Can you please provide the following:

1. The version of SDK and DCM are you running.
2. Verify you are using the rear facing R200 camera, not the forward facing F200 (the sample you're referencing is targeting the R200).
3. Ensure your system meets the requirements listed here: https://software.intel.com/en-us/realsense/devkit?

Also, if you have recently upgraded the SDK version on your dev system, did you ensure that your Unity project was also updated to use the most recent DLLs?

This information will help in troubleshooting the problem.

Thanks,
Bryan

0 Kudos
Bryan_B_Intel1
Employee
693 Views

Hi Shivang,

You'll also want to study the Unity examples provided in the RSSDK to see how the AcquireFrame method is called in a Unity script's Update() method, as opposed to a continuous thread as shown in the C#/XAML sample you've copied the code from.

-Bryan

0 Kudos
Reply