Example #1
0
    protected virtual void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            exitScreen.SetActive(!exitScreen.activeInHierarchy);
        }

        if (Input.GetKeyDown(KeyCode.F5))
        {
            // save pos/rot
            SaveAutoPositionRotation();
        }

        if (Input.GetKeyDown(KeyCode.F9))
        {
            // load saved pos and rot and apply to controller transform
            LoadAutoPositionRotation();
        }

        if (Input.GetKeyDown(KeyCode.F10))
        {
            // move car in front of user vehicle
            ToggleNPCObstacleToUser();
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            var ui        = GetComponent <RectTransform>();
            var camView   = CameraPreview.GetComponent <RectTransform>();
            var extraView = ColorSegmentPreview.GetComponent <RectTransform>();

            int w, h;

            if (camView.offsetMax.y == 480)
            {
                // make it big
                camView.offsetMax = new Vector2(0, ui.sizeDelta.y * 2);
                camView.offsetMin = new Vector2(-ui.sizeDelta.x, 0);

                extraView.offsetMax = new Vector2(-ui.sizeDelta.x / 2, ui.sizeDelta.y);
                extraView.offsetMin = new Vector2(-ui.sizeDelta.x, 0);

                w = (int)camView.sizeDelta.x;
                h = (int)camView.sizeDelta.y;
            }
            else
            {
                // revert
                camView.offsetMax = new Vector2(0.0f, 480.0f);
                camView.offsetMin = new Vector2(-640.0f, 0.0f);

                extraView.offsetMax = new Vector2(-320.0f, 240.0f);
                extraView.offsetMin = new Vector2(-640.0f, 0.0f);

                w = 640;
                h = 480;
            }

            CameraPreview.renderTexture = new RenderTexture(w, h, 24, RenderTextureFormat.ARGB32);
            {
                var vs = CameraPreview?.renderCamera?.GetComponent <VideoToROS>();
                if (vs != null)
                {
                    vs.SwitchResolution(w, h);
                }
            }

            ColorSegmentPreview.renderTexture = new RenderTexture(w, h, 24, RenderTextureFormat.ARGBHalf);
            {
                var rendCam = ColorSegmentPreview?.renderCamera;
                if (rendCam != null)
                {
                    rendCam.targetTexture = ColorSegmentPreview.renderTexture;
                }
            }
        }

        CheckStateErrors();
    }
    void Visualize(List <Ros.Detection2D> objects, Camera cam, RenderTextureDisplayer camPreview, List <GameObject> boundingBoxes)
    {
        if (objects == null || cam == null || camPreview == null)
        {
            return;
        }

        if (!cam.enabled || !camPreview.gameObject.activeInHierarchy)
        {
            return;
        }

        foreach (GameObject bbox in boundingBoxes)
        {
            Destroy(bbox);
        }
        boundingBoxes.Clear();

        foreach (Ros.Detection2D obj in objects)
        {
            float x      = (float)obj.bbox.x;
            float y      = (float)obj.bbox.y;
            float width  = (float)obj.bbox.width;
            float height = (float)obj.bbox.height;

            if (previewWidth == -1 || previewHeight == -1)
            {
                previewWidth  = camPreview.GetComponent <RectTransform>().sizeDelta.x;
                previewHeight = camPreview.GetComponent <RectTransform>().sizeDelta.y;
            }

            x      = obj.bbox.x / cam.pixelWidth * previewWidth;
            y      = obj.bbox.y / cam.pixelHeight * previewHeight;
            width  = obj.bbox.width / cam.pixelWidth * previewWidth;
            height = obj.bbox.height / cam.pixelHeight * previewHeight;

            GameObject bbox = Instantiate(boundingBox, camPreview.transform);

            RectTransform rt = bbox.GetComponent <RectTransform>();
            rt.anchoredPosition = new Vector3(x, -y, 0);
            rt.sizeDelta        = new Vector3(width, height);
            rt.localScale       = Vector3.one;

            Image image = bbox.GetComponent <Image>();
            switch (obj.label)
            {
            case "car":
                image.color = new Color(0, 1, 0, 0.1f);      // Color.green
                break;

            case "pedestrian":
                image.color = new Color(1, 0.92f, 0.016f, 0.1f);      // Color.yellow
                break;

            case "bicycle":
                image.color = new Color(0, 1, 1, 0.1f);      // Color.cyan
                break;

            default:
                image.color = new Color(1, 0, 1, 0.1f);      // Color.magenta
                break;
            }

            bbox.SetActive(true);
            Destroy(bbox, Time.deltaTime);
            boundingBoxes.Add(bbox);
        }
    }