Пример #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            using (var ms = new MemoryStream())
            {
                pictureBox1.Image.Save(ms, ImageFormat.Png);
                var items = yoloWrapper.Detect(ms.ToArray());
                yoloItemBindingSource.DataSource = items;

                if (items != null && items.Any())
                {
                    // draw rectangles
                    var image       = pictureBox1.Image;
                    var graphic     = Graphics.FromImage(image);
                    var rectangleFs = items.Select(x => new RectangleF(x.X, x.Y, x.Width, x.Height)).ToArray();
                    graphic.DrawRectangles(new Pen(Brushes.Red, 5), rectangleFs);

                    pictureBox1.Image = image;
                    ThreadHelper.SetText(this, lbDetectTime, $"{items.Count()} result");
                }
                else
                {
                    ThreadHelper.SetText(this, lbDetectTime, $"No result");
                }
            }
        }
Пример #2
0
        private List <YoloItem> Detect(bool memoryTransfer = true)
        {
            if (_yoloWrapper == null)
            {
                return(null);
            }

            var imageInfo = GetCurrentImage();
            var imageData = File.ReadAllBytes(imageInfo.Path);

            var sw = new Stopwatch();

            sw.Start();
            List <YoloItem> items;

            if (memoryTransfer)
            {
                items = _yoloWrapper.Detect(imageData).ToList();
            }
            else
            {
                items = _yoloWrapper.Detect(imageInfo.Path).ToList();
            }

            sw.Stop();
            groupBoxResult.Text = $"Result [ processed in {sw.Elapsed.TotalMilliseconds:0} ms ]";

            return(items);
        }
Пример #3
0
        static void Main(string[] args)
        {
            YoloWrapper yolodetector;

            try
            {
                yolodetector = new YoloWrapper("yolov5x.cfg", "x_best.weights", 0);
            }
            catch (Exception)
            {
                throw;
            }

            byte[]   picBytes    = GetPictureData("5_YXK54X.jpg");
            bbox_t[] result_list = yolodetector.Detect(picBytes);
            foreach (var result in result_list)
            {
                Console.WriteLine($" hair deteteted -> x:{result.x}, y:{result.y}");
            }

            bbox_t[] result_list_2 = yolodetector.Detect("5_YXK54X.jpg");
            foreach (var result in result_list_2)
            {
                Console.WriteLine($" hair deteteted -> x:{result.x}, y:{result.y}");
            }
            Console.WriteLine("Hello World!");
        }
Пример #4
0
    public void LoadDummyImageForObjectDetection()
    {
        var items     = yolo.Detect(ImageFilename);
        var yoloItems = items as YoloItem[] ?? items.ToArray();

        Assert.That(yoloItems, Is.Not.Null.Or.InnerException);
        foreach (var item in yoloItems)
        {
            Console.WriteLine("Found " + item.Type + " " + item.X + "," + item.Y);
        }
    }
        private string Detection(byte[] imagebyte)
        {
            UIUpdate("In detect");
            var configurationDetector = new ConfigurationDetector();
            var config = configurationDetector.Detect();

            YoloWrapper            yoloWrapper = new YoloWrapper(config);       // 使用YoloWrapper物件(Alturos裡的主要辨識程式)
            IEnumerable <YoloItem> items       = yoloWrapper.Detect(imagebyte); // 呼叫偵測函式,結果儲存於items裡
            string message = string.Empty;

            foreach (YoloItem item in items)
            {
                //if (item.Type == "TroopsHeadquarters")
                //{
                //    message += "1,";
                //}
                //else message += item.Type + ',';

                message += (item.Type == "TroopsHeadquarters" ? "1" : item.Type) + ',' +
                           item.X + ',' +
                           item.Y + ',' +
                           item.Width + ',' +
                           item.Height + ',' +
                           item.Confidence + ';';   // 將目標的種類、座標及信賴值存成字串,以方便傳輸
            }

            return(message);
        }
