private VirtualObjectBox InstanceNewSemanticObject(SemanticObject _obj)
        {
            Transform obj_inst = Instantiate(prefDetectedObject, _obj.Position, _obj.Rotation).transform;

            obj_inst.parent = tfFrameForObjects;
            VirtualObjectBox result = obj_inst.GetComponentInChildren <VirtualObjectBox>();

            result.InitializeSemanticObject(_obj);
            return(result);
        }
        public Color32 GetColorObject(VirtualObjectBox vob)
        {
            Color32 newColor = new Color32((byte)Random.Range(0f, 255f), (byte)Random.Range(0f, 255f), (byte)Random.Range(0f, 255f), 255);

            while (boxColors.ContainsKey(newColor))
            {
                newColor = new Color32((byte)Random.Range(0f, 255f), (byte)Random.Range(0f, 255f), (byte)Random.Range(0f, 255f), 255);
            }
            boxColors[newColor] = vob;
            return(newColor);
        }
        private VirtualObjectBox Matching(SemanticObject obj1, ICollection <VirtualObjectBox> listToCompare)
        {
            VirtualObjectBox match      = null;
            float            best_score = 999;

            foreach (VirtualObjectBox vob in listToCompare)
            {
                List <SemanticObject.Corner> order = YNN(vob.semanticObject.Corners, obj1.Corners);
                float score = CalculateMatchingScore(vob.semanticObject.Corners, order);

                if (((score < thresholdMatchSameSype && obj1.ObjectClass.Equals(vob.semanticObject.ObjectClass)) ||
                     (score < thresholdMatchDiffType && !obj1.ObjectClass.Equals(vob.semanticObject.ObjectClass))) && score < best_score)
                {
                    obj1.SetNewCorners(order);
                    match      = vob;
                    best_score = score;
                    //Debug.Log("Union: " + obj1.Id+ " con: " + vob.semanticObject.Id + ", por distancia: " + score);
                }//else { Debug.Log("NO Union: " + obj1.Id+ " con: " + vob.semanticObject.Id + ", por distancia: " + score); }
            }
            return(match);
        }
        private IEnumerator ProcessMsgs()
        {
            Rect          rect              = new Rect(0, 0, 640, 480);
            Texture2D     image             = new Texture2D(640, 480, TextureFormat.RGB24, false);
            RenderTexture renderTextureMask = new RenderTexture(640, 480, 24);

            while (Application.isPlaying)
            {
                if (processingQueue.Count > 0)
                {
                    DetectionArrayMsg _detections = processingQueue.Dequeue();

                    //Get view previous detections from bbCamera located in the origin
                    bbCamera.transform.position = _detections.origin.GetPositionUnity();
                    bbCamera.transform.rotation = _detections.origin.GetRotationUnity() * Quaternion.Euler(0f, 90f, 0f);



                    Dictionary <VirtualObjectBox, int> virtualObjectBoxInRange = new Dictionary <VirtualObjectBox, int>();
                    List <VirtualObjectBox>            visibleVirtualObjectBox = new List <VirtualObjectBox>();

                    //int n = 0;
                    bool newIteration = true;
                    while (newIteration)
                    {
                        newIteration           = false;
                        bbCamera.targetTexture = renderTextureMask;
                        bbCamera.Render();
                        RenderTexture.active = renderTextureMask;
                        image.ReadPixels(rect, 0, 0);
                        image.Apply();

                        var q = from x in image.GetPixels()
                                group x by x into g
                                let count = g.Count()
                                            orderby count descending
                                            select new { Value = g.Key, Count = count };

                        foreach (var xx in q)
                        {
                            if (boxColors.ContainsKey(xx.Value))
                            {
                                var vob = boxColors[xx.Value];
                                vob.gameObject.SetActive(false);
                                visibleVirtualObjectBox.Add(vob);
                                var distance = Vector3.Distance(vob.semanticObject.Position, bbCamera.transform.position);
                                if (deepRange.y >= distance && distance >= deepRange.x)
                                {
                                    virtualObjectBoxInRange.Add(vob, xx.Count);
                                }
                                newIteration = true;
                                //Debug.Log("Value: " + xx.Value + " Count: " + xx.Count);
                            }
                        }

                        bbCamera.targetTexture = null;
                        RenderTexture.active   = null; //Clean
                        //Destroy(renderTextureMask); //Free memory
                        if (q.Count() == 1)
                        {
                            break;
                        }
                        //n++;
                    }

                    foreach (VirtualObjectBox vob in visibleVirtualObjectBox)
                    {
                        vob.gameObject.SetActive(true);
                    }

                    List <VirtualObjectBox> detectedVirtualObjectBox = new List <VirtualObjectBox>();
                    foreach (DetectionMsg detection in _detections.detections)
                    {
                        SemanticObject virtualObject = new SemanticObject(detection.GetScores(),
                                                                          detection.GetCorners(),
                                                                          detection.occluded_corners);

                        //Check the type object is in the ontology
                        if (!OntologySystem.instance.CheckInteresObject(virtualObject.ObjectClass))
                        {
                            Log(virtualObject.ObjectClass + " - detected but it is not in the ontology", LogLevel.Error, true);
                            continue;
                        }

                        //Checks the distance to the object
                        var distance = Vector3.Distance(virtualObject.Position, bbCamera.transform.position);
                        if (distance < deepRange.x || distance > deepRange.y)
                        {
                            Log(virtualObject.ObjectClass + " - detected but it is not in deep range. Distance: " + distance, LogLevel.Normal, true);
                            continue;
                        }

                        //Insertion detection into the ontology
                        virtualObject = OntologySystem.instance.AddNewDetectedObject(virtualObject);

                        //Try to get a match
                        VirtualObjectBox match = Matching(virtualObject, virtualObjectBoxInRange.Keys);

                        //Match process
                        if (match != null)
                        {
                            match.NewDetection(virtualObject);
                            detectedVirtualObjectBox.Add(match);
                        }
                        else
                        {
                            Log("New object detected: " + virtualObject.ToString(), LogLevel.Developer);
                            m_objectDetected.Add(virtualObject);
                            VirtualObjectBox nvob = InstanceNewSemanticObject(virtualObject);
                            detectedVirtualObjectBox.Add(nvob);
                        }
                        nDetections++;
                    }

                    List <VirtualObjectBox> inRange = virtualObjectBoxInRange.Keys.ToList();
                    for (int i = 0; i < inRange.Count - 1; i++)
                    {
                        if (inRange[i] != null)
                        {
                            VirtualObjectBox match = null;
                            match = Matching(inRange[i].semanticObject, inRange.GetRange(i + 1, inRange.Count - i - 1));
                            if (match != null)
                            {
                                match.NewDetection(inRange[i].semanticObject);
                                inRange[i].RemoveVirtualBox();
                            }
                        }
                    }

                    detectedVirtualObjectBox.ForEach(dvob => virtualObjectBoxInRange.Remove(dvob));

                    foreach (KeyValuePair <VirtualObjectBox, int> o in virtualObjectBoxInRange)
                    {
                        if (o.Value > minPixelsMask)
                        {
                            o.Key.NewDetection(null);
                        }
                    }
                }

                yield return(null);
            }
        }