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.

PbtxtParser.cs 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 Newtonsoft.Json;
  14. using System.Collections.Generic;
  15. namespace TensorFlowNET.Examples.Utility
  16. {
  17. public class PbtxtItem
  18. {
  19. public string name { get; set; }
  20. public int id { get; set; }
  21. public string display_name { get; set; }
  22. }
  23. public class PbtxtItems
  24. {
  25. public List<PbtxtItem> items { get; set; }
  26. }
  27. public class PbtxtParser
  28. {
  29. public static PbtxtItems ParsePbtxtFile(string filePath)
  30. {
  31. string line;
  32. string newText = "{\"items\":[";
  33. using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
  34. {
  35. while ((line = reader.ReadLine()) != null)
  36. {
  37. string newline = string.Empty;
  38. if (line.Contains("{"))
  39. {
  40. newline = line.Replace("item", "").Trim();
  41. //newText += line.Insert(line.IndexOf("=") + 1, "\"") + "\",";
  42. newText += newline;
  43. }
  44. else if (line.Contains("}"))
  45. {
  46. newText = newText.Remove(newText.Length - 1);
  47. newText += line;
  48. newText += ",";
  49. }
  50. else
  51. {
  52. newline = line.Replace(":", "\":").Trim();
  53. newline = "\"" + newline;// newline.Insert(0, "\"");
  54. newline += ",";
  55. newText += newline;
  56. }
  57. }
  58. newText = newText.Remove(newText.Length - 1);
  59. newText += "]}";
  60. reader.Close();
  61. }
  62. PbtxtItems items = JsonConvert.DeserializeObject<PbtxtItems>(newText);
  63. return items;
  64. }
  65. }
  66. }