Пример #1
0
        //OnFrameMatAcquired == update pero para el HL builded
        /*20n versionskeleton */
#if NETFX_CORE && !DISABLE_HOLOLENSCAMSTREAM_API
        public void OnFrameMatAcquired(Mat bgraMat, Matrix4x4 projectionMatrix, Matrix4x4 cameraToWorldMatrix)
        {
            Imgproc.cvtColor(bgraMat, grayMat, Imgproc.COLOR_BGRA2GRAY);

            //Debug.Log("Entré al onframeaquired desde el tracker");

            //26n Debug.Log("si hay puntos " + p1.x.ToString("0.00") + ";" + p1.y.ToString("0.00")); //yep
            //textDebug.text = "p2:"+p2.x +","+ p2.y;  //never leave a textMesh inside a Hololen's Update

            //28n pm
            //if (p1 != null && p2 != null) {

            //    Imgproc.circle(bgraMat, p1, 5, new Scalar(255, 0, 0, 255), 2);

            //    Imgproc.circle(bgraMat, p2, 5, new Scalar(0, 255, 0, 255), 2);

            //}

            //Imgproc.rectangle(bgraMat, p1, p2, new Scalar(255, 255, 255, 255), 2, 1, 0);


            if (storedTouchPoint != null) //osea si el tipo es allguien
            {
                //28n selectedPointList.Add(storedTouchPoint);

                p1 = new Point(storedTouchPoint.x - 50, storedTouchPoint.y - 50);
                p2 = new Point(storedTouchPoint.x + 50, storedTouchPoint.y + 50);
                //#ToDo crete the ROI with two taps like MonoTrackingExample.cs

                selectedPointList.Add(p1);

                selectedPointList.Add(p2);

                //Imgproc.circle(bgraMat, p2, 6, new Scalar(0, 0, 0, 255), 2);

                using (MatOfPoint selectedPointMat = new MatOfPoint(selectedPointList.ToArray()))
                {
                    OpenCVForUnity.Rect region = Imgproc.boundingRect(selectedPointMat); //si se necesita al fin y al cabo
                    Debug.Log("region de ancho " + region.width);

                    SelectTracker(tracker_type);   //16n monotracker = TrackerKCF.create(); //8n
                    trackerInitialized = monotracker.init(grayMat, new Rect2d(region.x, region.y, region.width, region.height));
                }

                storedTouchPoint = null;
            }

            //21N
            //22nif (selectedPointList.Count != 0)
            //{
            //    //p1 = new Point(bgraMat.width() / 2, bgraMat.height() / 2);
            //    //p2 = new Point(bgraMat.width() * 2 / 3, bgraMat.height() * 2 / 3); //21n vamos a reiniciar los puntos para ver si es cosa de coordenada o de que solo pinta por un ratico, yepsi los pinta

            //    Debug.Log("si hay puntos "+ p1.x.ToString("0.00") +";"+ p1.y.ToString("0.00")); //yep
            //    //textDebug.text = "p2:"+p2.x +","+ p2.y;  //nunca maaaas mesh.text en un upate del HL

            //    Imgproc.circle(bgraMat, p1, 6, new Scalar(255, 0, 0, 255), 2);

            //    Imgproc.circle(bgraMat, p2, 6, new Scalar(0, 255, 0, 255), 2);

            //    Imgproc.rectangle(bgraMat, p1, p2, new Scalar(255, 255, 255, 255), 2, 1, 0);

            //    //pues debe ser que a este paso ya no hay punticos mijo!
            //}


            if (trackerInitialized)
            {
                Debug.Log("SI inicializo el tracking");
                bool updated = monotracker.update(grayMat, bbox);//8n //pero nunca le pasa la bbox ni las coordenadas, unicamente al momento de tracker.init
                if (bbox.width != 0 && bbox.height != 0)
                {
                    //Debug.Log("tracking por aca:" + bbox.x + ";" + bbox.y);
                    Imgproc.rectangle(bgraMat, bbox.tl(), bbox.br(), new Scalar(255, 255, 255, 255), 2, 1, 0);

                    previousBox = new Rect2d(bbox.x, bbox.y, bbox.width, bbox.height);

                    // ------ 3D LOCATION ----------- #Todo

                    //28n is like a task, take long time to response, then keep going with others
                    //ray = cameraInstance.ScreenPointToRay(new Vector3((float)(bbox.x + bbox.width/2), (float)(bbox.y + bbox.height/2), 0));
                    //Debug.Log("dime que no te trabas ahi porfa" + ray.ToString("F2"));


                    //if (Physics.Raycast(ray, out hit))
                    //{

                    //    box3DPosition = hit.transform.position;
                    //    print("I'm looking at " + box3DPosition);
                    //    Debug.Log("the box is in" + box3DPosition);
                    //}
                    //else
                    //    print("I'm looking at nothing!");
                }
                else
                {
                    if (previousBox != null)
                    {
                        Debug.Log("Se perdio en:" + previousBox.x + ";" + previousBox.y);
                        Imgproc.rectangle(bgraMat, previousBox.tl(), previousBox.br(), new Scalar(0, 0, 255, 255), 2, 1, 0); //8n
                    }
                }
            }



            //new HL
            UnityEngine.WSA.Application.InvokeOnAppThread(() => {
                if (!webCamTextureToMatHelper.IsPlaying())
                {
                    return;
                }

                Utils.fastMatToTexture2D(bgraMat, texture);
                bgraMat.Dispose();

                Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse;

                quad_renderer.sharedMaterial.SetMatrix("_WorldToCameraMatrix", worldToCameraMatrix);

                // Position the canvas object slightly in front
                // of the real world web camera.
                Vector3 position = cameraToWorldMatrix.GetColumn(3) - cameraToWorldMatrix.GetColumn(2);
                position        *= 1.2f;

                // Rotate the canvas object so that it faces the user.
                Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1));

                gameObject.transform.position = position;
                gameObject.transform.rotation = rotation;
            }, false);
        }
        // Update is called once per frame
        void Update()
        {
            /// Mouse interaction, not developed yet
#if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR)
            //Touch
            int touchCount = Input.touchCount;
            if (touchCount == 1)
            {
                Touch t = Input.GetTouch(0);
                if (t.phase == TouchPhase.Ended && !EventSystem.current.IsPointerOverGameObject(t.fingerId))
                {
                    storedTouchPoint = new Point(t.position.x, t.position.y);
                    //Debug.Log ("touch X " + t.position.x);
                    //Debug.Log ("touch Y " + t.position.y);
                }
            }
#else
            //Mouse
            if (Input.GetMouseButtonUp(0) && !EventSystem.current.IsPointerOverGameObject())
            {
                storedTouchPoint = new Point(Input.mousePosition.x, Input.mousePosition.y);
                //Debug.Log ("mouse X " + Input.mousePosition.x);
                //Debug.Log ("mouse Y " + Input.mousePosition.y);
            }
        #endif


            if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
            {
                Mat rgbaMat = webCamTextureToMatHelper.GetMat();
                Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);

                //Here starts the OpenCV script

                //onTouch para el 1er clic
                if (selectedPointList.Count == 1)
                {
                    if (storedTouchPoint != null)
                    {
                        ConvertScreenPointToTexturePoint(storedTouchPoint, storedTouchPoint, gameObject, rgbaMat.cols(), rgbaMat.rows());

                        OnTouch(rgbaMat, storedTouchPoint); //rgab o gray¡
                        Debug.Log("primer clic por " + storedTouchPoint.x + ";" + storedTouchPoint.y);
                        storedTouchPoint = null;
                    }
                }

                //error PlayerLoop called recursively! on iOS.reccomend WebCamTexture.
                if (selectedPointList.Count != 1)
                {
                    //onTouch para el 2do clic
                    if (storedTouchPoint != null)
                    {
                        ConvertScreenPointToTexturePoint(storedTouchPoint, storedTouchPoint, gameObject, rgbaMat.cols(), rgbaMat.rows());
                        OnTouch(rgbaMat, storedTouchPoint);
                        storedTouchPoint = null;
                    }

                    //si ya es 2, creeme otra trackinArea
                    if (selectedPointList.Count < 2)
                    {
                        foreach (var point in selectedPointList)
                        {
                            Imgproc.circle(rgbaMat, point, 6, new Scalar(0, 0, 255), 2);
                        }
                    }
                    else
                    {
                        //key line! DM
                        using (MatOfPoint selectedPointMat = new MatOfPoint(selectedPointList.ToArray()))
                        {
                            //TODO 13n cambiar essa region por varias regiones, que le pasara
                            OpenCVForUnity.Rect region = Imgproc.boundingRect(selectedPointMat); //si se necesita al fin y al cabo

                            SelectTracker(tracker_type);                                         //16n monotracker = TrackerKCF.create(); //8n
                            trackerInitialized = monotracker.init(grayMat, new Rect2d(region.x, region.y, region.width, region.height));
                        }
                        selectedPointList.Clear();
                    }


                    // aca ya no lo inicializa, sino que lo actualiza
                    if (trackerInitialized)
                    {
                        bool updated = monotracker.update(grayMat, bbox);//8n //pero nunca le pasa la bbox ni las coordenadas, unicamente al momento de tracker.init
                        if (bbox.width != 0 && bbox.height != 0)
                        {
                            Debug.Log("tracking por aca:" + bbox.x + ";" + bbox.y);
                            Imgproc.rectangle(rgbaMat, bbox.tl(), bbox.br(), new Scalar(255, 255, 255, 255), 2, 1, 0);
                            previousBox = new Rect2d(bbox.x, bbox.y, bbox.width, bbox.height);
                        }
                        else
                        {
                            Debug.Log("Se perdio en:" + previousBox.x + ";" + previousBox.y);
                            Imgproc.rectangle(rgbaMat, previousBox.tl(), previousBox.br(), new Scalar(255, 0, 0, 255), 2, 1, 0); //8n
                        }
                    }
                    //     bool updated = trackers.update (rgbaMat, objects);
                    //     Debug.Log ("updated " + updated);
                    //     if (!updated && bbox.width > 0)
                    //         OnResetTrackerButtonClick ();
                    //     }


                    if (selectedPointList.Count != 1)
                    {
                        //Imgproc.putText (rgbaMat, "Please touch the screen, and select tracking regions.", new Point (5, rgbaMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
                        if (fpsMonitor != null)
                        {
                            fpsMonitor.consoleText = "Touch the screen to select a new tracking region.";
                        }
                    }
                    else
                    {
                        //Imgproc.putText (rgbaMat, "Please select the end point of the new tracking region.", new Point (5, rgbaMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
                        if (fpsMonitor != null)
                        {
                            fpsMonitor.consoleText = "Please select the end point of the new tracking region.";
                        }
                    }

                    Utils.matToTexture2D(rgbaMat, texture, webCamTextureToMatHelper.GetBufferColors());

                    //Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
                }
            }
        }
        // Update is called once per frame
        void Update()
        {
            #if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR)
            //Touch
            int touchCount = Input.touchCount;
            if (touchCount == 1)
            {
                Touch t = Input.GetTouch(0);
                if (t.phase == TouchPhase.Ended && !EventSystem.current.IsPointerOverGameObject(t.fingerId))
                {
                    storedTouchPoint = new Point(t.position.x, t.position.y);
                    //Debug.Log ("touch X " + t.position.x);
                    //Debug.Log ("touch Y " + t.position.y);
                }
            }
            #else
            //Mouse
            if (Input.GetMouseButtonUp(0) && !EventSystem.current.IsPointerOverGameObject())
            {
                storedTouchPoint = new Point(Input.mousePosition.x, Input.mousePosition.y);
                //Debug.Log ("mouse X " + Input.mousePosition.x);
                //Debug.Log ("mouse Y " + Input.mousePosition.y);
            }
            #endif

            //onTouch para el 1er clic
            if (selectedPointList.Count == 1)
            {
                if (storedTouchPoint != null)
                {
                    ConvertScreenPointToTexturePoint(storedTouchPoint, storedTouchPoint, gameObject, rgbMat.cols(), rgbMat.rows());
                    OnTouch(rgbMat, storedTouchPoint);
                    storedTouchPoint = null;
                }
            }

            //Loop play
            if (capture.get(Videoio.CAP_PROP_POS_FRAMES) >= capture.get(Videoio.CAP_PROP_FRAME_COUNT))
            {
                capture.set(Videoio.CAP_PROP_POS_FRAMES, 0);
            }

            //error PlayerLoop called recursively! on iOS.reccomend WebCamTexture.
            if (selectedPointList.Count != 1 && capture.grab())
            {
                capture.retrieve(rgbMat, 0);
                Imgproc.cvtColor(rgbMat, rgbMat, Imgproc.COLOR_BGR2RGB);

                //onTouch para el 2do clic
                if (storedTouchPoint != null)
                {
                    ConvertScreenPointToTexturePoint(storedTouchPoint, storedTouchPoint, gameObject, rgbMat.cols(), rgbMat.rows());
                    OnTouch(rgbMat, storedTouchPoint);
                    storedTouchPoint = null;
                }

                //si ya es 2, creeme otra trackinArea
                if (selectedPointList.Count < 2)
                {
                    foreach (var point in selectedPointList)
                    {
                        Imgproc.circle(rgbMat, point, 6, new Scalar(0, 0, 255), 2);
                    }
                }
                else
                {
                    //key line! DM
                    using (MatOfPoint selectedPointMat = new MatOfPoint(selectedPointList.ToArray())) {
                        OpenCVForUnity.Rect region = Imgproc.boundingRect(selectedPointMat); //si se necesita al fin y al cabo                                                                                              //8n multitracker trackers.add (TrackerKCF.create (), rgbMat, new Rect2d (region.x, region.y, region.width, region.height)); //tocomment soon

                        SelectTracker(tracker_type);                                         //16n monotracker = TrackerKCF.create(); //8n
                        trackerInitialized = monotracker.init(rgbMat, new Rect2d(region.x, region.y, region.width, region.height));
                    }

                    selectedPointList.Clear();  //comentela pa que no resetee el init
                    //8n trackingColorList.Add (new Scalar (UnityEngine.Random.Range (0, 255), UnityEngine.Random.Range (0, 255), UnityEngine.Random.Range (0, 255))); //le pone color
                }


                // aca ya no lo inicializa, update los antesriores
                //trackers.update (rgbMat, objects);


                if (trackerInitialized)
                {
                    //bbox aca esta en 0s
                    bool updated = monotracker.update(rgbMat, bbox);//8n //pero nunca le pasa la bbox ni las coordenadas, unicamente al momento de tracker.init
                    if (bbox.width != 0 && bbox.height != 0)
                    {
                        Debug.Log("tracking por aca:" + bbox.x + ";" + bbox.y);
                        Imgproc.rectangle(rgbMat, bbox.tl(), bbox.br(), new Scalar(255, 255, 255, 255), 2, 1, 0);
                        previousBox = new Rect2d(bbox.x, bbox.y, bbox.width, bbox.height);
                    }
                    else
                    {
                        Debug.Log("Se perdio en:" + previousBox.x + ";" + previousBox.y);
                        Imgproc.rectangle(rgbMat, previousBox.tl(), previousBox.br(), new Scalar(255, 0, 0, 255), 2, 1, 0);


                        /*
                         * //16n intento de redimensionar la bbox // como hace CSRT
                         * Rect2d resized = new Rect2d(previousBox.tl().x, previousBox.tl().y, previousBox.width * 0.75f, previousBox.height * 0.75f );
                         *  Imgproc.rectangle( rgbMat, resized.tl(), resized.br(), new Scalar(255, 255, 0, 255), 2, 1, 0);
                         */
                    }
                    //if (!updated /*&& bbox.width > 0*/)
                    //{
                    //    OnResetTrackerButtonClick();
                    //    Debug.Log("creamos uno nuevo!!!!!!!!");
                    //}
                }


                //bool updated = trackers.update (rgbMat, objects);
                //                Debug.Log ("updated " + updated);
                //                if (!updated && objects.rows () > 0) {
                //                    OnResetTrackerButtonClick ();
                //                }


                Imgproc.rectangle(rgbMat, bbox.tl(), bbox.br(), new Scalar(255, 255, 255), 2, 1, 0); //8n

                if (selectedPointList.Count != 1)
                {
                    //Imgproc.putText (rgbMat, "Please touch the screen, and select tracking regions.", new Point (5, rgbMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
                    if (fpsMonitor != null)
                    {
                        fpsMonitor.consoleText = "Touch the screen to select tracking region.";
                    }
                }
                else
                {
                    //Imgproc.putText (rgbMat, "Please select the end point of the new tracking region.", new Point (5, rgbMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
                    if (fpsMonitor != null)
                    {
                        fpsMonitor.consoleText = "Please select the end point of the new tracking region.";
                    }
                }

                Utils.fastMatToTexture2D(rgbMat, texture);
            }
        }