Bladeren bron

Enable logging to txt as JSON

Nick Steyer 1 jaar geleden
bovenliggende
commit
f16ef26e6b

+ 103 - 9
Assets/Logging/DetectionFrameLogger.cs

@@ -1,7 +1,9 @@
-using System;
+using Newtonsoft.Json;
+using System;
 using System.IO;
 using System.Text;
 using UnityEngine;
+using UnityEngine.Networking;
 
 namespace Assets.Logging
 {
@@ -9,18 +11,110 @@ namespace Assets.Logging
     {
         public void Log(DetectionFrame frame)
         {
-            var builder = new StringBuilder();
-            builder.AppendLine($"{DateTime.Now}: Detected {frame.objectCount} objects");
+            var pocoFrame = new Poco.DetectionFrame()
+            {
+                RawObjectsFrame = new Poco.ObjectsFrameSDK
+                {
+                    DetectionModel = frame.rawObjectsFrame.detectionModel,
+                    IsNew = frame.rawObjectsFrame.isNew,
+                    IsTracked = frame.rawObjectsFrame.isTracked,
+                    NumObject = frame.rawObjectsFrame.numObject,
+                    Timestamp = frame.rawObjectsFrame.timestamp
+                }
+            };
 
-            int count = 0;
-            foreach(var detectedObject in frame.detectedObjects)
+            foreach (var detectedObject in frame.detectedObjects)
             {
-                count++;
-                var position = detectedObject.Get3DWorldPosition();
-                builder.AppendLine($"{DateTime.Now}: Object {count} detected at ({position.x}|{position.y}|{position.z}). Type: {detectedObject.objectClass}");
+                pocoFrame.DetectedObjects.Add(new Poco.DetectedObject
+                {
+                    RawObjectData = new Poco.ObjectDataSDK()
+                    {
+                        actionState = detectedObject.actionState,
+                        confidence = detectedObject.confidence,
+                        globalRootOrientation = detectedObject.rawObjectData.globalRootOrientation,
+                        headBoundingBox = detectedObject.rawObjectData.headBoundingBox,
+                        headWorldPosition = detectedObject.rawObjectData.headWorldPosition,
+                        id = detectedObject.rawObjectData.id,
+                        imageBoundingBox = detectedObject.rawObjectData.imageBoundingBox,
+                        keypointConfidence = detectedObject.rawObjectData.keypointConfidence,
+                        localOrientationPerJoint = detectedObject.rawObjectData.localOrientationPerJoint,
+                        localPositionPerJoint = detectedObject.rawObjectData.localPositionPerJoint,
+                        mask = detectedObject.rawObjectData.mask,
+                        objectClass = detectedObject.objectClass,
+                        objectSubClass = detectedObject.objectSubClass,
+                        objectTrackingState = detectedObject.rawObjectData.objectTrackingState,
+                        positionCovariance = detectedObject.rawObjectData.positionCovariance,
+                        rawLabel = detectedObject.rawObjectData.rawLabel,
+                        rootWorldPosition = detectedObject.rawObjectData.rootWorldPosition,
+                        rootWorldVelocity = detectedObject.rawObjectData.rootWorldVelocity,
+                        skeletonJointPosition2D = detectedObject.rawObjectData.skeletonJointPosition2D,
+                        skeletonJointPosition = detectedObject.rawObjectData.skeletonJointPosition,
+                        uniqueObjectId = detectedObject.rawObjectData.uniqueObjectId,
+                        worldBoundingBox = detectedObject.rawObjectData.worldBoundingBox
+                    }
+                });
             }
 
-            File.AppendAllText(@"C:\Users\nick.steyer\SmartStreetLight\log.txt", builder.ToString());
+            string json = JsonConvert.SerializeObject(pocoFrame, new Vector2Converter(), new Vector3Converter(), new QuaternionConverter());
+
+            File.AppendAllText(@"C:\Users\nick.steyer\SmartStreetLight\log.txt", json);
+        }
+    }
+
+    public class Vector2Converter : JsonConverter
+    {
+        public override bool CanConvert(Type objectType)
+        {
+            return objectType == typeof(Vector2);
+        }
+
+        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+        {
+            return new Vector2();
+        }
+
+        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+        {
+            var vector = (Vector2)value;
+            writer.WriteValue($"({vector.x}|{vector.y})");
+        }
+    }
+
+    public class Vector3Converter : JsonConverter
+    {
+        public override bool CanConvert(Type objectType)
+        {
+            return objectType == typeof(Vector3);
+        }
+
+        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+        {
+            return new Vector3();
+        }
+
+        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+        {
+            var vector = (Vector3)value;
+            writer.WriteValue($"({vector.x}|{vector.y}|{vector.z})");
+        }
+    }
+
+    public class QuaternionConverter : JsonConverter
+    {
+        public override bool CanConvert(Type objectType)
+        {
+            return objectType == typeof(Quaternion);
+        }
+
+        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+        {
+            return new Quaternion();
+        }
+
+        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+        {
+            var vector = (Quaternion)value;
+            writer.WriteValue($"({vector.x}|{vector.y}|{vector.z}|{vector.w})");
         }
     }
 }