Пример #6
0
        public YoloItem AnalyzeStream(MemoryStream ms)
        {
            IEnumerable <Alturos.Yolo.Model.YoloItem> items = yoloWrapper.Detect(ms.ToArray());

            if (items.Count() > 0)
            {
                foreach (var item in items)
                {
                    if (item.Confidence > _MIN_CONFIDENCE && objects.Contains <string>(item.Type))
                    {
                        return(item);
                    }
                }
            }
            return(null);
        }
Пример #7
0
        public override Task <Result> GetDetections(Payload request, ServerCallContext context)
        {
            using var yoloWrapper = new YoloWrapper(
                      "C:\\Development\\PointerAppAlyn\\YOLOv3-Object-Detection-with-OpenCV\\profiling\\cfg\\yolov3.cfg",
                      "C:\\Development\\PointerAppAlyn\\YOLOv3-Object-Detection-with-OpenCV\\profiling\\weights\\yolov3.weights",
                      "C:\\Development\\PointerAppAlyn\\YOLOv3-Object-Detection-with-OpenCV\\profiling\\coco.names");
            var items = yoloWrapper.Detect(request.Image.ToByteArray());

            var result = new Result();

            foreach (var yoloItem in items)
            {
                Console.WriteLine(yoloItem.Type);
                result.Detections.Add(new DetectionResult
                {
                    Type = yoloItem.Type,
                    X    = yoloItem.X,
                    Y    = yoloItem.Y,
                    W    = yoloItem.Width,
                    H    = yoloItem.Height
                });
            }

            return(Task.FromResult(result));
        }
Пример #8
0
        private void btnOpen_Click(object sender, EventArgs e)
        {
            // using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "PNG|*.png|JPEG|*.jpeg|JPG|*.jpg"})
            // {
            //   if (ofd.ShowDialog() == DialogResult.OK)
            // {

            pic.Image = Image.FromFile(@"D:\Visual Studio 2017\Projects\Alturos.Yolo-master\Images\Car1.png");
            using (var yoloWrapper = new YoloWrapper("yolov2-tiny-voc.cfg", "yolov2-tiny-voc.weights", "voc.names"))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    pic.Image.Save(ms, ImageFormat.Png);
                    var items = yoloWrapper.Detect(ms.ToArray());
                    yoloItemBindingSource.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
            }
            //}
            //}
        }
Пример #9
0
        public void DetectNative()
        {
            var configuration = new YoloConfigurationDetector().Detect();

            using (var yoloWrapper = new YoloWrapper(configuration))
            {
                using (var image = new Bitmap(600, 400))
                    using (var canvas = Graphics.FromImage(image))
                    {
                        canvas.Clear(Color.AliceBlue);
                        canvas.FillPie(Brushes.Black, 0, 0, 100, 100, 0, 360);
                        canvas.Flush();

                        BitmapData bmpData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
                                                            ImageLockMode.ReadOnly,
                                                            image.PixelFormat);

                        var rawImagePtr = bmpData.Scan0;
                        var bmpImagePtr = IntPtr.Zero;

                        try
                        {
                            bmpImagePtr = CreateBmp(rawImagePtr, image.Width, image.Height, out var length);
                            var result = yoloWrapper.Detect(bmpImagePtr, length);
                        }
                        finally
                        {
                            if (bmpImagePtr != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(bmpImagePtr);
                            }
                        }
                    }
            }
        }
