DetectionFrameLogger.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. namespace Assets.Logging
  8. {
  9. internal class DetectionFrameLogger : ILogger<DetectionFrame>
  10. {
  11. public void Log(DetectionFrame frame)
  12. {
  13. var pocoFrame = new Poco.DetectionFrame()
  14. {
  15. RawObjectsFrame = new Poco.ObjectsFrameSDK
  16. {
  17. DetectionModel = frame.rawObjectsFrame.detectionModel,
  18. IsNew = frame.rawObjectsFrame.isNew,
  19. IsTracked = frame.rawObjectsFrame.isTracked,
  20. NumObject = frame.rawObjectsFrame.numObject,
  21. Timestamp = frame.rawObjectsFrame.timestamp
  22. }
  23. };
  24. foreach (var detectedObject in frame.detectedObjects)
  25. {
  26. pocoFrame.DetectedObjects.Add(new Poco.DetectedObject
  27. {
  28. RawObjectData = new Poco.ObjectDataSDK()
  29. {
  30. actionState = detectedObject.actionState,
  31. confidence = detectedObject.confidence,
  32. globalRootOrientation = detectedObject.rawObjectData.globalRootOrientation,
  33. headBoundingBox = detectedObject.rawObjectData.headBoundingBox,
  34. headWorldPosition = detectedObject.rawObjectData.headWorldPosition,
  35. id = detectedObject.rawObjectData.id,
  36. imageBoundingBox = detectedObject.rawObjectData.imageBoundingBox,
  37. keypointConfidence = detectedObject.rawObjectData.keypointConfidence,
  38. localOrientationPerJoint = detectedObject.rawObjectData.localOrientationPerJoint,
  39. localPositionPerJoint = detectedObject.rawObjectData.localPositionPerJoint,
  40. mask = detectedObject.rawObjectData.mask,
  41. objectClass = detectedObject.objectClass,
  42. objectSubClass = detectedObject.objectSubClass,
  43. objectTrackingState = detectedObject.rawObjectData.objectTrackingState,
  44. positionCovariance = detectedObject.rawObjectData.positionCovariance,
  45. rawLabel = detectedObject.rawObjectData.rawLabel,
  46. rootWorldPosition = detectedObject.rawObjectData.rootWorldPosition,
  47. rootWorldVelocity = detectedObject.rawObjectData.rootWorldVelocity,
  48. skeletonJointPosition2D = detectedObject.rawObjectData.skeletonJointPosition2D,
  49. skeletonJointPosition = detectedObject.rawObjectData.skeletonJointPosition,
  50. uniqueObjectId = detectedObject.rawObjectData.uniqueObjectId,
  51. worldBoundingBox = detectedObject.rawObjectData.worldBoundingBox
  52. }
  53. });
  54. }
  55. string json = JsonConvert.SerializeObject(pocoFrame, new Vector2Converter(), new Vector3Converter(), new QuaternionConverter());
  56. File.AppendAllText(@"C:\Users\nick.steyer\SmartStreetLight\log.txt", json);
  57. }
  58. }
  59. public class Vector2Converter : JsonConverter
  60. {
  61. public override bool CanConvert(Type objectType)
  62. {
  63. return objectType == typeof(Vector2);
  64. }
  65. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  66. {
  67. return new Vector2();
  68. }
  69. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  70. {
  71. var vector = (Vector2)value;
  72. writer.WriteValue($"({vector.x}|{vector.y})");
  73. }
  74. }
  75. public class Vector3Converter : JsonConverter
  76. {
  77. public override bool CanConvert(Type objectType)
  78. {
  79. return objectType == typeof(Vector3);
  80. }
  81. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  82. {
  83. return new Vector3();
  84. }
  85. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  86. {
  87. var vector = (Vector3)value;
  88. writer.WriteValue($"({vector.x}|{vector.y}|{vector.z})");
  89. }
  90. }
  91. public class QuaternionConverter : JsonConverter
  92. {
  93. public override bool CanConvert(Type objectType)
  94. {
  95. return objectType == typeof(Quaternion);
  96. }
  97. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  98. {
  99. return new Quaternion();
  100. }
  101. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  102. {
  103. var vector = (Quaternion)value;
  104. writer.WriteValue($"({vector.x}|{vector.y}|{vector.z}|{vector.w})");
  105. }
  106. }
  107. }