コード例 #1
0
 public void DetectedObject(DetectionArrayMsg _detections, string _ip)
 {
     processingQueue.Enqueue(_detections);
 }
コード例 #2
0
        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);
            }
        }