Пример #10
0
        private void Grabar(object sender, NewFrameEventArgs frame)
        {
            Bitmap    video   = (Bitmap)frame.Frame.Clone();
            Bitmap    temp    = new Bitmap(video.Width, video.Height);
            Graphics  g       = Graphics.FromImage(temp);
            int       left    = video.Width / 4;
            int       top     = video.Height / 4;
            int       width   = video.Width / 2;
            int       height  = video.Height / 2;
            Rectangle srcRect = new Rectangle(left, top, width, height);
            Rectangle dstRect = new Rectangle(0, 0, temp.Width, temp.Height);

            g.DrawImage(video, dstRect, srcRect, GraphicsUnit.Pixel);

            //========================================= Prueba IA =================================================
            picture.Image = video;
            var configurationDetector = new ConfigurationDetector();
            var config       = configurationDetector.Detect();
            var yolo         = new YoloWrapper(config);
            var memoryStream = new MemoryStream();

            video.Save(memoryStream, ImageFormat.Png);
            var items = yolo.Detect(memoryStream.ToArray()).ToList();

            AddDetailsToPictureBox(picture, items, frame);

            //========================================= Prueba IA =================================================

            //picture.Image = temp;
            //pictureBox1.Image = this.Tracker(frame, color1, "celeste");
            //pictureBox2.Image = this.Tracker(frame, color2, "verde");
            //pictureBox3.Image = this.Tracker(frame, color3, "rojo");
        }
Пример #11
0
        private void narsalarniAniqlashToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var configurationDetector = new ConfigurationDetector();
            var config = configurationDetector.Detect();

            using (var yoloWrapper = new YoloWrapper(config))
            {
                var items = yoloWrapper.Detect(ImageToByte(pictureBox1.Image));
                if (items.Count() > 0)
                {
                    foreach (YoloItem item in items)
                    {
                        Bitmap    bmp       = new Bitmap(pictureBox1.Image);
                        Rectangle rectangle = new Rectangle(item.X, item.Y, item.Width, item.Height);
                        ImgInput = new Image <Bgr, byte>(bmp);
                        ImgInput.Draw(rectangle, new Bgr(0, 0, 255), 2);
                        ImgInput.Draw($"{item.Type} ({string.Format("{0:0.0}", item.Confidence)})",
                                      new Point(item.X - 2, item.Y - 2),
                                      Emgu.CV.CvEnum.FontFace.HersheyDuplex, 0.5, new Bgr(Color.DarkBlue));

                        pictureBox1.Image = ImgInput.Bitmap;
                    }
                }
            }
        }
Пример #12
0
        private void DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            if (yoloWrapper != null)
            {
                try
                {
                    byte[] raw = e.Result;

                    using (MemoryStream ms = new MemoryStream(raw))
                    {
                        List <YoloItem> items = yoloWrapper.Detect(ms.ToArray()).ToList();
                        DrawImage(items, ms);
                        if (items.Exists(x => x.Type == "DevelopBeyond18!"))
                        {
                            if (!playing)
                            {
                                player.Play();
                                playing = true;
                            }
                        }
                        ev.Set();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Got exception: " + ex.Message);
                }
            }
        }
Пример #13
0
        private void Check(GpuConfig gpuConfig)
        {
            var yoloWrapper = new YoloWrapper("yolov2-tiny-voc.cfg", "yolov2-tiny-voc.weights", "voc.names", gpuConfig);
            var files       = Directory.GetFiles(@".\Images");

            var retrys = 10;

            var sw = new Stopwatch();

            foreach (var file in files)
            {
                var elapsed   = 0.0;
                var fileInfo  = new FileInfo(file);
                var imageData = File.ReadAllBytes(file);

                for (var i = 0; i < retrys; i++)
                {
                    sw.Restart();
                    yoloWrapper.Detect(imageData);
                    sw.Stop();

                    elapsed += sw.Elapsed.TotalMilliseconds;
                }

                var average = elapsed / retrys;
                Console.WriteLine($"{fileInfo.Name} {average}ms");
            }

            yoloWrapper.Dispose();
        }
