Florian Mueller hace 8 años
padre
commit
58a79ae58e
Se han modificado 3 ficheros con 114 adiciones y 39 borrados
  1. BIN
      .vs/bbiwarg/v14/.suo
  2. 105 36
      bbiwarg/Input/InputProviding/InputProviderIntel.cs
  3. 9 3
      bbiwarg/bbiwarg.csproj

BIN
.vs/bbiwarg/v14/.suo


+ 105 - 36
bbiwarg/Input/InputProviding/InputProviderIntel.cs

@@ -59,6 +59,18 @@ namespace BBIWARG.Input.InputProviding
         }
 
         public void start()
+        {
+            initCameraDevice();
+
+            IsActive = true;
+
+            if (DeviceStartedEvent != null && CurrentFrameID == 0)
+                DeviceStartedEvent(this, new EventArgs());
+
+            keepActive();
+        }
+
+        private void initCameraDevice()
         {
             senseManager = PXCMSenseManager.CreateInstance();
             PXCMVideoModule.DataDesc ddesc = new PXCMVideoModule.DataDesc();
@@ -70,21 +82,73 @@ namespace BBIWARG.Input.InputProviding
             PXCMPointF32 fov = senseManager.captureManager.device.QueryDepthFieldOfView();
             FieldOfViewHorizontal = fov.x;
             FieldOfViewVertical = fov.y;
-            
+
             lowConfidenceValue = senseManager.captureManager.device.QueryDepthLowConfidenceValue();
             senseManager.captureManager.device.SetDepthConfidenceThreshold((UInt16)Parameters.ConfidenceImageMinThreshold);
+        }
 
-            IsActive = true;
+        public void stop()
+        {
+            IsActive = false;
+        }
 
-            if (DeviceStartedEvent != null)
-                DeviceStartedEvent(this, new EventArgs());
+        bool crashed = false;
+
+        protected void keepActive() {
+            
 
-            run();
+            while (IsActive) {
+
+                if (crashed)
+                {
+                    Console.WriteLine("Trying to reinitialize the camera...");
+                    Task<bool> initCameraTask = Task.Run(() => restartCamera());
+                    crashed = !initCameraTask.Wait(TimeSpan.FromSeconds(5)) || !initCameraTask.Result;
+                }
+                else {
+                    //try to acquire new frame...
+                    Task acquireFrameTask = Task.Run(() => acquireNewFrame());
+                    //wait a second...
+                    if (acquireFrameTask.Wait(TimeSpan.FromSeconds(1)))
+                    {
+                        //that worked, we should have a new frame by now...
+                        handleNewFrame();
+                        senseManager.ReleaseFrame();
+                    }
+                    else
+                    {
+                        //cam crashed. try to dispose and start again.
+                        crashed = true;
+                    }
+                }
+            }
         }
 
-        public void stop()
+        private bool restartCamera()
         {
-            IsActive = false;
+            try
+            {
+                crashed = false;
+                senseManager.Dispose();
+                initCameraDevice();
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+
+            return true;
+        }
+
+        protected bool acquireNewFrame() {
+            var status = senseManager.AcquireFrame(true);
+
+            if (status != pxcmStatus.PXCM_STATUS_NO_ERROR) {
+                Console.WriteLine("Cam crashed with status" + status);
+                return false;
+            }
+
+            return true;
         }
 
         protected void run()
@@ -95,42 +159,47 @@ namespace BBIWARG.Input.InputProviding
 
                 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));
+                    handleNewFrame();
                 }
 
-                CurrentFrameID += 1;
 
                 senseManager.ReleaseFrame();
             }
 
             senseManager.Dispose();
         }
+
+        private void handleNewFrame()
+        {
+            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;
+        }
     }
 }

+ 9 - 3
bbiwarg/bbiwarg.csproj

@@ -220,14 +220,20 @@
     <None Include="App.config">
       <SubType>Designer</SubType>
     </None>
-    <None Include="libpxccpp2c.dll.signature" />
+    <None Include="libpxccpp2c.dll.signature">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </None>
   </ItemGroup>
   <ItemGroup>
     <Content Include="cudart32_55.dll">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </Content>
-    <Content Include="libpxcclr.cs.dll" />
-    <Content Include="libpxccpp2c.dll" />
+    <Content Include="libpxcclr.cs.dll">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
+    <Content Include="libpxccpp2c.dll">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </Content>
     <Content Include="opencv_core290.dll">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </Content>