Browse Source

hacky solution for restarting after the intel camera crashing

Florian Mueller 7 years ago
parent
commit
1268d82326

BIN
.vs/bbiwarg/v14/.suo


+ 38 - 68
bbiwarg/Input/InputProviding/InputProviderIntel.cs

@@ -1,34 +1,30 @@
 using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 using Emgu.CV;
 using Emgu.CV.Structure;
-using System.Drawing;
+using System.Threading;
 
 namespace BBIWARG.Input.InputProviding
 {
-    class InputProviderIntel : IInputProvider
+    class InputProviderIntel : MarshalByRefObject, IInputProvider
     {
-        PXCMSenseManager senseManager;
+        IntelCameraWrapper wrapper;
 
         public int CurrentFrameID
         {
             get;
-            private set;
+            set;
         }
 
         public float FieldOfViewHorizontal
         {
             get;
-            private set;
+            set;
         }
 
         public float FieldOfViewVertical
         {
             get;
-            private set;
+            set;
         }
 
         public int ImageWidth
@@ -44,14 +40,15 @@ namespace BBIWARG.Input.InputProviding
         public bool IsActive
         {
             get;
-            private set;
+            set;
         }
 
         public event DeviceStartedEventHandler DeviceStartedEvent;
 
         public event NewFrameEventHandler NewFrameEvent;
 
-        protected UInt16 lowConfidenceValue;
+        public UInt16 lowConfidenceValue;
+        private AppDomain domain;
 
         public void initialize()
         {
@@ -60,77 +57,50 @@ namespace BBIWARG.Input.InputProviding
 
         public void start()
         {
-            senseManager = PXCMSenseManager.CreateInstance();
-            PXCMVideoModule.DataDesc ddesc = new PXCMVideoModule.DataDesc();
-            ddesc.deviceInfo.streams = PXCMCapture.StreamType.STREAM_TYPE_DEPTH;
-            senseManager.EnableStreams(ddesc);
+            domain = System.AppDomain.CreateDomain("IntelSucks");
 
-            senseManager.Init();
-
-            PXCMPointF32 fov = senseManager.captureManager.device.QueryDepthFieldOfView();
-            FieldOfViewHorizontal = fov.x;
-            FieldOfViewVertical = fov.y;
-            
-            lowConfidenceValue = senseManager.captureManager.device.QueryDepthLowConfidenceValue();
-            senseManager.captureManager.device.SetDepthConfidenceThreshold((UInt16)Parameters.ConfidenceImageMinThreshold);
+            wrapper = (IntelCameraWrapper) domain.CreateInstanceAndUnwrap(typeof(IntelCameraWrapper).Assembly.GetName().ToString(), typeof(IntelCameraWrapper).FullName);
+            wrapper.init(this);
 
             IsActive = true;
 
-            if (DeviceStartedEvent != null)
+            if (DeviceStartedEvent != null) {
                 DeviceStartedEvent(this, new EventArgs());
+                DeviceStartedEvent = null; //only notify once...
+            }
+               
+
+            wrapper.run();
 
-            run();
+            if (wrapper.errorstate) {
+                //we have crashed. Kill the app domain and try again.
+                Console.WriteLine("Killing AppDomain...");
+                System.AppDomain.Unload(domain);
+                Thread.Sleep(5000);
+                Console.WriteLine("Starting again...");
+                start();
+            }
         }
 
+
         public void stop()
         {
             IsActive = false;
         }
 
-        protected void run()
+        internal bool hasNewFrameEvent()
         {
-            while (IsActive) {
-                if (senseManager.AcquireFrame(true).IsError())
-                    continue;
-
-                if (NewFrameEvent != null)
-                {
-
-                    PXCMCapture.Sample sample = senseManager.QuerySample();
-                    PXCMImage depthImage = sample.depth;
-                    PXCMImage.ImageInfo info = depthImage.info;
-                    PXCMImage.ImageData imageData;
-                    
-                    depthImage.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH, out imageData);
-                    ushort[] data = imageData.ToUShortArray(0, info.width * info.height);
-
-                    Image<Gray, UInt16> dImg;
-
-                    unsafe
-                    {
-                        fixed (ushort* d = data)
-                        {
-                            IntPtr ptr = (IntPtr)d;
-                            dImg = new Image<Gray, UInt16>(info.width, info.height, info.width * sizeof(ushort), ptr).Copy();
-                        }
-                    }
-                    
-                    depthImage.ReleaseAccess(imageData);
-                    depthImage.Dispose();
-                  
-                    // confidence filter
-                    Image<Gray, byte> mask = dImg.InRange(new Gray(lowConfidenceValue), new Gray(lowConfidenceValue));
-                    dImg.SetValue(new Gray(Int16.MaxValue), mask);
-
-                    NewFrameEvent(this, new NewFrameEventArgs(CurrentFrameID, dImg));
-                }
-
-                CurrentFrameID += 1;
-
-                senseManager.ReleaseFrame();
-            }
+            return NewFrameEvent != null;
+        }
 
-            senseManager.Dispose();
+        internal void killAndRestart()
+        {
+            throw new NotImplementedException();
+        }
+
+        internal void newFrame(int currentFrameID, Image<Gray, ushort> dImg)
+        {
+            NewFrameEvent(this, new NewFrameEventArgs(currentFrameID, dImg));
         }
     }
 }

