public ObjectRecognitionController(
     ILogger <ObjectRecognitionController> logger,
     DarknetService darknetService)
 {
     _logger         = logger;
     _darknetService = darknetService;
 }
示例#2
0
        public ClassFilterPage(DarknetService darknetService)
        {
            var section = new TableSection("Classes");

            NavigationPage.SetHasNavigationBar(this, false);
            foreach (var obj in darknetService.ClassCollection.classes)
            {
                var switchCell = new SwitchCell()
                {
                    Text = obj
                };
                switchCell.On         = darknetService.FilteredClasses.Contains(obj) ? true : false;
                switchCell.OnChanged += (o, e) =>
                {
                    if (e.Value)
                    {
                        darknetService.FilteredClasses.Add((o as SwitchCell).Text);
                    }
                    else
                    {
                        darknetService.FilteredClasses.Remove((o as SwitchCell).Text);
                    }
                };
                section.Add(switchCell);
            }
            Content = new TableView()
            {
                Root = new TableRoot {
                    section
                }
            };
        }
示例#3
0
        public PreviewPage(DarknetService darknetService)
        {
            // We do not want to use the smartphone horizontal
            CrossDeviceOrientation.Current.LockOrientation(Plugin.DeviceOrientation.Abstractions.DeviceOrientations.Landscape);
            // Remove NavigationBar
            NavigationPage.SetHasNavigationBar(this, false);
            ClassColors       = new Dictionary <string, Color>();
            UnderlyingService = darknetService;

            UnderlyingService.ClassCollectionAquired += (o, e) =>
            {
                // Whenever a new class collection is received, new colors will be calculated
                // This algorithm ensures that only high contrast colors are used for prediction drawing
                ClassColors.Clear();
                int    step_number = (int)Math.Ceiling((float)e.classes.Count / 6);
                double step_width  = 1.0 / (step_number - 1);
                int    count       = 0;

                for (int i = 0; i < step_number; i++)
                {
                    AddDictonaryEntry(e, 0, 1, (float)(i * step_width), count++);
                    AddDictonaryEntry(e, (float)(i * step_width), 0, 1, count++);
                    AddDictonaryEntry(e, (float)(i * step_width), 1, 0, count++);
                    AddDictonaryEntry(e, 1, 0, (float)(i * step_width), count++);
                    AddDictonaryEntry(e, 0, (float)(i * step_width), 1, count++);
                    AddDictonaryEntry(e, 1, (float)(i * step_width), 0, count++);
                }
            };

            Disappearing += (o, e) =>
            {
                darknetService.StopService();
                CrossDeviceOrientation.Current.UnlockOrientation();
            };
        }
 public TrainNetworkController(
     ILogger <ObjectRecognitionController> logger,
     IBackgroundJobClient backgroundJobs,
     DarknetService darknetService)
 {
     _logger         = logger;
     _backgroundJobs = backgroundJobs;
     _darknetService = darknetService;
 }
        public SettingsPage(DarknetService darknetService)
        {
            this.darknetService = darknetService;
            // First copy the current darknet settings
            // The new settings will be modified by the user in this page, and only written after the "Apply Changes" Button is pressed
            newSettings = new DarknetSettings(darknetService.Settings);
            NavigationPage.SetHasNavigationBar(this, false);

            var stackLayout = new StackLayout()
            {
                Children = { new Label()
                             {
                                 Text = "Darknet Settings", FontSize = 30, HorizontalTextAlignment = TextAlignment.Center
                             }, new SectionLine(), }
            };

            // Setup user interface for each property
            foreach (var property in newSettings)
            {
                SettingAttribute attr    = property.GetCustomAttribute(typeof(SettingAttribute)) as SettingAttribute;
                bool             Enabled = attr.Degree <= UserDegree;

                stackLayout.Children.Add(new SettingsInfoLabel(attr.Name));

                // Pattern matching, C# 7.0
                // Choose User Control depending on the property type and attribute
                switch (attr as object)
                {
                case SettingMinMax darkSlider:
                    var label = new Label()
                    {
                        HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Center, Text = property.GetValue(newSettings).ToString() + darkSlider.Unit
                    };
                    // The following cast will result in InvalidCastException:  int -> object -> double
                    // So the object needs to be casted to int before:          int -> object -> int -> double
                    var slider = new SettingSlider(property, label)
                    {
                        IsEnabled = Enabled
                    };
                    slider.Value         = (int)property.GetValue(newSettings);
                    slider.ValueChanged += (o, e) => {
                        var sl = o as SettingSlider;
                        // Same as above
                        sl.PropertyInfo.SetValue(newSettings, (int)(e.NewValue));
                        sl.Label.Text = sl.PropertyInfo.GetValue(newSettings).ToString() + sl.SettingAttribute.Unit;
                    };
                    stackLayout.Children.Add(label);
                    stackLayout.Children.Add(slider);
                    break;

                case SettingEnum darkEnum:
                    SettingPicker picker = new SettingPicker(property)
                    {
                        Title     = attr.Name,
                        IsEnabled = Enabled
                    };

                    picker.SelectedIndex         = Array.IndexOf(picker.Values, property.GetValue(newSettings));
                    picker.SelectedIndexChanged += (o, e) =>
                    {
                        var pick = o as SettingPicker;
                        pick.PropertyInfo.SetValue(newSettings, pick.Values.GetValue(pick.SelectedIndex));
                    };
                    stackLayout.Children.Add(picker);
                    break;

                case SettingString darkEntry:
                    var entry = new SettingEntry(property)
                    {
                        Text = property.GetValue(newSettings).ToString(), IsEnabled = Enabled
                    };
                    entry.TextChanged += (o, e) =>
                    {
                        var entr = o as SettingEntry;
                        if (e.NewTextValue.Length > entr.SettingAttribute.MaxChars)
                        {
                            entr.TextColor = Color.Red;
                            return;
                        }
                        foreach (char c in e.NewTextValue)
                        {
                            bool containsAtLeastOneChar = false;
                            foreach (char c2 in entr.SettingAttribute.AllowedChars)
                            {
                                if (c2 == c)
                                {
                                    containsAtLeastOneChar = true;
                                }
                            }
                            if (containsAtLeastOneChar == false)
                            {
                                entr.TextColor = Color.Red;
                                return;
                            }
                        }
                        entr.TextColor = Color.Black;
                        switch (Type.GetTypeCode(entr.PropertyInfo.PropertyType))
                        {
                        case TypeCode.Int32:
                            entr.PropertyInfo.SetValue(newSettings, int.Parse(e.NewTextValue));
                            break;

                        case TypeCode.String:
                            entr.PropertyInfo.SetValue(newSettings, e.NewTextValue);
                            break;
                        }
                    };
                    stackLayout.Children.Add(entry);
                    break;

                default:
                    switch (property.Name)
                    {
                    case "Resolution":
                        int index   = 0;
                        var resList = darknetService.CameraPreview.SupportedResolutions.ToList();
                        foreach (var obj in resList)
                        {
                            if (obj.Height == newSettings.Resolution.Height && obj.Width == newSettings.Resolution.Width)
                            {
                                index = resList.IndexOf(obj);
                                break;
                            }
                        }

                        Picker resolutionsPicker = new Picker()
                        {
                            Title = "Resolutions", ItemsSource = resList, SelectedIndex = index, IsEnabled = Enabled
                        };
                        resolutionsPicker.SelectedIndexChanged += (o, e) =>
                        {
                            newSettings.Resolution = darknetService.CameraPreview.SupportedResolutions.ToList()[resolutionsPicker.SelectedIndex];
                        };
                        stackLayout.Children.Add(resolutionsPicker);
                        break;
                    }
                    break;
                }
                stackLayout.Children.Add(new SectionLine());
            }

            Button applySettings = new Button()
            {
                Text = "Apply Settings"
            };

            applySettings.Pressed += (o, e) =>
            {
                darknetService.Settings = newSettings;
                Navigation.PopAsync();
            };
            stackLayout.Children.Add(applySettings);
            Content = new ScrollView()
            {
                Content = stackLayout
            };



            /// OUTDATED CODE

            //Label ThresholdLabel = new Label() { HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Center, Text = newSettings.Threshold.ToString() + " %" };
            //Slider thresholdSlider = new Slider(0, 100, newSettings.Threshold) { MaximumTrackColor = Color.Blue, MinimumTrackColor = Color.Blue };
            //thresholdSlider.ValueChanged += (o, e) =>
            //{
            //    ThresholdLabel.Text = e.NewValue.ToString() + " %";
            //    newSettings.Threshold = (int)e.NewValue;
            //};

            //Picker formatPicker = new Picker() { Title = "Image Format", ItemsSource = new List<string>() { "compressed", "uncompressed" }, SelectedIndex = newSettings.ImageFormat == ImageFormat.compressed ? 0 : 1, IsEnabled = false };
            //formatPicker.SelectedIndexChanged += (o, e) =>
            //{
            //    newSettings.ImageFormat = formatPicker.SelectedIndex == 0 ? ImageFormat.compressed : ImageFormat.uncompressed;
            //};

            //int index = 0;
            //var resList = darknetService.CameraPreview.SupportedResolutions.ToList();
            //foreach (var obj in resList)
            //{
            //    if (obj.Height == newSettings.Resolution.Height && obj.Width == newSettings.Resolution.Width)
            //    {
            //        index = resList.IndexOf(obj);
            //        break;
            //    }
            //}
            //Picker resolutionsPicker = new Picker() { Title = "Resolutions", ItemsSource = resList, SelectedIndex = index };
            //resolutionsPicker.SelectedIndexChanged += (o, e) =>
            //{
            //    newSettings.Resolution = darknetService.CameraPreview.SupportedResolutions.ToList()[resolutionsPicker.SelectedIndex];
            //};

            //Entry ipEntry = new Entry() { Text = newSettings.IpAddress.ToString(), HorizontalTextAlignment = TextAlignment.Start };
            //ipEntry.TextChanged += (o, e) =>
            //{
            //    newSettings.IpAddress = e.NewTextValue;
            //};

            //Entry portEntry = new Entry() { Text = newSettings.Port.ToString(), HorizontalTextAlignment = TextAlignment.Start };
            //portEntry.TextChanged += (o, e) =>
            //{
            //    int port = 0;
            //    if (int.TryParse(e.NewTextValue, out port))
            //    {
            //        newSettings.Port = port;
            //        portEntry.TextColor = Color.Black;
            //    }
            //    else
            //    {
            //        portEntry.TextColor = Color.Red;
            //    }

            //};

            //Button applySettings = new Button() { Text = "Apply Settings" };
            //applySettings.Pressed += (o, e) =>
            //{
            //    darknetService.Settings = newSettings;
            //    Navigation.PopAsync();
            //};
            //stackLayout.Children.Add(applySettings);
            //Content = new ScrollView()
            //{
            //    Content = new StackLayout
            //    {
            //        Children = {
            //        new Label() {Text = "Darknet Settings", FontSize = 30, HorizontalTextAlignment = TextAlignment.Center }, new SectionLine(),
            //        new SettingsInfoLabel("Prediction Threshold"), ThresholdLabel, thresholdSlider, new SectionLine(),
            //        new SettingsInfoLabel("Image Format"), formatPicker, new SectionLine(),
            //        new SettingsInfoLabel("Image Resolution"), resolutionsPicker, new SectionLine(),
            //        new SettingsInfoLabel("Host IP Address"), ipEntry, new SectionLine(),
            //        new SettingsInfoLabel("Host Port"), portEntry, new SectionLine(),
            //        applySettings
            //    }
            //    }
            //};
        }
        public Menu()
        {
            InitializeComponent();

            darknetService          = DarknetService.Create();
            darknetService.Settings = new DarknetSettings()
            {
                Resolution = darknetService.CameraPreview.SupportedResolutions.OrderBy((res) => res.Width * res.Height).First()
            };

            particleManager = new ParticleManager(absLayout)
            {
                Enabled = false
            };

            BackgroundImage = "background.jpg";

            startDetectionButton = new MenuButton("StartDetectionWhite.png")
            {
                IsEnabled = false
            };
            customizeFilterButton = new MenuButton("CustomizeFilterWhite.png")
            {
                IsEnabled = false
            };
            trainNetworkButton = new MenuButton("TrainNetworkWhite.png")
            {
                IsEnabled = false
            };
            optionsButton    = new MenuButton("OptionsWhite.png");
            disconnectButton = new MenuButton("DisconnectWhite.png");
            connectButton    = new MenuButton("ConnectWhite.png");
            starButton       = new ToggleButton("Stern.png", false);
            volumeButton     = new ToggleButton("LautstaerkeButton.png");
            volumeSlider     = new Slider(0, 1, 1)
            {
                ThumbColor = Color.AliceBlue
            };

            cyberDogImage = new Image()
            {
                Source = "cyber_dog.png", Scale = 0
            };
            AbsoluteLayout.SetLayoutBounds(cyberDogImage, new Rectangle(0, 0, 1, 1));
            AbsoluteLayout.SetLayoutFlags(cyberDogImage, AbsoluteLayoutFlags.All);
            absLayout.Children.Add(cyberDogImage);

            toolbarGrid = new Grid();
            toolbarGrid.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(1, GridUnitType.Auto)
            });
            toolbarGrid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Auto)
            });
            toolbarGrid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Auto)
            });
            toolbarGrid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Star)
            });

            toolbarGrid.Padding = new Thickness(0, 0, 0, 0);
            toolbarGrid.Margin  = new Thickness(0, 0, 0, 0);
            toolbarGrid.Children.Add(starButton, 0, 0);
            toolbarGrid.Children.Add(volumeButton, 1, 0);
            toolbarGrid.Children.Add(volumeSlider, 2, 0);

            grid.Children.Add(startDetectionButton, 0, 0);
            grid.Children.Add(optionsButton, 0, 1);
            grid.Children.Add(customizeFilterButton, 0, 2);
            grid.Children.Add(trainNetworkButton, 0, 3);
            grid.Children.Add(connectButton, 0, 4);
            grid.Children.Add(toolbarGrid, 0, 5);

            player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
            player.Load("mainMenu.mp3");
            player.Loop = true;
            player.Play();

            // Handler
            starButton.ToggleChanged += (o, e) =>
            {
                if (e)
                {
                    particleManager.Enabled = true;
                }
                else
                {
                    particleManager.Enabled = false;
                }
            };

            volumeButton.ToggleChanged += (o, e) =>
            {
                if (e)
                {
                    player.Play();
                }
                else
                {
                    player.Pause();
                }
            };


            volumeSlider.ValueChanged += (o, e) =>
            {
                player.Volume = volumeSlider.Value;
            };

            connectButton.ClickedAnimationFinished += (o, e) =>
            {
                connectButton.IsEnabled = false;
                if (darknetService.ConnectionState == DarknetService.ConnectionStatus.Disconnected)
                {
                    darknetService.Connect();
                }
            };

            disconnectButton.ClickedAnimationFinished += (o, e) =>
            {
                disconnectButton.IsEnabled = false;
                if (darknetService.ConnectionState == DarknetService.ConnectionStatus.Connected)
                {
                    darknetService.Disconnect();
                }
            };

            optionsButton.ClickedAnimationFinished         += (o, e) => Navigation.PushAsync(new SettingsPage(darknetService));
            startDetectionButton.ClickedAnimationFinished  += (o, e) => Navigation.PushAsync(new PreviewPage(darknetService));
            customizeFilterButton.ClickedAnimationFinished += (o, e) => Navigation.PushAsync(new ClassFilterPage(darknetService));

            darknetService.ClassCollectionAquired += (o, e) =>
            {
                if (e != null)
                {
                    Device.BeginInvokeOnMainThread(() => customizeFilterButton.IsEnabled = true);
                    if (darknetService.FilteredClasses == null)
                    {
                        darknetService.FilteredClasses = new List <string>();
                        foreach (var obj in e.classes)
                        {
                            darknetService.FilteredClasses.Add(obj);
                        }
                    }
                }
            };

            darknetService.Connected += (o, e) =>
            {
                darknetService.SendSettings();
                Device.BeginInvokeOnMainThread(() =>
                {
                    if (grid.Children.Contains(connectButton))
                    {
                        grid.Children.Remove(connectButton);
                        grid.Children.Add(disconnectButton, 0, 4);
                    }
                    disconnectButton.IsEnabled     = true;
                    startDetectionButton.IsEnabled = true;
                });
                cyberDogImage.ScaleTo(1, 250);
            };

            darknetService.Disconnected += (o, e) =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    if (grid.Children.Contains(disconnectButton))
                    {
                        grid.Children.Remove(disconnectButton);
                        grid.Children.Add(connectButton, 0, 4);
                    }
                    connectButton.IsEnabled        = true;
                    startDetectionButton.IsEnabled = false;
                });
                cyberDogImage.ScaleTo(0, 250);
            };
        }
        public MainMenu_NOT_USED()
        {
            const double fontSize = 30;
            Font         font     = Font.Default;
            Label        Welcome  = new Label()
            {
                Text = "HS Pforzheim\nObject Detection", FontSize = fontSize, HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Center
            };
            Button StartObjectDetection = new Button()
            {
                Text = "Start", FontSize = fontSize, BackgroundColor = Color.AliceBlue, Font = font, IsEnabled = false
            };
            Button Settings = new Button()
            {
                Text = "Change Settings", FontSize = fontSize, Font = font
            };
            Button Train = new Button()
            {
                Text = "Train Network", IsEnabled = false, FontSize = fontSize, Font = font
            };
            Button ShowClassList = new Button()
            {
                Text = "Customize Class Filter", FontSize = fontSize, Font = font, IsEnabled = false
            };
            Button Credits = new Button()
            {
                Text = "Credits", FontSize = fontSize, Font = font
            };
            Label Connected = new Label()
            {
                Text = "Disconnected", FontSize = fontSize, TextColor = Color.Red, HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Center
            };
            Button TryConnect = new Button()
            {
                Text = "Try to Connect", FontSize = fontSize, Font = font
            };

            Content = new StackLayout
            {
                Children =
                {
                    Welcome, StartObjectDetection, Settings, Train, ShowClassList, Credits, Connected, TryConnect
                }
            };

            darknetService          = DarknetService.Create();
            darknetService.Settings = new DarknetSettings()
            {
                Resolution = darknetService.CameraPreview.SupportedResolutions.OrderByDescending((res) => res.Width * res.Height).First()
            };

            TryConnect.Pressed += (o, e) =>
            {
                Connected.TextColor = Color.Orange;
                if (darknetService.ConnectionState == DarknetService.ConnectionStatus.Connected)
                {
                    Connected.Text = "Disconnecting...";
                    darknetService.Disconnect();
                }
                else
                {
                    Connected.Text = "Connecting...";
                    darknetService.Connect();
                }
            };

            darknetService.Connected += (o, e) =>
            {
                darknetService.SendSettings();
                Device.BeginInvokeOnMainThread(() =>
                {
                    TryConnect.Text = "Disconnect";
                    StartObjectDetection.IsEnabled = true;
                    Connected.TextColor            = Color.Green;
                    Connected.Text = "Connected";
                });
            };

            darknetService.Disconnected += (o, e) =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    TryConnect.Text = "Try To Connect";
                    StartObjectDetection.IsEnabled = false;
                    Connected.TextColor            = Color.Red;
                    Connected.Text = "Disconnected";
                });
            };

            darknetService.ClassCollectionAquired += (o, e) =>
            {
                if (e != null)
                {
                    Device.BeginInvokeOnMainThread(() => ShowClassList.IsEnabled = true);
                    if (darknetService.FilteredClasses == null)
                    {
                        darknetService.FilteredClasses = new List <string>();
                        foreach (var obj in e.classes)
                        {
                            darknetService.FilteredClasses.Add(obj);
                        }
                    }
                }
            };

            Settings.Pressed += (o, e) =>
            {
                Navigation.PushAsync(new SettingsPage(darknetService));
            };

            StartObjectDetection.Pressed += (o, e) =>
            {
                Navigation.PushAsync(new PreviewPage(darknetService));
            };

            ShowClassList.Pressed += (o, e) =>
            {
                Navigation.PushAsync(new ClassFilterPage(darknetService));
            };
        }