Пример #14
0
        public int Detect(string imgPath)
        {
            string target1 = @"D:\Visual Project\TrafficServer\TrafficServer\bin\yolov2-tiny-voc.cfg";
            string target2 = @"D:\Visual Project\TrafficServer\TrafficServer\bin\yolov2-tiny-voc.weights";
            string target3 = @"D:\Visual Project\TrafficServer\TrafficServer\bin\voc.names";

            if (File.Exists(target1))
            {
                var configurationDetector = new ConfigurationDetector();
                using (var yoloWrapper = new YoloWrapper(target1, target2, target3))
                {
                    var sw = new Stopwatch();
                    sw.Start();
                    var items = yoloWrapper.Detect(imgPath);
                    //txtKetQua.Text = "Number of Object Detected: " + items.Count().ToString() + "\tin: " + sw.Elapsed.TotalSeconds + "s\n";
                    int y = 0;
                    foreach (Alturos.Yolo.Model.YoloItem s in items)
                    {
                        if (s.Type.ToString().Equals("car"))
                        {
                            y++;
                        }
                    }
                    sw.Stop();
                    return(items.Count());
                }
                //txtKetQua.Text = "Yes";
            }
            else
            {
                return(0);
            }
        }
Пример #15
0
        public CvModel[] DetectObjects(Bitmap Source, Rectangle Look_Bounds)
        {
            // MemoryStream is created to convert bitmap into a byte array.
            // We then use yolo to perform image detection and convert that into Computer Vision Models.

            CvModel[] @out;
            using (var ms = new MemoryStream())
            {
                Source.Save(ms, ImageFormat.Jpeg);
                var items = Wrapper.Detect(ms.ToArray());

                @out = new CvModel[items.Count()];
                int index = 0; // We can't directly access an index of an IEnumerable<T> so we declare the index here, iterate every object of our IEnumerable and then put it into our CvModel array.
                foreach (var item in items)
                {
                    @out[index++] = new CvModel {
                        BoundingBox = new Rectangle(item.X, item.Y, item.Width, item.Height),
                        Confidence  = (float)item.Confidence,
                        Look_Bounds = Look_Bounds,
                        Type        = item.Type
                    };
                }
            }
            return(@out);
        }
        /// <summary>
        /// Analyses a bitmap by detecting all the objects in it and returning them as a list.
        /// </summary>
        /// <param name="image"></param>
        public IEnumerable <YoloItem> DetectItemsInBitmap(Bitmap image)
        {
            var cfgPath     = ServiceContainer.GetService <GlobalEnviromentService>().YOLOConfigLocation;
            var weightsPath = ServiceContainer.GetService <GlobalEnviromentService>().YOLOWeightsLocation;
            var namesPath   = ServiceContainer.GetService <GlobalEnviromentService>().YOLONamesLocation;

            using (var yoloWrapper = new YoloWrapper(cfgPath, weightsPath, namesPath))
            {
                using (var memStream = new MemoryStream())
                {
                    image.Save(memStream, ImageFormat.Png);
                    var items = yoloWrapper.Detect(memStream.ToArray());
                    // We do NOT want sports ball as objects.
                    // TODO: Later I can add a blacklist of all the objects that are not important to ignore them.
                    items = items.Where(i => i.Type != "sports ball").Select(i => i);

                    for (int i = 0; i < items.Count(); i++)
                    {
                        items.ElementAt(i).Type = $"object #{i}";
                    }

                    return(items);
                }
            }
        }
Пример #17
0
        static void Main(string[] args)
        {
            var windowCapture = new Window("windowCapture");
            var capture       = new VideoCapture(0);
            var image         = new Mat();
            var yoloWrapper   = new YoloWrapper("yolov2-tiny-voc.cfg", "yolov2-tiny-voc.weights", "voc.names", DetectionSystem.GPU);

            while (true)
            {
                capture.Read(image);
                if (image.Empty())
                {
                    break;
                }

                var bytes = image.ToBytes(".png");
                var items = yoloWrapper.Detect(bytes).ToArray();

                foreach (var item in items)
                {
                    var value = (item.Confidence * 100).ToString("0");
                    var text  = $"{item.Type} - {value}%";
                    ImageUtilHelper.AddBoxToImage(image, text, item.X, item.Y, item.X + item.Width, item.Y + item.Height);
                }

                windowCapture.ShowImage(image);

                if ((Cv2.WaitKey(25) & 0xFF) == 'q')
                {
                    Cv2.DestroyAllWindows();
                    break;
                }
            }
        }