+ 133 - 0
bbiwarg/Input/InputProviding/IntelCameraHandler.cs

@@ -0,0 +1,133 @@
+using Emgu.CV;
+using Emgu.CV.Structure;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace BBIWARG.Input.InputProviding
+{
+    class IntelCameraWrapper : MarshalByRefObject
+    {
+        private Assembly assembly;
+        public InputProviderIntel inputProviderIntel;
+        private dynamic senseManager;
+        private dynamic session;
+        private dynamic AccessRead;
+        private dynamic PixelFormat;
+        private dynamic EverythingFine;
+        private MethodInfo acquireAccessMethod;
+        private object[] methodParams;
+        public bool errorstate = false;
+
+        public IntelCameraWrapper()
+        {
+           
+        }
+
+        public void init(InputProviderIntel intelInputProvider) {
+            inputProviderIntel = intelInputProvider;
+
+            var domain = System.AppDomain.CreateDomain("IntelCamDomain");
+            assembly = Assembly.Load("libpxcclr.cs");
+
+            AccessRead = (dynamic)Enum.Parse(assembly.GetType("PXCMImage+Access"), "ACCESS_READ");
+            PixelFormat = (dynamic)Enum.Parse(assembly.GetType("PXCMImage+PixelFormat"), "PIXEL_FORMAT_DEPTH");
+
+            EverythingFine = (dynamic)Enum.Parse(assembly.GetType("pxcmStatus"), "PXCM_STATUS_NO_ERROR");
+
+            // here be dragons
+            Type imageDataType = assembly.GetType("PXCMImage+ImageData");
+            Type refType = imageDataType.MakeByRefType();
+            Type[] types = new Type[] { AccessRead.GetType(), PixelFormat.GetType(), refType };
+            acquireAccessMethod = assembly.GetType("PXCMImage").GetMethod("AcquireAccess", types);
+
+            //depthImage.AcquireAccess(AccessRead, PixelFormat, out imageData);
+            methodParams = new object[] { AccessRead, PixelFormat, null };
+
+            session = assembly.GetType("PXCMSession").GetMethod("CreateInstance").Invoke(null, null);
+            senseManager = session.CreateSenseManager();
+
+            dynamic ddesc = Activator.CreateInstance(assembly.GetType("PXCMVideoModule+DataDesc"));
+            ddesc.deviceInfo.streams = (dynamic)Enum.Parse(assembly.GetType("PXCMCapture+StreamType"), "STREAM_TYPE_DEPTH");
+
+            senseManager.EnableStreams(ddesc);
+            var result = senseManager.Init();
+
+            dynamic fov = senseManager.captureManager.device.QueryDepthFieldOfView();
+            inputProviderIntel.FieldOfViewHorizontal = fov.x;
+            inputProviderIntel.FieldOfViewVertical = fov.y;
+            inputProviderIntel.lowConfidenceValue = senseManager.captureManager.device.QueryDepthLowConfidenceValue();
+            senseManager.captureManager.device.SetDepthConfidenceThreshold((UInt16)Parameters.ConfidenceImageMinThreshold);
+
+            Console.WriteLine("Started camera. Delivering frames...");
+        }
+
+        internal void run()
+        {
+            while (inputProviderIntel.IsActive)
+            {
+                var status = senseManager.AcquireFrame(true);
+
+                if (status != EverythingFine)
+                {
+                    Console.WriteLine("Stupid camera crashed with "+ status + ". Trying to recover...");
+                    errorstate = true;
+                    break;
+                }
+
+                if (inputProviderIntel.hasNewFrameEvent())
+                {
+
+                    dynamic sample = senseManager.QuerySample();
+                    dynamic depthImage = sample.depth;
+                    dynamic info = depthImage.info;
+
+
+
+                    acquireAccessMethod.Invoke(depthImage, methodParams);
+
+                    dynamic imageData = methodParams[2];
+
+                    ushort[] data = imageData.ToUShortArray(0, info.width * info.height);
+
+
+                    Image<Gray, UInt16> dImg;
+
+                    unsafe
+                    {
+                        fixed (ushort* d = data)
+                        {
+                            IntPtr ptr = (IntPtr)d;
+                            dImg = new Image<Gray, UInt16>(info.width, info.height, info.width * sizeof(ushort), ptr).Copy();
+                        }
+                    }
+
+                    depthImage.ReleaseAccess(imageData);
+                    depthImage.Dispose();
+
+                    // confidence filter
+                    Image<Gray, byte> mask = dImg.InRange(new Gray(inputProviderIntel.lowConfidenceValue), new Gray(inputProviderIntel.lowConfidenceValue));
+                    dImg.SetValue(new Gray(Int16.MaxValue), mask);
+
+                    inputProviderIntel.newFrame(inputProviderIntel.CurrentFrameID, dImg);
+                }
+
+                inputProviderIntel.CurrentFrameID += 1;
+
+                senseManager.ReleaseFrame();
+            }
+
+            senseManager.Dispose();
+        }
+
+        private void closeCamera()
+        {
+            senseManager.Close();
+            session.Dispose();
+        }
+    }
+}

+ 1 - 3
bbiwarg/bbiwarg.csproj

@@ -91,9 +91,6 @@
       <SpecificVersion>False</SpecificVersion>
       <HintPath>lib\iisuNet.dll</HintPath>
     </Reference>
-    <Reference Include="libpxcclr.cs">
-      <HintPath>..\..\..\..\..\..\Program Files (x86)\Intel\RSSDK\bin\win32\libpxcclr.cs.dll</HintPath>
-    </Reference>
     <Reference Include="MathNet.Numerics">
       <HintPath>lib\MathNet.Numerics.dll</HintPath>
     </Reference>
@@ -120,6 +117,7 @@
     <Compile Include="Input\InputHandling\NewProcessedFrameEventArgs.cs" />
     <Compile Include="Input\InputProviding\InputProviderIntel.cs" />
     <Compile Include="Input\InputProviding\IInputProvider.cs" />
+    <Compile Include="Input\InputProviding\IntelCameraHandler.cs" />
     <Compile Include="Input\InputProviding\NewFrameEventArgs.cs" />
     <Compile Include="Output\GlassesOutput\GlassesWindow.cs">
       <SubType>Form</SubType>