You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectDetection.cs 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using NumSharp;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Text;
  18. using Tensorflow;
  19. using TensorFlowNET.Examples.Utility;
  20. using System.Drawing;
  21. using System.Drawing.Drawing2D;
  22. using System.Linq;
  23. using static Tensorflow.Python;
  24. namespace TensorFlowNET.Examples
  25. {
  26. public class ObjectDetection : IExample
  27. {
  28. public bool Enabled { get; set; } = true;
  29. public string Name => "Object Detection";
  30. public bool IsImportingGraph { get; set; } = false;
  31. public float MIN_SCORE = 0.5f;
  32. string modelDir = "ssd_mobilenet_v1_coco_2018_01_28";
  33. string imageDir = "images";
  34. string pbFile = "frozen_inference_graph.pb";
  35. string labelFile = "mscoco_label_map.pbtxt";
  36. string picFile = "input.jpg";
  37. public bool Run()
  38. {
  39. PrepareData();
  40. // read in the input image
  41. var imgArr = ReadTensorFromImageFile(Path.Join(imageDir, "input.jpg"));
  42. var graph = new Graph().as_default();
  43. graph.Import(Path.Join(modelDir, pbFile));
  44. Tensor tensorNum = graph.OperationByName("num_detections");
  45. Tensor tensorBoxes = graph.OperationByName("detection_boxes");
  46. Tensor tensorScores = graph.OperationByName("detection_scores");
  47. Tensor tensorClasses = graph.OperationByName("detection_classes");
  48. Tensor imgTensor = graph.OperationByName("image_tensor");
  49. Tensor[] outTensorArr = new Tensor[] { tensorNum, tensorBoxes, tensorScores, tensorClasses };
  50. with(tf.Session(graph), sess =>
  51. {
  52. var results = sess.run(outTensorArr, new FeedItem(imgTensor, imgArr));
  53. NDArray[] resultArr = results.Data<NDArray>();
  54. buildOutputImage(resultArr);
  55. });
  56. return true;
  57. }
  58. public void PrepareData()
  59. {
  60. // get model file
  61. string url = "http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2018_01_28.tar.gz";
  62. Web.Download(url, modelDir, "ssd_mobilenet_v1_coco.tar.gz");
  63. Compress.ExtractTGZ(Path.Join(modelDir, "ssd_mobilenet_v1_coco.tar.gz"), "./");
  64. // download sample picture
  65. url = $"https://github.com/tensorflow/models/raw/master/research/object_detection/test_images/image2.jpg";
  66. Web.Download(url, imageDir, "input.jpg");
  67. // download the pbtxt file
  68. url = $"https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/mscoco_label_map.pbtxt";
  69. Web.Download(url, modelDir, "mscoco_label_map.pbtxt");
  70. }
  71. private NDArray ReadTensorFromImageFile(string file_name)
  72. {
  73. return with(tf.Graph().as_default(), graph =>
  74. {
  75. var file_reader = tf.read_file(file_name, "file_reader");
  76. var decodeJpeg = tf.image.decode_jpeg(file_reader, channels: 3, name: "DecodeJpeg");
  77. var casted = tf.cast(decodeJpeg, TF_DataType.TF_UINT8);
  78. var dims_expander = tf.expand_dims(casted, 0);
  79. return with(tf.Session(graph), sess => sess.run(dims_expander));
  80. });
  81. }
  82. private void buildOutputImage(NDArray[] resultArr)
  83. {
  84. // get pbtxt items
  85. PbtxtItems pbTxtItems = PbtxtParser.ParsePbtxtFile(Path.Join(modelDir, "mscoco_label_map.pbtxt"));
  86. // get bitmap
  87. Bitmap bitmap = new Bitmap(Path.Join(imageDir, "input.jpg"));
  88. float[] scores = resultArr[2].Data<float>();
  89. for (int i=0; i<scores.Length; i++)
  90. {
  91. float score = scores[i];
  92. if (score > MIN_SCORE)
  93. {
  94. float[] boxes = resultArr[1].Data<float>();
  95. float top = boxes[i * 4] * bitmap.Height;
  96. float left = boxes[i * 4 + 1] * bitmap.Width;
  97. float bottom = boxes[i * 4 + 2] * bitmap.Height;
  98. float right = boxes[i * 4 + 3] * bitmap.Width;
  99. Rectangle rect = new Rectangle()
  100. {
  101. X = (int)left,
  102. Y = (int)top,
  103. Width = (int)(right - left),
  104. Height = (int)(bottom - top)
  105. };
  106. float[] ids = resultArr[3].Data<float>();
  107. string name = pbTxtItems.items.Where(w => w.id == (int)ids[i]).Select(s=>s.display_name).FirstOrDefault();
  108. drawObjectOnBitmap(bitmap, rect, score, name);
  109. }
  110. }
  111. string path = Path.Join(imageDir, "output.jpg");
  112. bitmap.Save(path);
  113. Console.WriteLine($"Processed image is saved as {path}");
  114. }
  115. private void drawObjectOnBitmap(Bitmap bmp, Rectangle rect, float score, string name)
  116. {
  117. using (Graphics graphic = Graphics.FromImage(bmp))
  118. {
  119. graphic.SmoothingMode = SmoothingMode.AntiAlias;
  120. using (Pen pen = new Pen(Color.Red, 2))
  121. {
  122. graphic.DrawRectangle(pen, rect);
  123. Point p = new Point(rect.Right + 5, rect.Top + 5);
  124. string text = string.Format("{0}:{1}%", name, (int)(score * 100));
  125. graphic.DrawString(text, new Font("Verdana", 8), Brushes.Red, p);
  126. }
  127. }
  128. }
  129. public Graph ImportGraph()
  130. {
  131. throw new NotImplementedException();
  132. }
  133. public Graph BuildGraph()
  134. {
  135. throw new NotImplementedException();
  136. }
  137. public void Train(Session sess)
  138. {
  139. throw new NotImplementedException();
  140. }
  141. public void Predict(Session sess)
  142. {
  143. throw new NotImplementedException();
  144. }
  145. public void Test(Session sess)
  146. {
  147. throw new NotImplementedException();
  148. }
  149. }
  150. }