public void SelectTracker(TrackerType tracker_type)
 {
     if (tracker_type == TrackerType.Boosting)
     {
         monotracker = TrackerBoosting.create();
     }
     if (tracker_type == TrackerType.MIL)
     {
         monotracker = TrackerMIL.create();
     }
     if (tracker_type == TrackerType.KCF)
     {
         monotracker = TrackerKCF.create();
     }
     if (tracker_type == TrackerType.TLD)
     {
         monotracker = TrackerTLD.create();
     }
     if (tracker_type == TrackerType.MedianFlow)
     {
         monotracker = TrackerMedianFlow.create();
     }
     if (tracker_type == TrackerType.CSRT)
     {
         monotracker = TrackerCSRT.create();
     }
     if (tracker_type == TrackerType.MOSSE)
     {
         monotracker = TrackerMOSSE.create();
     }
 }
Exemplo n.º 2
0
        protected override Tracker CreateTracker()
        {
            TrackerMOSSE tracker = TrackerMOSSE.create();

            if (tracker == null)
            {
                throw new ArgumentNullException(nameof(tracker));
            }
            return(tracker);
        }
    // 帧刷新时调用
    private void Update()
    {
        // 对应初始化阶段
        if (TrackingFlag == 0)
        {
            if (VuforiaRenderer.Instance != null && VuforiaRenderer.Instance.VideoBackgroundTexture != null)
            {
                tempFrame = VuforiaRenderer.Instance.VideoBackgroundTexture;                                     // 取到一帧
                BackgroundPlaneBehaviour vuforiaBackgroundPlane = FindObjectOfType <BackgroundPlaneBehaviour>(); //获取屏幕
                if (vuforiaBackgroundPlane != null)
                {
                    vuforiaBackgroundPlane.GetComponent <Renderer>().material.mainTexture = tempFrame;
                }
            }
        }

        // 在OCR与API1返回正确的结果后进入
        if (ocrThreadFlag)
        {
            ocrThreadFlag = false;  // 线程结束收到,将线程标志位复原

            tipsText.text = "";

            // 获取关键词数量
            JsonData resultJson = JsonMapper.ToObject(OCR_RESULT);
            JsonData resultsNum = resultJson["result_num"];
            int      wordsNum   = int.Parse(resultsNum.ToString());

            if (wordsNum == 0)
            {
                tipsText.text = "对不起,您拍摄的页面没有识别到关键词\n请尝试重新拍摄";
            }
            else
            {
                GetResult(wordsNum);                   // 获取关键词位置

                cubeButton = new GameObject[wordsNum]; // 生成Collider Button的数组

                for (int c = 0; c < wordsNum; c++)     // 实例化Collider Button
                {
                    cubeButton[c]      = Instantiate(cubeButtonPrefab);
                    cubeButton[c].name = c.ToString();
                    cubeButton[c].tag  = "CubeButton";
                }

                // 初始跟踪框坐标与框
                xyLow          = new Point(points[0], points[1]);
                xyHigh         = new Point(points[2], points[3]);
                trackingWindow = new Rect2d(xyLow, xyHigh);

                // 创建各种图像资源的引用
                if (VuforiaRenderer.Instance != null && VuforiaRenderer.Instance.VideoBackgroundTexture != null)
                {
                    tempFrame        = VuforiaRenderer.Instance.VideoBackgroundTexture;                                        // 取帧Texture
                    trackingFrameOld = new Texture2D(tempFrame.width, tempFrame.height, TextureFormat.RGB24, false);           // 取帧Texture2D
                    Utils.textureToTexture2D(tempFrame, trackingFrameOld);
                    trackingFrame     = new Mat(trackingFrameOld.height, trackingFrameOld.width, CvType.CV_8UC3);              // 构造Mat
                    trackingFrameGray = new Mat(trackingFrameOld.height, trackingFrameOld.width, CvType.CV_8UC1);              // 灰度图Mat
                    trackingFrameNew  = new Texture2D(trackingFrame.cols(), trackingFrame.rows(), TextureFormat.RGB24, false); // 初始化新的Vuforia帧
                }

                TrackingFlag = 1;   // 将TrackingFlag置1,开启识别模式
            }
        }

        // OCR确定关键词信息和追踪区域后初始化Tracker时调用
        if (TrackingFlag == 1 && TimeFlag == TIMEFLAG)
        {
            if (VuforiaRenderer.Instance != null && VuforiaRenderer.Instance.VideoBackgroundTexture != null)
            {
                tempFrame = VuforiaRenderer.Instance.VideoBackgroundTexture;                // 取到一帧
                Utils.textureToTexture2D(tempFrame, trackingFrameOld);
                Utils.texture2DToMat(trackingFrameOld, trackingFrame, true, -1);            // 将帧转换成Mat处理
                Imgproc.cvtColor(trackingFrame, trackingFrameGray, Imgproc.COLOR_BGR2GRAY); // 转化成灰度图计算

                trackers = TrackerMOSSE.create();

                if (trackers.init(trackingFrameGray, trackingWindow))                                                // 初始化追踪
                {
                    DrawTrackingAndButton();                                                                         // 绘制关键词的追踪框和button

                    Utils.matToTexture2D(trackingFrame, trackingFrameNew, true, -1);                                 // 将Mat格式的frame转换成Texture2D

                    BackgroundPlaneBehaviour vuforiaBackgroundPlane = FindObjectOfType <BackgroundPlaneBehaviour>(); // 获取屏幕
                    if (vuforiaBackgroundPlane != null)
                    {
                        vuforiaBackgroundPlane.GetComponent <Renderer>().material.mainTexture = trackingFrameNew; // 绘制处理后的图像
                        TrackingFlag = 2;                                                                         // 更新flag到更新格式
                        TimeFlag     = 1;                                                                         // time标志位归一
                        startButton.SetActive(false);
                        restartButton.SetActive(true);
                    }
                }
            }
        }

        // Tracker初始化正常,追踪阶段(更新Tracker阶段)调用
        if (TrackingFlag == 2 && TimeFlag == TIMEFLAG)
        {
            if (VuforiaRenderer.Instance != null && VuforiaRenderer.Instance.VideoBackgroundTexture != null)
            {
                tempFrame = VuforiaRenderer.Instance.VideoBackgroundTexture;     // 取到一帧
                Utils.textureToTexture2D(tempFrame, trackingFrameOld);
                Utils.texture2DToMat(trackingFrameOld, trackingFrame, true, -1); // 将帧转换成Mat处理
                Imgproc.cvtColor(trackingFrame, trackingFrameGray, Imgproc.COLOR_BGR2GRAY);

                if (trackers.update(trackingFrameGray, trackingWindow))    // 更新追踪器
                {
                    TimeFlag = 1;

                    // 计算新的追踪框的位置
                    xyLow.x  = trackingWindow.x;
                    xyLow.y  = trackingWindow.y;
                    xyHigh.x = trackingWindow.x + trackingWindow.width;
                    xyHigh.y = trackingWindow.y + trackingWindow.height;

                    DrawTrackingAndButton();

                    if (Input.GetMouseButton(0))
                    {
                        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                        RaycastHit hit;

                        if (Physics.Raycast(ray, out hit))
                        {
                            GameObject clickedGameObject = hit.collider.gameObject;
                            if (clickedGameObject.tag == "CubeButton")
                            {
                                int clickedName = int.Parse(clickedGameObject.name);
                                InitializeGroundPlane(items[clickedName]);
                            }
                        }
                    }

                    Utils.matToTexture2D(trackingFrame, trackingFrameNew, true, -1);                                 // 将Mat格式的frame转换成Texture2D

                    BackgroundPlaneBehaviour vuforiaBackgroundPlane = FindObjectOfType <BackgroundPlaneBehaviour>(); // 获取屏幕
                    if (vuforiaBackgroundPlane != null)
                    {
                        vuforiaBackgroundPlane.GetComponent <Renderer>().material.mainTexture = trackingFrameNew;    // 绘制处理后的图像
                    }
                }
            }
        }

        // 每次time标志位没有到达TIMEFLAG值的时候正常刷新
        if (TimeFlag != TIMEFLAG && TrackingFlag != 3 && TrackingFlag != 0)
        {
            TimeFlag++;
            if (VuforiaRenderer.Instance != null && VuforiaRenderer.Instance.VideoBackgroundTexture != null)
            {
                tempFrame = VuforiaRenderer.Instance.VideoBackgroundTexture;     // 取到一帧
                Utils.textureToTexture2D(tempFrame, trackingFrameOld);
                Utils.texture2DToMat(trackingFrameOld, trackingFrame, true, -1); // 将帧转换成Mat处理

                DrawTrackingAndButton();

                if (Input.GetMouseButton(0))
                {
                    Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    RaycastHit hit;

                    if (Physics.Raycast(ray, out hit))
                    {
                        GameObject clickedGameObjectHere = hit.collider.gameObject;
                        if (clickedGameObjectHere.tag == "CubeButton")
                        {
                            int clickedName = int.Parse(clickedGameObjectHere.name);
                            InitializeGroundPlane(items[clickedName]);
                        }
                    }
                }

                Utils.matToTexture2D(trackingFrame, trackingFrameNew, true, -1);                                 // 将Mat格式的frame转换成Texture2D

                BackgroundPlaneBehaviour vuforiaBackgroundPlane = FindObjectOfType <BackgroundPlaneBehaviour>(); //获取屏幕
                if (vuforiaBackgroundPlane != null)
                {
                    vuforiaBackgroundPlane.GetComponent <Renderer>().material.mainTexture = trackingFrameNew;
                }
            }
        }

        // 用户确定查看关键词,进入AR阶段
        if (TrackingFlag == 3)
        {
            if (VuforiaRenderer.Instance != null && VuforiaRenderer.Instance.VideoBackgroundTexture != null)
            {
                tempFrame = VuforiaRenderer.Instance.VideoBackgroundTexture;                                     // 取到一帧
                BackgroundPlaneBehaviour vuforiaBackgroundPlane = FindObjectOfType <BackgroundPlaneBehaviour>(); //获取屏幕
                if (vuforiaBackgroundPlane != null)
                {
                    vuforiaBackgroundPlane.GetComponent <Renderer>().material.mainTexture = tempFrame;
                }
            }

            // 点击球的逻辑
            if (Input.GetMouseButton(0))
            {
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;

                if (Physics.Raycast(ray, out hit))
                {
                    clickedSphere = hit.collider.gameObject;
                    int sKey = clickedSphere.GetInstanceID();

                    Dictionary <int, string[]> sValue;

                    if (leaves.TryGetValue(sKey, out sValue))
                    {
                        GameObject fatherGameObject = clickedSphere.transform.parent.gameObject;
                        platformPoint = fatherGameObject.GetComponentsInChildren <MyPlatformPoint>()[0].gameObject.transform;

                        scrollView.gameObject.SetActive(true);

                        // 先销毁所有content下的button
                        OnMessageButtonClicked[] buttonsInContent = content.GetComponentsInChildren <OnMessageButtonClicked>();
                        for (int b = 0; b < buttonsInContent.Length; b++)
                        {
                            Destroy(buttonsInContent[b].gameObject);
                        }

                        int num = sValue.Count;

                        content.GetComponents <RectTransform>()[0].anchoredPosition3D = new Vector3(0, 0, 0);
                        content.GetComponents <RectTransform>()[0].sizeDelta          = new Vector2(0, 50 * num + 25);

                        textButton = new Button[num];
                        int h = 0;

                        foreach (int messageKey in sValue.Keys)
                        {
                            textButton[h] = Instantiate(textMessagePrefab);
                            textButton[h].transform.parent = content.transform;
                            textButton[h].GetComponentsInChildren <Text>()[0].text = sValue[messageKey][0];
                            textButton[h].GetComponents <RectTransform>()[0].anchoredPosition3D = new Vector3(0, h * (-50) - 10, 0);

                            OnMessageButtonClicked onMessageButton = textButton[h].GetComponents <OnMessageButtonClicked>()[0];
                            onMessageButton.messageValue = sValue[messageKey][1];

                            h++;
                        }
                        clickedFlag = true; // 将点击球标志位置为true
                    }
                }
            }

            // 如果点击中了一个球
            if (clickedFlag)
            {
                screenPos = Camera.main.WorldToScreenPoint(platformPoint.position);
                RectTransform rectTrans = scrollView.GetComponent <RectTransform>();
                rectTrans.position = screenPos;
            }

            // 手势控制
            if (Input.touchCount <= 0)
            {
                return;
            }

            // 手势控制
            //单点触摸控制旋转
            if (1 == Input.touchCount)
            {
                Touch   touch    = Input.GetTouch(0);
                Vector2 deltaPos = touch.deltaPosition;
                treeTransform.Rotate(Vector3.down * deltaPos.x / ROTATE_FECTOR, Space.World);
            }

            //多点触摸控制防缩
            Touch newTouch1 = Input.GetTouch(0);
            Touch newTouch2 = Input.GetTouch(1);

            if (newTouch2.phase == TouchPhase.Began)
            {
                oldTouch2 = newTouch2;
                oldTouch1 = newTouch1;
                return;
            }

            float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
            float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
            float offset      = newDistance - oldDistance;

            float scaleFactor = offset / SCALE_FECTOR;

            Vector3 localScale = treeTransform.localScale;
            Vector3 scale      = new Vector3(localScale.x + scaleFactor, localScale.y + scaleFactor, localScale.z + scaleFactor);

            if (scale.x > SCALE_MIN_FECTOR && scale.y > SCALE_MIN_FECTOR && scale.z > SCALE_MIN_FECTOR && scale.x < SCALE_MAX_FECTOR && scale.y < SCALE_MAX_FECTOR && scale.z < SCALE_MAX_FECTOR)
            {
                treeTransform.localScale = scale;
            }

            oldTouch1 = newTouch1;
            oldTouch2 = newTouch2;
        }
    }