Пример #18
0
        private void btnDetect_Click(object sender, EventArgs e)
        {
            var configurationDetector = new ConfigurationDetector();
            var config = configurationDetector.Detect();

            using (var yoloWrapper = new YoloWrapper(config))
            {
                MemoryStream ms = new MemoryStream();
                pictureBox1.Image.Save(ms, ImageFormat.Png);
                var items = yoloWrapper.Detect(ms.ToArray());
                foreach (var item in items)
                {
                    var x      = item.X;
                    var y      = item.Y;
                    var width  = item.Width;
                    var height = item.Height;
                    var type   = item.Type;

                    Pen      pen = new Pen(Color.Red);
                    Graphics g   = pictureBox1.CreateGraphics();
                    g.DrawRectangle(pen, x, y, width, height);
                }

                dataGridView1.DataSource = items;
            }
        }
Пример #19
0
        static void TestLogic1()
        {
            var yoloWrapper = new YoloWrapper("yolov2-tiny-voc.cfg", "yolov2-tiny-voc.weights", "voc.names");
            var files       = Directory.GetFiles(@".\Images");

            var retrys = 100;

            for (var i = 0; i < retrys; i++)
            {
                foreach (var file in files)
                {
                    var fileInfo  = new FileInfo(file);
                    var imageData = File.ReadAllBytes(file);

                    var sw = new Stopwatch();
                    sw.Start();
                    var items = yoloWrapper.Detect(imageData).ToList();
                    sw.Stop();
                    Console.WriteLine($"{fileInfo.Name} found {items.Count} results, elapsed {sw.Elapsed.TotalMilliseconds:0.00}ms");

                    if (items.Count > 0)
                    {
                        Console.WriteLine("------------------DETAILS-----------------");

                        foreach (var item in items)
                        {
                            Console.WriteLine($"Type:{item.Type} Confidence:{item.Confidence:0.00}");
                        }

                        Console.WriteLine("------------------------------------------");
                    }
                }
            }
        }
Пример #20
0
        public override async Task Detect(DetectionRequest request,
                                          IServerStreamWriter <DetectionResponse> responseStream, ServerCallContext context)
        {
            Console.WriteLine("Image received...");
            Stopwatch timer = new Stopwatch();

            timer.Start();

            IEnumerable <YoloItem> items = yoloWrapper.Detect(request.Image.ToByteArray());

            DetectionResponse response = new DetectionResponse();

            foreach (YoloItem item in items)
            {
                response.YoloItems.Add(new DetectionResult
                {
                    Type       = item.Type,
                    Confidence = item.Confidence,
                    X          = item.X,
                    Y          = item.Y,
                    Width      = item.Width,
                    Height     = item.Height
                });
            }
            timer.Stop();
            response.ElapsedMilliseconds = timer.Elapsed.Milliseconds;
            Console.WriteLine("{0} objects detected in {1} ms", response.YoloItems.Count, timer.Elapsed.Milliseconds);

            await responseStream.WriteAsync(response);
        }
Пример #21
0
 public IEnumerable <YoloItem> GetItems(Image img, double confidence = 0.2)
 {
     using (var ms = new MemoryStream())
     {
         img.Save(ms, ImageFormat.Bmp);
         return(yoloWrapper.Detect(ms.ToArray()).Where(x => x.Confidence > confidence));
     }
 }
Пример #22
0
        public void FileNotFoundTest()
        {
            var configuration = new ConfigurationDetector().Detect();

            using (var yoloWrapper = new YoloWrapper(configuration))
            {
                yoloWrapper.Detect("image-not-exists.jpg");
            }
        }
