Example #1
0
        public static int Main(string[] args)
        {
            string tmpCfg, tmpWeights, tmpNames;

            if (args.Length == 3)
            {
                tmpCfg     = args[0];
                tmpWeights = args[1];
                tmpNames   = args[2];
            }
            else
            {
                //Starting Darknet
                Console.WriteLine("*** YOLO CONFIGURATION ***");
                Console.WriteLine("Yolo CFG File: ");
                tmpCfg = "yolo-obj.cfg";
                Console.WriteLine("Yolo WEIGHTS File: ");
                tmpWeights = "yolo-obj_21000.weights";
                Console.WriteLine("Yolo NAMES File: ");
                tmpNames = "obj.names";
            }

            Console.WriteLine("Initializing YOLO  ...");
            Yolo.InitDnn(tmpCfg, tmpWeights, tmpNames, 0);

            //Starting MQTTServer
            MqttClient.StartClient();

            Task.Run(async() =>
            {
                while (true)
                {
                    MqttClient.ProcessEvents();
                }
            });

            //Handling console commands
            var command = Console.ReadLine()?.ToLower();

            while (command != "stop")
            {
                switch (command)
                {
                case "help":
                    PrintHelp();
                    break;

                default:
                    Console.WriteLine("Invalid command. Type 'help' for a list of available commands");
                    break;
                }
                command = Console.ReadLine()?.ToLower();
            }

            //Stopping the server
            Console.WriteLine("Stopping client ...");
            MqttClient.StopClient();
            return(0);
        }
Example #2
0
        public static void ProcessEvents()
        {
            if (Yolo.IsProbing)
            {
                //TODO REPLACE IMAGE
                var detectionResults = Yolo.DetectObjects(CameraHelper.CaptureFrame());
                var valuesToAdd      = new Dictionary <string, int>();
                foreach (var result in detectionResults)
                {
                    if (!valuesToAdd.ContainsKey(Yolo.GetClassnameById(result.ObjId)))
                    {
                        valuesToAdd.Add(Yolo.GetClassnameById(result.ObjId), 0);
                    }

                    valuesToAdd[Yolo.GetClassnameById(result.ObjId)]++;
                }

                var tmpLastValues = new Dictionary <string, int>();
                foreach (var entry in valuesToAdd)
                {
                    if (!_lastSeenBeers.ContainsKey(entry.Key))
                    {
                        if (!_drinks.ContainsKey(entry.Key))
                        {
                            _drinks.Add(entry.Key, 0);
                        }
                        _drinks[entry.Key] += entry.Value;
                        //Console.WriteLine("Added 1");
                    }
                    else if (_lastSeenBeers[entry.Key] > entry.Value)
                    {
                        if (!_drinks.ContainsKey(entry.Key))
                        {
                            _drinks.Add(entry.Key, 0);
                        }
                        _drinks[entry.Key] += entry.Value;
                        //Console.WriteLine("Added 2");
                    }

                    tmpLastValues.Add(entry.Key, entry.Value);

                    //Console.WriteLine("check routine done");

                    _lastSeenBeers = tmpLastValues;
                }
            }
            else if (Yolo.IsSearchingForBeer)
            {
                var camMat           = CameraHelper.CaptureFrame();
                var detectionResults = Yolo.DetectObjects(camMat);

                foreach (var result in detectionResults)
                {
                    var imageHCenter = (camMat.Cols / 2) - 110;

                    //if (result.X + (result.W / 2) - imageHCenter >= 0 && result.X + (result.W / 2) - imageHCenter < imageHCenter && result.X + (result.W / 2) - imageHCenter > result.W)
                    //{
                    //    if (IsArmCycling)
                    //    {
                    //        IsArmCycling = false;
                    //        //Sending stop cmd to arm
                    //        var yoloMessage = new MqttApplicationMessageBuilder()
                    //            .WithTopic($"yolo/cmd")
                    //            .WithPayload("stop")
                    //            .WithAtMostOnceQoS()
                    //            .Build();

                    //        MqttClientInstance.PublishAsync(yoloMessage);
                    //    }

                    //    //Moving left
                    //    var yoloCmd = new MqttApplicationMessageBuilder()
                    //        .WithTopic($"yolo/cmd")
                    //        .WithPayload("stepright")
                    //        .WithAtMostOnceQoS()
                    //        .Build();

                    //    MqttClientInstance.PublishAsync(yoloCmd);

                    //    Console.WriteLine("Move right");


                    //}
                    //else if (result.X + (result.W / 2) - imageHCenter <= 0 && result.X + (result.W / 2) - imageHCenter > (-imageHCenter) && result.X + (result.W / 2) - imageHCenter < (-result.W))
                    //{
                    //    if (IsArmCycling)
                    //    {
                    //        IsArmCycling = false;
                    //        //Sending stop cmd to arm
                    //        var yoloMessage = new MqttApplicationMessageBuilder()
                    //            .WithTopic($"yolo/cmd")
                    //            .WithPayload("stop")
                    //            .WithAtMostOnceQoS()
                    //            .Build();

                    //        MqttClientInstance.PublishAsync(yoloMessage);
                    //    }

                    //    //Moving right
                    //    var yoloCmd = new MqttApplicationMessageBuilder()
                    //        .WithTopic($"yolo/cmd")
                    //        .WithPayload("stepleft")
                    //        .WithAtMostOnceQoS()
                    //        .Build();

                    //    MqttClientInstance.PublishAsync(yoloCmd);

                    //    Console.WriteLine("Move left");


                    //}
                    if (result.X + (result.W / 2) - imageHCenter <= result.W && result.X + (result.W / 2) - imageHCenter >= (-result.W))
                    {
                        //On bottle
                        //if (IsArmCycling)
                        //{
                        //    IsArmCycling = false;
                        //    //Sending stop cmd to arm
                        //    var yoloMessage = new MqttApplicationMessageBuilder()
                        //        .WithTopic($"yolo/cmd")
                        //        .WithPayload("stop")
                        //        .WithAtMostOnceQoS()
                        //        .Build();

                        //    MqttClientInstance.PublishAsync(yoloMessage);
                        //}

                        //Pickup
                        //var yoloCmd = new MqttApplicationMessageBuilder()
                        //    .WithTopic($"yolo/cmd")
                        //    .WithPayload("pickup")
                        //    .WithAtMostOnceQoS()
                        //    .Build();

                        //MqttClientInstance.PublishAsync(yoloCmd);

                        //Yolo.IsSearchingForBeer = false;

                        var yoloCmd = new MqttApplicationMessageBuilder()
                                      .WithTopic($"yolo/cmd")
                                      .WithPayload("getbody")
                                      .WithAtMostOnceQoS()
                                      .Build();

                        MqttClientInstance.PublishAsync(yoloCmd);

                        Console.WriteLine("Found");
                    }
                }
            }
        }