예제 #1
0
        private void ExObjectDetect()
        {
            this.MediaService.Capture();
            var configurationDetector = new ConfigurationDetector();
            var config = configurationDetector.Detect();

            using (var yoloWrapper = new YoloWrapper(config))
            {
                var items = yoloWrapper.Detect(@"C:\Users\csy18\OneDrive\바탕 화면\RomanticLabel\RomanticLabel\cap.png");
                foreach (var item in items)
                {
                    var rect = new Rect(item.X, item.Y, item.Width, item.Height);
                    BoundingBoxes.Add(new BoundingBox(rect, item.Type));
                }
            }
        }
예제 #2
0
        private void SetupYolo(List <YoloEntry> configs, YoloEntry.YoloSelection selection)
        {
            if (yoloWrapper != null)
            {
                yoloWrapper.Dispose();
                yoloWrapper = null;
            }
            var config = configs.Find(x => x.Selection == selection).Conf;

            if (config.IsValid)
            {
                configurationDetector = new ConfigurationDetector(config);
                config      = configurationDetector.Detect();
                yoloWrapper = new YoloWrapper(config);
            }
            else
            {
                throw new InvalidDataException("Configuration files missing!");
            }
        }
예제 #3
0
        private void BtnDetect_Click(object sender, EventArgs e)
        {
            var configurationDetector = new ConfigurationDetector();
            var config = configurationDetector.Detect();

            using (var yoloWrapper = new YoloWrapper(config))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    picBox.Image.Save(ms, ImageFormat.Png);
                    var items = yoloWrapper.Detect(ms.ToArray());
                    result.DataSource = items;
                }

                //items[0].Type -> "Person, Car, ..."
                //items[0].Confidence -> 0.0 (low) -> 1.0 (high)
                //items[0].X -> bounding box
                //items[0].Y -> bounding box
                //items[0].Width -> bounding box
                //items[0].Height -> bounding box
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            //The download takes some time depending on the internet connection.
            //var repository = new YoloPreTrainedDatasetRepository();
            //var task= repository.DownloadDatasetAsync("YOLOv3", ".");
            //task.Wait();

            var configurationDetector = new ConfigurationDetector();
            var config = configurationDetector.Detect();

            using (var yoloWrapper = new YoloWrapper(config))
            {
                var items = yoloWrapper.Detect(@"IMG_2072.JPG");
                //items[0].Type -> "Person, Car, ..."
                //items[0].Confidence -> 0.0 (low) -> 1.0 (high)
                //items[0].X -> bounding box
                //items[0].Y -> bounding box
                //items[0].Width -> bounding box
                //items[0].Height -> bounding box
            }
            Console.ReadKey();
        }
예제 #5
0
        private void findingThingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var configurationDetector = new ConfigurationDetector();
            var config = configurationDetector.Detect();

            using (var yoloWraper = new YoloWrapper(config))
            {
                var items = yoloWraper.Detect(ImagetoByte(pictureBox1.Image));
                if (items.Count() > 0)
                {
                    foreach (YoloItem item in items)
                    {
                        Bitmap    bitmap    = new Bitmap(pictureBox1.Image);
                        Rectangle rectangle = new Rectangle(item.X, item.Y, item.Width, item.Height);
                        ImageInput = new Image <Bgr, byte>(bitmap);
                        ImageInput.Draw(rectangle, new Bgr(0, 255, 0), 3);
                        ImageInput.Draw($"{item.Type} ({string.Format( "{0:0.0}",item.Confidence)})",
                                        new Point(item.X - 3, item.Y - 3), Emgu.CV.CvEnum.FontFace.HersheyComplex, 0.5,
                                        new Bgr(Color.DarkBlue));
                        pictureBox1.Image = ImageInput.Bitmap;
                    }
                }
            }
        }
예제 #6
0
        public Classifier()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;

            //initialize components
            List <SizeInfo> sizeList = new List <SizeInfo>();

            sizeList.Add(new SizeInfo {
                Name = "Large", Value = "L"
            });
            sizeList.Add(new SizeInfo {
                Name = "Medium", Value = "M"
            });
            sizeList.Add(new SizeInfo {
                Name = "Small", Value = "S"
            });

            carSizeList.ItemsSource       = sizeList;
            carSizeList.DisplayMemberPath = "Name";
            carSizeList.SelectedValuePath = "Value";
            carSizeList.SelectedIndex     = 0;

            //initialize classifier
            try
            {
                var configurationDetector = new ConfigurationDetector();
                var config = configurationDetector.Detect();
                wrapper = new YoloWrapper(config);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                Close();
            }
        }
