Esempio n. 1
0
        private void buildOutputImage(NDArray[] resultArr)
        {
            // get pbtxt items
            PbtxtItems pbTxtItems = PbtxtParser.ParsePbtxtFile(Path.Combine(modelDir, "mscoco_label_map.pbtxt"));

            // get bitmap
            Bitmap bitmap = new Bitmap(_FileName);

            var scores = resultArr[2].AsIterator <float>();
            var boxes  = resultArr[1].GetData <float>();
            var id     = np.squeeze(resultArr[3]).GetData <float>();

            for (int i = 0; i < scores.size; i++)
            {
                float score = scores.MoveNext();
                if (score > MIN_SCORE)
                {
                    float top    = boxes[i * 4] * bitmap.Height;
                    float left   = boxes[i * 4 + 1] * bitmap.Width;
                    float bottom = boxes[i * 4 + 2] * bitmap.Height;
                    float right  = boxes[i * 4 + 3] * bitmap.Width;

                    Rectangle rect = new Rectangle()
                    {
                        X      = (int)left,
                        Y      = (int)top,
                        Width  = (int)(right - left),
                        Height = (int)(bottom - top)
                    };

                    string name = pbTxtItems.items.Where(w => w.id == id[i]).Select(s => s.display_name).FirstOrDefault();
                    label1.Text = name.ToString();
                    drawObjectOnBitmap(bitmap, rect, score, name, i + 1);
                }
            }

            string path = Path.Combine(imageDir, "output.jpg");

            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.Image    = (Image)bitmap;
            //pictureBox2.Image = Image.FromStream(bitmap.Clone());
            //bitmap.Save(path);
            //Console.WriteLine($"Processed image is saved as {path}");
        }
Esempio n. 2
0
        private void buildOutputImage(NDArray[] resultArr)
        {
            // get pbtxt items
            PbtxtItems pbTxtItems = PbtxtParser.ParsePbtxtFile(Path.Join(modelDir, "mscoco_label_map.pbtxt"));

            // get bitmap
            Bitmap bitmap = new Bitmap(Path.Join(imageDir, "input.jpg"));

            float[] scores = resultArr[2].Data <float>();

            for (int i = 0; i < scores.Length; i++)
            {
                float score = scores[i];
                if (score > MIN_SCORE)
                {
                    float[] boxes  = resultArr[1].Data <float>();
                    float   top    = boxes[i * 4] * bitmap.Height;
                    float   left   = boxes[i * 4 + 1] * bitmap.Width;
                    float   bottom = boxes[i * 4 + 2] * bitmap.Height;
                    float   right  = boxes[i * 4 + 3] * bitmap.Width;

                    Rectangle rect = new Rectangle()
                    {
                        X      = (int)left,
                        Y      = (int)top,
                        Width  = (int)(right - left),
                        Height = (int)(bottom - top)
                    };

                    float[] ids = resultArr[3].Data <float>();

                    string name = pbTxtItems.items.Where(w => w.id == (int)ids[i]).Select(s => s.display_name).FirstOrDefault();

                    drawObjectOnBitmap(bitmap, rect, score, name);
                }
            }

            string path = Path.Join(imageDir, "output.jpg");

            bitmap.Save(path);
            Console.WriteLine($"Processed image is saved as {path}");
        }