+ 7 - 0
Assets/Logging/Poco/DetectedObject.cs

@@ -0,0 +1,7 @@
+namespace Assets.Logging.Poco
+{
+    public class DetectedObject
+    {
+        public ObjectDataSDK RawObjectData { get; set; }
+    }
+}

+ 13 - 0
Assets/Logging/Poco/DetectionFrame.cs

@@ -0,0 +1,13 @@
+using System.Collections.Generic;
+
+namespace Assets.Logging.Poco
+{
+    public class DetectionFrame
+    {
+        public Poco.ObjectsFrameSDK RawObjectsFrame { get; set; }
+
+        private List<DetectedObject> detObjects = new List<DetectedObject>();
+
+        public List<DetectedObject> DetectedObjects => detObjects;
+    }
+}

+ 38 - 0
Assets/Logging/Poco/ObjectDataSDK.cs

@@ -0,0 +1,38 @@
+using UnityEngine;
+
+namespace Assets.Logging.Poco
+{
+    public class ObjectDataSDK
+    {
+        public int id;
+        public string uniqueObjectId;
+        public int rawLabel;
+        public sl.OBJECT_CLASS objectClass;
+        public sl.OBJECT_SUBCLASS objectSubClass;
+        public sl.OBJECT_TRACK_STATE objectTrackingState;
+        public sl.OBJECT_ACTION_STATE actionState;
+        public float confidence;
+
+        public System.IntPtr mask;
+        public Vector2[] imageBoundingBox;
+
+
+        public Vector3 rootWorldPosition; //object root position
+        public Vector3 headWorldPosition; //object head position (only for HUMAN detectionModel)
+        public Vector3 rootWorldVelocity; //object root velocity
+
+
+        public Vector3[] worldBoundingBox; // 3D Bounding Box of object
+        public Vector3[] headBoundingBox;// 3D Bounding Box of head (only for HUMAN detectionModel)
+
+        public Vector2[] skeletonJointPosition2D;// 2D position of the joints of the skeleton
+        public Vector3[] skeletonJointPosition;// 3D position of the joints of the skeleton
+        public float[] positionCovariance;// covariance matrix of the 3d position, represented by its upper triangular matrix value
+
+        public float[] keypointConfidence;
+
+        public Vector3[] localPositionPerJoint;
+        public Quaternion[] localOrientationPerJoint;
+        public Quaternion globalRootOrientation;
+    }
+}

+ 11 - 0
Assets/Logging/Poco/ObjectsFrameSDK.cs

@@ -0,0 +1,11 @@
+namespace Assets.Logging.Poco
+{
+    public class ObjectsFrameSDK
+    {
+        public int NumObject { get; set; }
+        public ulong Timestamp { get; set; }
+        public int IsNew { get; set; }
+        public int IsTracked { get; set; }
+        public sl.DETECTION_MODEL DetectionModel { get; set; }
+    }
+}

+ 18 - 0
Assets/Logging/StreetLightContext.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Data.Entity;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Assets.Logging
+{
+    [DbConfigurationType(typeof(StreetLightDbConfiguration))]
+    internal class StreetLightContext : DbContext
+    {
+        public StreetLightContext() : base("Server=(LocalDb)\\MSSQLLocalDB;Database=myDataBase;Trusted_Connection=True;")
+        {
+        }
+        public DbSet<Poco.DetectionFrame> DetectionFrames { get; set; }
+    }
+}

+ 13 - 0
Assets/Logging/StreetLightDbConfiguration.cs

@@ -0,0 +1,13 @@
+using System.Data.Entity;
+
+namespace Assets.Logging
+{
+    public class StreetLightDbConfiguration : DbConfiguration
+    {
+        public StreetLightDbConfiguration()
+        {
+            //System.InvalidOperationException: The requested .Net Framework Data Provider's implementation does not have an Instance field of a System.Data.Common.DbProviderFactory derived type
+            //this.SetProviderFactory("", )
+        }
+    }
+}

+ 1 - 2
Assets/ZED/Examples/Object Detection/Scripts/ZED3DObjectVisualizer.cs

@@ -143,8 +143,7 @@ public class ZED3DObjectVisualizer : MonoBehaviour
 
     private void Awake()
     {
-        //zedManagerLazy = new(new LoggingZEDManager(FindObjectOfType<ZEDManager>(), new DetectionFrameLogger()));
-        zedManagerLazy = new(() => FindObjectOfType<ZEDManager>());
+        zedManagerLazy = new(new LoggingZEDManager(FindObjectOfType<ZEDManager>(), new DetectionFrameLogger()));
     }
 
     // Use this for initialization