Example #1
0
        public void OnRemoveColorDefinition(RemoveColorDefinition remove)
        {
            ColorDefinition delete = remove.Body;
            ColorSet        set    = _state.Colors.Find(delete.Compare);

            if (set != null)
            {
                ColorDefinition existing = set.Colors.Find(delete.CompareColor);

                if (existing != null)
                {
                    set.Colors.Remove(existing);
                    if (set.Colors.Count == 0)
                    {
                        _state.Colors.Remove(set);
                    }
                    _state.UpdateColorSetMap();
                    remove.ResponsePort.Post(DefaultDeleteResponseType.Instance);
                    SendNotification(_submgrPort, remove, Filter.ColorDefinitions.ToString());
                }
                else
                {
                    remove.ResponsePort.Post(
                        Fault.FromCodeSubcodeReason(
                            FaultCodes.Receiver,
                            DsspFaultCodes.UnknownEntry,
                            "Color Definition List does not contain an entry for: " + delete.Name
                            )
                        );
                }
            }
            else
            {
                remove.ResponsePort.Post(
                    Fault.FromCodeSubcodeReason(
                        FaultCodes.Receiver,
                        DsspFaultCodes.UnknownEntry,
                        "Color Set List does not contain an entry for: " + delete.Name
                        )
                    );
            }
        }
