コード例 #1
0
        private void openPic_Click(object sender, EventArgs e)
        {
            if (this.openPicDialog.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            using var image = Image.Load <Rgb24>(this.openPicDialog.FileName);

            var timer = Stopwatch.StartNew();

            ObjectDetectionResult[] detections = YOLO.Detect(this.infer,
                                                             supportedSize: new Size(MS_COCO.InputSize, MS_COCO.InputSize),
                                                             image: image);
            timer.Stop();

            image.Mutate(context => {
                var font      = SystemFonts.CreateFont("Arial", 16);
                var textColor = Color.White;
                var boxPen    = new Pen(Color.White, width: 4);
                foreach (var detection in detections)
                {
                    string className = detection.Class < MS_COCO.ClassCount && detection.Class >= 0
                        ? MS_COCO.ClassNames[detection.Class] : "imaginary class";
                    string text = $"{className}: {detection.Score:P0}";
                    var box     = Scale(detection.Box, image.Size());
                    context.DrawText(text, font, textColor, TopLeft(box));
                    var drawingBox = new RectangularPolygon(box);
                    context.Draw(boxPen, drawingBox);
                }
            });

            using var temp = new MemoryStream();
            image.SaveAsBmp(temp);
            temp.Position = 0;

            this.pictureBox.Image = new System.Drawing.Bitmap(temp);

            this.Text = "YOLO " + string.Join(", ", detections.Select(d => MS_COCO.ClassNames[d.Class]))
                        + " in " + timer.ElapsedMilliseconds + "ms";
        }