private void initOpenCV()
    {
        int camWidth = 0, camHeight = 0;
        int tracker = (int)trackerTypes;
        int cascade = (int)cascadeTypes;


        int result = OpenCVInterop.Init(ref camWidth, ref camHeight, ref tracker, ref cascade);

        // Run the OpenV Init script, and log the result

        if (result < 0)
        {
            if (result == -1)
            {
                Debug.LogWarningFormat("[{0}] Failed to find cascades definition.", GetType());
            }
            else if (result == -2)
            {
                Debug.LogWarningFormat("[{0}] Failed to open camera stream.", GetType());
            }
            else if (result == -3)
            {
                Debug.LogWarningFormat("[{0}] No Bodies Detected.", GetType());
            }
            else if (result == -4)
            {
                Debug.LogWarningFormat("[{0}] Tracking Error.", GetType());
            }
            return;
        }

        // Prepare all variables and arrays for OpenCV
        CameraResolution            = new Vector2(camWidth, camHeight);
        _bodies                     = new CvRectangle[_maxTrackCount];
        _tracking                   = new CvRectangle[_maxTrackCount];
        patientBody                 = new CvRectangle();
        NormalizedBodyPositions     = new List <Vector2>();
        NormalizedTrackingPositions = new List <Vector2>();
        OpenCVInterop.SetScale(DetectionDownScale);
        DetectBodies();
        frameRate = 0;
        _ready    = true;
    }
    private void DetectBodies()
    {
        // Unsafe codeblock to retrieve data from OpenCV
        int detectedBodyCount = 0;

        unsafe
        {
            fixed(CvRectangle *outBodies = _bodies)
            {
                OpenCVInterop.Detect(outBodies, _maxTrackCount, ref detectedBodyCount);
            }
        }

        // Record the Normalized Tracking Positions
        NormalizedBodyPositions.Clear();
        for (int i = 0; i < detectedBodyCount; i++)
        {
            NormalizedBodyPositions.Add(new Vector2((_bodies[i].X * DetectionDownScale) / CameraResolution.x, 1f - ((_bodies[i].Y * DetectionDownScale) / CameraResolution.y)));
        }
        patientBody = _tracking[0];
    }
    private void PatientTracking()
    {
        // Unsafe codeblock to retrieve data from OpenCV
        int detectedTrackingCount = 0;

        unsafe
        {
            fixed(CvRectangle *outTracking = _tracking)
            {
                OpenCVInterop.Track(outTracking, _maxTrackCount, ref detectedTrackingCount);
            }
        }

        // Record the Normalized Tracking Positions
        NormalizedTrackingPositions.Clear();
        for (int i = 0; i < detectedTrackingCount; i++)
        {
            NormalizedTrackingPositions.Add(new Vector2((_tracking[i].X * DetectionDownScale) / CameraResolution.x, 1f - ((_tracking[i].Y * DetectionDownScale) / CameraResolution.y)));
        }

        patientBody = _tracking[0];
        // Log current patient position for debugging
        Debug.Log("Patient At: (x=" + _tracking[0].X + " y=" + _tracking[0].Y + " width=" + _tracking[0].Width + " height=" + _tracking[0].Height + ")");
    }