Example #2
0
        public IEnumerator <ITask> OnHttpPost(HttpPost httpPost)
        {
            HttpListenerContext context    = httpPost.Body.Context;
            NameValueCollection parameters = null;
            Fault fault = null;

            try
            {
                ReadFormData readForm = new ReadFormData(httpPost);
                _utilitiesPort.Post(readForm);

                yield return(Arbiter.Choice(
                                 readForm.ResultPort,
                                 delegate(NameValueCollection success)
                {
                    parameters = success;
                },
                                 delegate(Exception e)
                {
                    LogError("Error reading form data", e);
                    fault = Fault.FromException(e);
                }
                                 ));

                if (fault != null)
                {
                    yield break;
                }

                string name           = string.Empty;
                string deleteName     = null;
                string expandName     = null;
                int    left           = 0;
                int    top            = 0;
                int    width          = 0;
                int    height         = 0;
                int    deleteY        = 0;
                int    deleteCb       = 0;
                int    deleteCr       = 0;
                double threshold      = 1.0;
                int    minBlobSize    = 0;
                bool   showPartial    = false;
                bool   despeckle      = false;
                bool   updateSettings = false;
                bool   save           = false;

                foreach (string key in parameters.Keys)
                {
                    if (key.StartsWith("Delete."))
                    {
                        string[] segments = key.Split('.');

                        deleteName = segments[1];
                        deleteY    = int.Parse(segments[2]);
                        deleteCb   = int.Parse(segments[3]);
                        deleteCr   = int.Parse(segments[4]);
                    }
                    else if (key.StartsWith("ExpandY."))
                    {
                        string[] segments = key.Split('.');

                        expandName = segments[1];
                        deleteY    = int.Parse(segments[2]);
                        deleteCb   = int.Parse(segments[3]);
                        deleteCr   = int.Parse(segments[4]);
                    }
                    else
                    {
                        switch (key)
                        {
                        case "Save":
                            save = true;
                            break;

                        case "Threshold":
                            threshold = double.Parse(parameters[key]);
                            break;

                        case "ShowPartial":
                            showPartial = parameters[key] == "on";
                            break;

                        case "Despeckle":
                            despeckle = parameters[key] == "on";
                            break;

                        case "UpdateSettings":
                            updateSettings = true;
                            break;

                        case "MinBlobSize":
                            minBlobSize = int.Parse(parameters[key]);
                            break;

                        case "New.Left":
                            left = int.Parse(parameters[key]);
                            break;

                        case "New.Top":
                            top = int.Parse(parameters[key]);
                            break;

                        case "New.Width":
                            width = int.Parse(parameters[key]);
                            break;

                        case "New.Height":
                            height = int.Parse(parameters[key]);
                            break;

                        case "New.Name":
                            name = parameters[key].Trim();
                            break;

                        default:
                            break;
                        }
                    }
                }

                if (save)
                {
                    yield return(Arbiter.Choice(
                                     SaveState(_state.SmallCopy),
                                     EmptyHandler,
                                     EmptyHandler
                                     ));
                }
                else if (!string.IsNullOrEmpty(deleteName))
                {
                    ColorDefinition definition = new ColorDefinition(deleteName, deleteY, deleteCb, deleteCr);

                    RemoveColorDefinition remove = new RemoveColorDefinition(definition);

                    OnRemoveColorDefinition(remove);
                }
                else if (!string.IsNullOrEmpty(expandName))
                {
                    ColorDefinition definition = new ColorDefinition(expandName, deleteY, deleteCb, deleteCr);
                    ColorSet        set        = _state.Colors.Find(definition.Compare);
                    if (set != null)
                    {
                        ColorDefinition existing = set.Colors.Find(definition.CompareColor);

                        definition.SigmaCb = existing.SigmaCb;
                        definition.SigmaCr = existing.SigmaCr;
                        if (existing.SigmaY < 1)
                        {
                            definition.SigmaY = 2;
                        }
                        else
                        {
                            definition.SigmaY = 3 * existing.SigmaY / 2;
                        }
                    }

                    UpdateColorDefinition update = new  UpdateColorDefinition(definition);

                    OnUpdateColorDefinition(update);
                }
                else if (updateSettings)
                {
                    UpdateSettings update = new UpdateSettings(
                        new Settings(
                            threshold,
                            showPartial,
                            despeckle,
                            minBlobSize
                            )
                        );

                    OnUpdateSettings(update);
                }
                else
                {
                    webcam.QueryFrameResponse response = null;

                    yield return(Arbiter.Choice(
                                     _webcamPort.QueryFrame(),
                                     delegate(webcam.QueryFrameResponse success)
                    {
                        response = success;
                    },
                                     delegate(Fault failure)
                    {
                        LogError("Unable to query frame for update", failure);
                    }
                                     ));

                    if (response == null)
                    {
                        yield break;
                    }

                    int    right     = left + width;
                    int    bottom    = top + height;
                    byte[] data      = response.Frame;
                    int    stride    = data.Length / response.Size.Height;
                    int    rowOffset = left * 3;
                    int    offset;

                    int    r, g, b;
                    int[]  yProjection  = new int[256];
                    int[]  cbProjection = new int[256];
                    int[]  crProjection = new int[256];
                    int    count        = 0;
                    double yMean        = 0;
                    double cbMean       = 0;
                    double crMean       = 0;

                    for (int y = top; y < bottom; y++)
                    {
                        offset = rowOffset + y * stride;

                        for (int x = left; x < right; x++)
                        {
                            b = data[offset++];
                            g = data[offset++];
                            r = data[offset++];

                            ColorDefinition pixel = new ColorDefinition(r, g, b, "pixel");

                            yProjection[pixel.Y]++;
                            cbProjection[pixel.Cb]++;
                            crProjection[pixel.Cr]++;

                            count++;

                            yMean  += pixel.Y;
                            cbMean += pixel.Cb;
                            crMean += pixel.Cr;
                        }
                    }

                    if (count <= 16)
                    {
                        LogError("The area was too small to generalize a color");
                        yield break;
                    }

                    yMean  /= count;
                    cbMean /= count;
                    crMean /= count;

                    double ySigma  = CalculateStdDev(yMean, yProjection, count);
                    double cbSigma = CalculateStdDev(cbMean, cbProjection, count);
                    double crSigma = CalculateStdDev(crMean, crProjection, count);

                    ColorDefinition definition = new ColorDefinition(
                        name,
                        (int)Math.Round(yMean),
                        (int)Math.Round(cbMean),
                        (int)Math.Round(crMean)
                        );
                    definition.SigmaY  = (int)Math.Max(1, Math.Round(2 * ySigma));
                    definition.SigmaCb = (int)Math.Max(1, Math.Round(2 * cbSigma));
                    definition.SigmaCr = (int)Math.Max(1, Math.Round(2 * crSigma));

                    if (!string.IsNullOrEmpty(expandName))
                    {
                        definition.Name = expandName;
                        UpdateColorDefinition update = new UpdateColorDefinition(definition);
                        OnUpdateColorDefinition(update);
                    }
                    else
                    {
                        AddColorDefinition add = new AddColorDefinition(definition);

                        OnAddColorDefinition(add);
                    }
                }
            }
            finally
            {
                httpPost.ResponsePort.Post(new HttpResponseType(HttpStatusCode.OK, _state.SmallCopy, _transform));
            }
        }