Exemple #1
0
        private static PbtxtItems ParsePbtxtFile(string filePath)
        {
            string line;
            string newText = "{\"items\":[";

            using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    string newline = string.Empty;

                    if (line.Contains("{"))
                    {
                        newline = line.Replace("item", "").Trim();
                        //newText += line.Insert(line.IndexOf("=") + 1, "\"") + "\",";
                        newText += newline;
                    }
                    else if (line.Contains("}"))
                    {
                        newText  = newText.Remove(newText.Length - 1);
                        newText += line;
                        newText += ",";
                    }
                    else
                    {
                        newline  = line.Replace(":", "\":").Trim();
                        newline  = "\"" + newline;// newline.Insert(0, "\"");
                        newline += ",";

                        newText += newline;
                    }
                }

                newText  = newText.Remove(newText.Length - 1);
                newText += "]}";

                reader.Close();
            }

            PbtxtItems items = JsonConvert.DeserializeObject <PbtxtItems>(newText);

            return(items);
        }
Exemple #2
0
        private static void buildOutputImage(NDArray[] resultArr)
        {
            // get pbtxt items
            PbtxtItems pbTxtItems = ParsePbtxtFile(Path.Join(modelDir, "mscoco_label_map.pbtxt"));

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

            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();

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

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

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