예제 #7
0
 public Form1()
 {
     InitializeComponent();
     configurationDetector = new ConfigurationDetector();
     config = configurationDetector.Detect();
 }
예제 #8
0
 public void Setup()
 {
     Configuration = new ConfigurationDetector();
     Wrapper       = new YoloWrapper(Configuration.Detect());
 }
예제 #9
0
        private void btnDetect_Click_1(object sender, EventArgs e)
        {
            //Проверка типа файла для распознавания
            if (cart == true)  //Для изображений
            {
                var configurationDetector = new ConfigurationDetector();
                var config = configurationDetector.Detect();
                using (var yoloWrapper = new YoloWrapper(config))
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        //Конвертация цветного изображения в черно-белое
                        var picWB = pic.Image;
                        picWB.Save(@"temp.jpg");
                        Mat            src       = new Mat(@"temp.jpg", ImreadModes.Grayscale);
                        ImageConverter converter = new ImageConverter();
                        byte[]         WbTemp    = (byte[])converter.ConvertTo(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src), typeof(byte[]));
                        using (FileStream fstream = new FileStream(@"tempWB.jpg", FileMode.Create))
                        {
                            fstream.Write(WbTemp, 0, WbTemp.Length);
                        }
                        //Без использования using файл tempWB.jpg занят процессом и не может быть удален
                        using (var WB = Image.FromFile(@"tempWB.jpg"))
                        {
                            WB.Save(ms, ImageFormat.Png);
                            var items = yoloWrapper.Detect(ms.ToArray());
                            yoloItemBindingSource.DataSource = items;
                            foreach (var item in items)
                            {
                                //Запись в лог
                                using (StreamWriter w = File.AppendText("log.txt"))
                                {
                                }
                                void Log(string logMessage, TextWriter w)
                                {
                                    w.Write("\r\nLog Entry : ");
                                    w.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}");
                                    w.WriteLine("  :");
                                    w.WriteLine($"  :{logMessage}");
                                    w.WriteLine("-------------------------------");
                                }

                                //Определение параметров для отображения сцепки и рисование рамки
                                x      = (float)item.X;
                                y      = (float)item.Y;
                                width  = (float)item.Width;
                                height = (float)item.Height;
                                if ((double)item.Confidence > 0.85)
                                {
                                    if (item.Type == "coup1")
                                    {
                                        Graphics graph = Graphics.FromImage(pic.Image);
                                        Pen      pen   = new Pen(Color.Blue, 3f);
                                        graph.DrawRectangle(pen, x, y, width, height);
                                        pic.Image = pic.Image;
                                        using (StreamWriter w = File.AppendText("log.txt"))
                                        {
                                            Log($"Обнаружена полусцепка: {NameF} | X: {x} | Y: {y} | width: {width} | height: {height} | Confidence: {item.Confidence}", w);
                                        }
                                    }
                                    if (item.Type == "coup2")
                                    {
                                        Graphics graph = Graphics.FromImage(pic.Image);
                                        Pen      pen   = new Pen(Color.Red, 3f);
                                        graph.DrawRectangle(pen, x, y, width, height);
                                        pic.Image = pic.Image;
                                        using (StreamWriter w = File.AppendText("log.txt"))
                                        {
                                            Log($"Обнаружена сцепка: {NameF} | X: {x} | Y: {y} | width: {width} | height: {height} | Confidence: {item.Confidence}", w);
                                        }

                                        /*CoupJSON image = new CoupJSON();
                                         * image.ImageName = ($"{NameF}");
                                         * CoupJSON TL = new CoupJSON();
                                         * TL.TopLeft = new List<int> { $"{ x }", $"{ y }" };
                                         */
                                    }

                                    Directory.CreateDirectory(@"JSON");
                                    var CoupJSON    = GetCoupJSON();
                                    var jsonToWrite = JsonConvert.SerializeObject(CoupJSON, Formatting.Indented);
                                    using (var writer = new StreamWriter(@"JSON/" + NameF + ".json"))
                                    {
                                        writer.Write(jsonToWrite);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else //Для видео
            {
                var capture   = new VideoCapture(PathF);
                int sleepTime = (int)Math.Round(1000 / capture.Fps);
                int count     = 0;
                Mat image     = new Mat();
                while (true)
                {
                    count++;
                    capture.Read(image);
                    if (image.Empty())
                    {
                        break;
                    }
                    capture.Read(image);

                    if (bStop == true)   //Действие на кнопку Стоп
                    {
                        bStop = false;
                        break;
                    }
                    //Сохранение временных файлов, начиная с 1 кадра
                    using (FileStream fstream = new FileStream(@"temp/temp" + count + ".jpg", FileMode.Create))
                    {
                        ImageConverter converter = new ImageConverter();
                        byte[]         bTemp     = (byte[])converter.ConvertTo(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image), typeof(byte[]));
                        fstream.Write(bTemp, 0, bTemp.Length);
                    }
                    pic.Image = Image.FromFile(@"temp/temp" + count + ".jpg");  //Передача кадра в pictureBox
                    var configurationDetector = new ConfigurationDetector();
                    var config = configurationDetector.Detect();
                    using (var yoloWrapper = new YoloWrapper(config))
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            pic.Image.Save(ms, ImageFormat.Png);    //Загрузка кадра из pictureBox в Yolo
                            var items = yoloWrapper.Detect(ms.ToArray());
                            yoloItemBindingSource.DataSource = items;
                            foreach (var item in items)
                            {
                                var x      = (float)item.X;
                                var y      = (float)item.Y;
                                var width  = (float)item.Width;
                                var height = (float)item.Height;
                                if ((double)item.Confidence > 0.85)
                                {
                                    if (item.Type == "coup1")
                                    {
                                        Graphics graph = Graphics.FromImage(pic.Image);
                                        Pen      pen   = new Pen(Color.Blue, 3f);
                                        graph.DrawRectangle(pen, x, y, width, height);
                                        pic.Image = pic.Image;
                                    }
                                    if (item.Type == "coup2")
                                    {
                                        Graphics graph = Graphics.FromImage(pic.Image);
                                        Pen      pen   = new Pen(Color.Red, 3f);
                                        graph.DrawRectangle(pen, x, y, width, height);
                                        pic.Image = pic.Image;
                                    }
                                }
                            }
                        }
                    }
                    Cv2.WaitKey(sleepTime);
                }
            }
        }
        private void TestVideoDetection()
        {
            Task.Run(() =>
            {
                // YOLO setting
                //int yoloWidth = 1920, yoloHeight = 1129;
                int yoloWidth = 175, yoloHeight = 102;
                //int yoloWidth = 1000, yoloHeight = 588;
                var configurationDetector = new ConfigurationDetector();
                var config      = configurationDetector.Detect();
                var yoloWrapper = new YoloWrapper(config);

                // OpenCV & WPF setting
                VideoCapture videocapture;
                Mat image          = new Mat();
                WriteableBitmap wb = new WriteableBitmap(yoloWidth, yoloHeight, 96, 96, System.Windows.Media.PixelFormats.Bgr24, null);

                byte[] imageInBytes = new byte[(int)(yoloWidth * yoloHeight * image.Channels())];

                // Read a video file and run object detection over it!
                using (videocapture = new VideoCapture("E:\\WPF Projects\\Automotive_Drones_Analysis_Tool\\Daten_automatisches_Fahren\\DJI_0137.MP4"))
                {
                    for (int i = 0; i < videocapture.FrameCount; i++)
                    {
                        using (Mat imageOriginal = new Mat())
                        {
                            // read a single frame and convert the frame into a byte array
                            videocapture.Read(imageOriginal);
                            image        = imageOriginal.Resize(new OpenCvSharp.Size(yoloWidth, yoloHeight));
                            imageInBytes = image.ToBytes();

                            // conduct object detection and display the result
                            var items = yoloWrapper.Detect(imageInBytes);
                            // We use the image to detect the objects in a very small size - then we draw them onto the
                            // uiImage and scale it up!
                            var uiImage = imageOriginal.Resize(new OpenCvSharp.Size(yoloWidth * 10, yoloHeight * 10));

                            foreach (var item in items)
                            {
                                var x      = item.X * 10;
                                var y      = item.Y * 10;
                                var width  = item.Width * 10;
                                var height = item.Height * 10;
                                var type   = item.Type; // class name of the object

                                // draw a bounding box for the detected object
                                // you can set different colors for different classes
                                Cv2.Rectangle(uiImage, new OpenCvSharp.Rect(x, y, width, height), Scalar.Green, 3);
                            }

                            // display the detection result
                            Application.Current?.Dispatcher?.Invoke(() =>
                            {
                                videoViewer.Source = BitmapHelper.BitmapToBitmapSource(BitmapConverter.ToBitmap(uiImage));
                            });
                        }
                        i++;
                    }
                }
            });
        }