Пример #23
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            var         configurationDetector = new ConfigurationDetector();
            var         config      = configurationDetector.Detect();
            YoloWrapper yoloWrapper = new YoloWrapper(config);

            // OpenCV & WPF setting

            image = new Mat();

            //WriteableBitmap wb = new WriteableBitmap(yoloWidth, yoloHeight, 96, 96, PixelFormats.Bgr24, null);
            var bgWorker = (BackgroundWorker)sender;

            byte[] imageInBytes = new byte[(int)(yoloWidth * yoloHeight * image.Channels())];
            videocapture = new VideoCapture(textBox1.Text);
            while (!bgWorker.CancellationPending)
            {
                using (Mat imageOriginal = new Mat())
                {
                    // read a single frame and convert the frame into a byte array
                    videocapture.Read(imageOriginal);
                    if (imageOriginal.Empty())
                    {
                        break;
                    }
                    image        = imageOriginal.Resize(new OpenCvSharp.Size(yoloWidth, yoloHeight));
                    imageInBytes = image.ToBytes();

                    // conduct object detection and display the result
                    var items = yoloWrapper.Detect(imageInBytes);
                    foreach (var item in items)
                    {
                        var x      = item.X;
                        var y      = item.Y;
                        var width  = item.Width;
                        var height = item.Height;
                        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(image, new OpenCvSharp.Rect(x, y, width, height), Scalar.Green, 3);
                        Cv2.PutText(image, type, new OpenCvSharp.Point(x - 10, y), HersheyFonts.HersheyComplex, 0.5, Scalar.Red);
                    }
                    bgWorker.ReportProgress(0, image);
                    //// display the detection result
                    //WriteableBitmapConverter.ToWriteableBitmap(image, wb);
                    ///* WPF component: videoViewer
                    //<Canvas Name="canvasYoloVideo" Height="608" Width="608">
                    //  <Image Name="videoViewer" Height="608" Width="608" Stretch="Fill" />
                    //</Canvas>
                    //*/
                    //videoViewer.Source = wb;
                }
            }
            // Read a video file and run object detection over it!
        }
Пример #24
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var configurationDetector = new ConfigurationDetector();
            var config = configurationDetector.Detect();

            using (var yoloWrapper = new YoloWrapper(config))
            {
                var items = yoloWrapper.Detect("image.png");
            }
        }
Пример #25
0
        public void DetectFromFilePath()
        {
            var configuration = new ConfigurationDetector().Detect();

            using (var yoloWrapper = new YoloWrapper(configuration))
            {
                var items = yoloWrapper.Detect(this._imagePath);
                Assert.IsTrue(items.Count() > 0);
            }
        }
Пример #26
0
        public IEnumerable <Alturos.Yolo.Model.YoloItem> getItems(System.Drawing.Image img, double confidence = (double)0.4)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                return(yoloWrapper.Detect(ms.ToArray())
                       .Where(x => x.Confidence > confidence && TrainingNames.Contains(x.Type)));
            }
        }
 private void btnDetect_Click(object sender, EventArgs e)
 {
     if (picSample.Image != null)
     {
         var configuration = detector.Detect();
         var yol           = new YoloWrapper(configuration);
         picSample.Image.Save(stream, ImageFormat.Jpeg);
         var extractedItems = yol.Detect(stream.ToArray()).ToList();
     }
 }
Пример #28
0
        static void TestLogic2()
        {
            var configurationDetector = new ConfigurationDetector();
            var config = configurationDetector.Detect();

            using (var yoloWrapper = new YoloWrapper(config))
            {
                var result = yoloWrapper.Detect(@"image.jpg");
            }
        }
Пример #29
0
        private Tuple <List <YoloItem>, string, double> ProcessResizeAfter(YoloWrapper yoloWrapper, byte[] imageData)
        {
            var sw = new Stopwatch();

            sw.Start();
            var items = yoloWrapper.Detect(imageData).ToList();

            sw.Stop();

            return(new Tuple <List <YoloItem>, string, double>(items, $"{sw.Elapsed.TotalMilliseconds:0.00}", sw.Elapsed.TotalMilliseconds));
        }
Пример #30
0
        public void DetectFromFileData()
        {
            var configuration = new ConfigurationDetector().Detect();

            using (var yoloWrapper = new YoloWrapper(configuration))
            {
                var imageData = File.ReadAllBytes(this._imagePath);
                var items     = yoloWrapper.Detect(imageData);
                Assert.IsTrue(items.Count() > 0);
            }
        }