Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. using System;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Configuration;
  9. using TensorFlow;
  10. using ExampleCommon;
  11. //using Mono.Options;
  12. using System.Reflection;
  13. using System.Net;
  14. //using ICSharpCode.SharpZipLib.Tar;
  15. //using ICSharpCode.SharpZipLib.GZip;
  16.  
  17. public class Program
  18. {
  19.  
  20.     private static IEnumerable<CatalogItem> _catalog;
  21.     private static string _currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  22.     private static string _input_relative = "test_images/input.jpg";
  23.     private static string _output_relative = "test_images/output.jpg";
  24.     private static string _input = Path.Combine(_currentDir, _input_relative);
  25.     private static string _output = Path.Combine(_currentDir, _output_relative);
  26.     private static string _catalogPath;
  27.     private static string _modelPath;
  28.  
  29.     private static double MIN_SCORE_FOR_OBJECT_HIGHLIGHTING = 0.5;
  30.  
  31.     //static OptionSet options = new OptionSet()
  32.     //    {
  33.     //        { "input_image=",  "Specifies the path to an image ", v => _input = v },
  34.     //        { "output_image=",  "Specifies the path to the output image with detected objects", v => _output = v },
  35.     //        { "catalog=", "Specifies the path to the .pbtxt objects catalog", v=> _catalogPath = v},
  36.     //        { "model=", "Specifies the path to the trained model", v=> _modelPath = v},
  37.     //        { "h|help", v => Help () }
  38.     //    };
  39.  
  40.     private class String2String
  41.     {
  42.         public string input { get; set; }
  43.         public string output { get; set; }
  44.  
  45.         public String2String(string input, string output)
  46.         {
  47.             this.input = input;
  48.             this.output = output;
  49.         }
  50.     }
  51.     /// <summary>
  52.     /// Run the ExampleObjectDetection util from command line. Following options are available:
  53.     /// input_image - optional, the path to the image for processing (the default is 'test_images/input.jpg')
  54.     /// output_image - optional, the path where the image with detected objects will be saved (the default is 'test_images/output.jpg')
  55.     /// catalog - optional, the path to the '*.pbtxt' file (by default, 'mscoco_label_map.pbtxt' been loaded)
  56.     /// model - optional, the path to the '*.pb' file (by default, 'frozen_inference_graph.pb' model been used, but you can download any other from here
  57.     /// https://github.com/tensorflow/models/blob/master/object_detection/g3doc/detection_model_zoo.md or train your own)
  58.     ///
  59.     /// for instance,
  60.     /// ExampleObjectDetection --input_image="/demo/input.jpg" --output_image="/demo/output.jpg" --catalog="/demo/mscoco_label_map.pbtxt" --model="/demo/frozen_inference_graph.pb"
  61.     /// </summary>
  62.     /// <param name="args"></param>
  63.     static void Main(string[] args)
  64.     {
  65.         //options.Parse(args);
  66.  
  67.         //if (_catalogPath == null)
  68.         //{
  69.         //    _catalogPath = DownloadDefaultTexts(_currentDir);
  70.         //}
  71.  
  72.         //if (_modelPath == null)
  73.         //{
  74.         //    _modelPath = DownloadDefaultModel(_currentDir);
  75.         //}
  76.        
  77.         _catalogPath = "C:\\Users\\tmp_u\\Documents\\tensor_detection\\models-master\\research\\object_detection\\data\\mscoco_label_map.pbtxt";
  78.         _modelPath = "C:\\Users\\tmp_u\\Documents\\tensor_detection\\models-master\\research\\object_detection\\ssd_mobilenet_v1_coco_11_06_2017\\frozen_inference_graph.pb";
  79.  
  80.         _catalog = CatalogUtil.ReadCatalogItems(_catalogPath);
  81.         var fileTuples = new List<String2String>() { new String2String(_input,_output) };
  82.         string modelFile = _modelPath;
  83.  
  84.         using (var graph = new TFGraph())
  85.         {
  86.             var model = File.ReadAllBytes(modelFile);
  87.             graph.Import(new TFBuffer(model));
  88.  
  89.             using (var session = new TFSession(graph))
  90.             {
  91.                 Console.WriteLine("Detecting objects");
  92.  
  93.                 foreach (var tuple in fileTuples)
  94.                 {
  95.                     var tensor = ImageUtil.CreateTensorFromImageFile(tuple.input, TFDataType.UInt8);
  96.                     var runner = session.GetRunner();
  97.  
  98.  
  99.                     runner
  100.                         .AddInput(graph["image_tensor"][0], tensor)
  101.                         .Fetch(
  102.                         graph["detection_boxes"][0],
  103.                         graph["detection_scores"][0],
  104.                         graph["detection_classes"][0],
  105.                         graph["num_detections"][0]);
  106.                     var output = runner.Run();
  107.  
  108.                     var boxes = (float[,,])output[0].GetValue(jagged: false);
  109.                     var scores = (float[,])output[1].GetValue(jagged: false);
  110.                     var classes = (float[,])output[2].GetValue(jagged: false);
  111.                     var num = (float[])output[3].GetValue(jagged: false);
  112.  
  113.                     DrawBoxes(boxes, scores, classes, tuple.input, tuple.output, MIN_SCORE_FOR_OBJECT_HIGHLIGHTING);
  114.                     Console.WriteLine($"Done. See {_output_relative}");
  115.                 }
  116.             }
  117.         }
  118.     }
  119.  
  120.     //private static string DownloadDefaultModel(string dir)
  121.     //{
  122.     //    string defaultModelUrl = ConfigurationManager.AppSettings["DefaultModelUrl"] ?? throw new ConfigurationErrorsException("'DefaultModelUrl' setting is missing in the configuration file");
  123.  
  124.     //    var modelFile = Path.Combine(dir, "faster_rcnn_inception_resnet_v2_atrous_coco_11_06_2017/frozen_inference_graph.pb");
  125.     //    var zipfile = Path.Combine(dir, "faster_rcnn_inception_resnet_v2_atrous_coco_11_06_2017.tar.gz");
  126.  
  127.     //    if (File.Exists(modelFile))
  128.     //        return modelFile;
  129.  
  130.     //    if (!File.Exists(zipfile))
  131.     //    {
  132.     //        Console.WriteLine("Downloading default model");
  133.     //        var wc = new WebClient();
  134.     //        wc.DownloadFile(defaultModelUrl, zipfile);
  135.     //    }
  136.  
  137.     //    ExtractToDirectory(zipfile, dir);
  138.     //    File.Delete(zipfile);
  139.  
  140.     //    return modelFile;
  141.     //}
  142.  
  143.     //private static void ExtractToDirectory(string file, string targetDir)
  144.     //{
  145.     //    Console.WriteLine("Extracting");
  146.  
  147.     //    using (Stream inStream = File.OpenRead(file))
  148.     //    using (Stream gzipStream = new GZipInputStream(inStream))
  149.     //    {
  150.     //        TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
  151.     //        tarArchive.ExtractContents(targetDir);
  152.     //    }
  153.     //}
  154.  
  155.     //private static string DownloadDefaultTexts(string dir)
  156.     //{
  157.     //    Console.WriteLine("Downloading default label map");
  158.  
  159.     //    string defaultTextsUrl = ConfigurationManager.AppSettings["DefaultTextsUrl"] ?? throw new ConfigurationErrorsException("'DefaultTextsUrl' setting is missing in the configuration file");
  160.     //    var textsFile = Path.Combine(dir, "C:\Users\tmp_u\Documents\tensor_detection\models-master\research\object_detection\data\mscoco_label_map.pbtxt");
  161.     //    var wc = new WebClient();
  162.     //    wc.DownloadFile(defaultTextsUrl, textsFile);
  163.  
  164.     //    return textsFile;
  165.     //}
  166.  
  167.     private static void DrawBoxes(float[,,] boxes, float[,] scores, float[,] classes, string inputFile, string outputFile, double minScore)
  168.     {
  169.         var x = boxes.GetLength(0);
  170.         var y = boxes.GetLength(1);
  171.         var z = boxes.GetLength(2);
  172.  
  173.         float ymin = 0, xmin = 0, ymax = 0, xmax = 0;
  174.  
  175.         using (var editor = new ImageEditor(inputFile, outputFile))
  176.         {
  177.             for (int i = 0; i < x; i++)
  178.             {
  179.                 for (int j = 0; j < y; j++)
  180.                 {
  181.                     if (scores[i, j] < minScore) continue;
  182.  
  183.                     for (int k = 0; k < z; k++)
  184.                     {
  185.                         var box = boxes[i, j, k];
  186.                         switch (k)
  187.                         {
  188.                             case 0:
  189.                                 ymin = box;
  190.                                 break;
  191.                             case 1:
  192.                                 xmin = box;
  193.                                 break;
  194.                             case 2:
  195.                                 ymax = box;
  196.                                 break;
  197.                             case 3:
  198.                                 xmax = box;
  199.                                 break;
  200.                         }
  201.  
  202.                     }
  203.  
  204.                     int value = Convert.ToInt32(classes[i, j]);
  205.                     CatalogItem catalogItem = _catalog.FirstOrDefault(item => item.Id == value);
  206.                     editor.AddBox(xmin, xmax, ymin, ymax, $"{catalogItem.DisplayName} : {(scores[i, j] * 100).ToString("0")}%");
  207.                 }
  208.             }
  209.         }
  210.     }
  211.  
  212.     //private static void Help()
  213.     //{
  214.     //    options.WriteOptionDescriptions(Console.Out);
  215.     //}
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement