Exemplo n.º 1
0
        public override void Encode(DetectionResult result)
        {
            int tagChannel = m_NumChannels * m_CrntStackIndex;

            m_Grid.ClearChannels(tagChannel, m_NumChannels);

            for (int iTag = 0, nTag = result.DetectableTags.Count; iTag < nTag; iTag++)
            {
                string tag = result.DetectableTags[iTag];

                if (m_Settings.HasObservations(tag, out IList <int> obsIndices))
                {
                    if (result.TryGetItems(tag, out IList <DetectionResult.Item> items))
                    {
                        ShapeModifier modifier = m_ModifiersByTag[tag];

                        for (int iItem = 0, nItem = items.Count; iItem < nItem; iItem++)
                        {
                            var item    = items[iItem];
                            int channel = tagChannel;
                            modifier.Clear();

                            // First observation.
                            float value = item.Detectable.Observations.Evaluate(obsIndices[0]);

                            for (int iPoint = 0, nPoint = item.Points.Count; iPoint < nPoint; iPoint++)
                            {
                                Vector2Int pos = m_Grid.NormalizedToGridPos(item.Points[iPoint]);
                                m_Grid.Write(channel, pos, value);
                                modifier.AddPoint(pos);
                            }

                            modifier.Process(m_Grid, channel);
                            channel++;

                            // Additional observations.
                            if (obsIndices.Count > 1)
                            {
                                for (int iObs = 1, nObs = obsIndices.Count; iObs < nObs; iObs++)
                                {
                                    // The modifier already contains the required grid positions,
                                    // even if we haven't applied any modifications (ShapeModNone).
                                    modifier.Write(m_Grid, channel,
                                                   item.Detectable.Observations.Evaluate(obsIndices[iObs]));
                                    channel++;
                                }
                            }
                        }
                    }
                }
                else
                {
                    throw new UnityException("No observations found for " + tag);
                }

                tagChannel += obsIndices.Count;
            }

            IncrementStackIndex();
        }
Exemplo n.º 2
0
        public static IDictionary <string, ShapeModifier> CreateModifiers(
            IEncodingSettings settings, ChannelGrid grid)
        {
            var modifiers = new Dictionary <string, ShapeModifier>();

            foreach (string tag in settings.DetectableTags)
            {
                ShapeModifier modifier = settings.GetShapeModifierType(tag) switch
                {
                    ShapeModifierType.Dilation => new ShapeModDilation(),
                    ShapeModifierType.Downsampling => new ShapeModDownsampling(),
                    ShapeModifierType.OrthogonalFill => new ShapeModOrthogonalFill(),
                    ShapeModifierType.DiagonalFill => new ShapeModDiagonalFill(),
                    _ => new ShapeModNone(),
                };
                modifier.Initialize(grid);
                modifiers.Add(tag, modifier);
            }

            return(modifiers);
        }
Exemplo n.º 3
0
        public override void Encode(DetectionResult result)
        {
            int tagChannel = m_NumChannels * m_CrntStackIndex;

            m_Grid.ClearChannels(tagChannel, m_NumChannels);

            for (int iTag = 0, nTag = result.DetectableTags.Count; iTag < nTag; iTag++)
            {
                string tag    = result.DetectableTags[iTag];
                bool   hasObs = m_Settings.HasObservations(tag, out IList <int> obsIndices);

                if (result.TryGetItems(tag, out IList <DetectionResult.Item> items))
                {
                    ShapeModifier modifier = m_ModifiersByTag[tag];

                    for (int iItem = 0, nItem = items.Count; iItem < nItem; iItem++)
                    {
                        var item    = items[iItem];
                        int channel = tagChannel;
                        modifier.Clear();

                        // Distance.
                        for (int iPoint = 0, nPoint = item.Points.Count; iPoint < nPoint; iPoint++)
                        {
                            Vector3    p   = item.Points[iPoint];
                            Vector2Int pos = m_Grid.NormalizedToGridPos(p);

                            // Inverts z -> 0 (far) to 1 (near).
                            float norm = m_DistanceNormalization.Evaluate(p.z);
                            float prev = m_Grid.Read(channel, pos);
                            // Override if closer.
                            if (norm > prev)
                            {
                                m_Grid.Write(channel, pos, norm);
                                modifier.AddPoint(pos, p.z); // z: 0 (near) to 1 (far).
                            }
                            // else: ignore occluded.
                        }

                        modifier.Process(m_Grid, channel);
                        channel++;

                        // Additional observations.
                        if (hasObs)
                        {
                            for (int iObs = 0, nObs = obsIndices.Count; iObs < nObs; iObs++)
                            {
                                // The modifier already contains the required grid positions,
                                // even if we haven't applied any modifications (ShapeModNone).
                                modifier.Write(m_Grid, channel,
                                               item.Detectable.Observations.Evaluate(obsIndices[iObs]));
                                channel++;
                            }
                        }
                    }
                }

                tagChannel += hasObs ? obsIndices.Count + 1 : 1;
            }

            IncrementStackIndex();
        }