Skip to content

Commit

Permalink
stability issues fixed
Browse files Browse the repository at this point in the history
Argument exception handled.

Co-Authored-By: alpyigit <[email protected]>
Co-Authored-By: Abdurrahman Akın Aktaş <[email protected]>
  • Loading branch information
3 people committed Sep 13, 2018
1 parent ba4fd57 commit e4af0b9
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 77 deletions.
Binary file added KinectRecorder/.vs/KinectRecorder/v14/.suo
Binary file not shown.
19 changes: 17 additions & 2 deletions KinectRecorder/BodyIndexHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,23 @@ public void BodyIndexFrameArrival(BodyIndexFrame bif, ref bool frameProcessed, d
ProcessBodyIndexFrameData(bodyIndexBuffer.UnderlyingBuffer, bodyIndexBuffer.Size);
frameProcessed = true;
}

if (bodyRecording)
{
bBitmap = UtilityClass.ByteArrayToBitmap(bodyPixelBuffer, width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
Bitmap bitmapFrame;
try
{
bitmapFrame = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
}
catch(Exception e)
{
Console.WriteLine("Body Exception");
Console.WriteLine(e);
System.GC.Collect();
bitmapFrame = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
}
UtilityClass.ByteArrayToBitmap(ref bitmapFrame, bodyPixelBuffer, width, height);
bBitmap = bitmapFrame;
bodyBitmapBuffer.Enqueue(bBitmap);
//System.GC.Collect();
frameCount++;
Expand All @@ -153,6 +166,8 @@ public void BodyIndexFrameArrival(BodyIndexFrame bif, ref bool frameProcessed, d
frameCount++;
}
}


}

}
Expand Down
34 changes: 22 additions & 12 deletions KinectRecorder/ColorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ public void Write()
//Console.WriteLine("color");
if (colorBitmapBuffer.Count > 0)
{
//Console.WriteLine("3");
//Console.WriteLine(colorBitmapBuffer.Count);
this.colorWriter.WriteVideoFrame(colorBitmapBuffer.Dequeue());
//Console.WriteLine("4");
}
else if (!colorRecording)
{
Expand Down Expand Up @@ -117,7 +119,6 @@ public void setRecordingState(bool state)

public void ColorFrameArrival(ColorFrame colorFrame, ref WriteableBitmap colorBitmap, double fps)
{

using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
{
colorBitmap.Lock();
Expand All @@ -138,31 +139,40 @@ public void ColorFrameArrival(ColorFrame colorFrame, ref WriteableBitmap colorBi

colorFrame.CopyConvertedFrameDataToArray(colorPixelBuffer, ColorImageFormat.Bgra);


if (colorRecording)
{
cBitmap = UtilityClass.ByteArrayToBitmap(colorPixelBuffer, width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//Console.WriteLine("1");
Bitmap bitmapFrame;
try
{
bitmapFrame = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}

catch (Exception e)
{
Console.WriteLine("Color Exception");
Console.WriteLine(e);
System.GC.Collect();
bitmapFrame = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
UtilityClass.ByteArrayToBitmap(ref bitmapFrame, colorPixelBuffer, width, height);
cBitmap = bitmapFrame;
colorBitmapBuffer.Enqueue(cBitmap);
//Console.WriteLine("2");
frameCount++;
//garbageCount++;
if (fps < 16.0)
{
Console.WriteLine("fps droped");
colorBitmapBuffer.Enqueue(cBitmap);
frameCount++;
//garbageCount++;
}
/*
if (garbageCount > 100)
{
System.GC.Collect();
garbageCount = 0;
}
*/

}
}
}


//Console.WriteLine("5");
}


Expand Down
5 changes: 3 additions & 2 deletions KinectRecorder/DepthHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,19 @@ public void DepthFrameArrival(DepthFrame df, ref bool frameProcessed, double fps
garbageCount++;
this.depthBinaryBuffer.Enqueue((byte[])(depthPixelBuffer.Clone()));
this.frameCount++;

if (fps < 16.0)
{
garbageCount++;
Console.WriteLine("fps drop yaşandı");
this.depthBinaryBuffer.Enqueue((byte[])(depthPixelBuffer.Clone()));
this.frameCount++;
}
if(garbageCount > 500)
/*if(garbageCount > 500)
{
System.GC.Collect();
garbageCount = 0;
}
}*/

}

Expand Down
4 changes: 2 additions & 2 deletions KinectRecorder/InfraredHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public void InfraredFrameArrival(InfraredFrame df, double fps, ref bool processe
}
}
}
private Bitmap IRFrameToBitmap(InfraredFrame frame)
/*private Bitmap IRFrameToBitmap(InfraredFrame frame)
{
System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
Expand All @@ -264,7 +264,7 @@ private Bitmap IRFrameToBitmap(InfraredFrame frame)
}
return UtilityClass.ByteArrayToBitmap(pixelData, this.Width, this.Height, format);
}
}*/
private unsafe void ProcessInfraredFrameData(IntPtr infraredFrameData, uint infraredFrameDataSize)
{
// infrared frame data is a 16 bit value
Expand Down
14 changes: 8 additions & 6 deletions KinectRecorder/KinectRecorder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<NuGetPackageImportStamp>262bc50c</NuGetPackageImportStamp>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -56,6 +57,7 @@
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -88,8 +90,8 @@
<HintPath>..\packages\Accord.Video.3.8.0\lib\net45\Accord.Video.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Accord.Video.FFMPEG, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=x86">
<HintPath>..\packages\Accord.Video.FFMPEG.3.8.0\lib\net45\Accord.Video.FFMPEG.dll</HintPath>
<Reference Include="Accord.Video.FFMPEG.x64, Version=3.8.0.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7, processorArchitecture=AMD64">
<HintPath>..\packages\Accord.Video.FFMPEG.x64.3.8.0\lib\net45\Accord.Video.FFMPEG.x64.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="CsvHelper, Version=7.0.0.0, Culture=neutral, PublicKeyToken=8c4959082be5c823, processorArchitecture=MSIL">
Expand Down Expand Up @@ -196,12 +198,12 @@
<Import Project="..\packages\Accord.3.8.0\build\Accord.targets" Condition="Exists('..\packages\Accord.3.8.0\build\Accord.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Accord.3.8.0\build\Accord.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Accord.3.8.0\build\Accord.targets'))" />
<Error Condition="!Exists('..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets'))" />
<Error Condition="!Exists('..\packages\Accord.Video.FFMPEG.x64.3.8.0\build\Accord.Video.FFMPEG.x64.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Accord.Video.FFMPEG.x64.3.8.0\build\Accord.Video.FFMPEG.x64.targets'))" />
</Target>
<Import Project="..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets" Condition="Exists('..\packages\Accord.Video.FFMPEG.3.8.0\build\Accord.Video.FFMPEG.targets')" />
<Import Project="..\packages\Accord.Video.FFMPEG.x64.3.8.0\build\Accord.Video.FFMPEG.x64.targets" Condition="Exists('..\packages\Accord.Video.FFMPEG.x64.3.8.0\build\Accord.Video.FFMPEG.x64.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
2 changes: 0 additions & 2 deletions KinectRecorder/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
<Label x:Name="skeletalResolutionText" Content="Resolution: " HorizontalAlignment="Left" Height="21" Margin="10,3,0,0" Grid.Row="4" VerticalAlignment="Top" Width="200" FontSize="10" Grid.Column="1"/>
<Label x:Name="colorFpsText" Content="fps: " Height="21" Margin="0,8,10,0" Grid.Row="2" VerticalAlignment="Top" Width="86" FontSize="10" HorizontalAlignment="Right"/>

<Button Grid.Row="3" Content="Screenshot" Height="27" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,102,0,0" Click="ScreenshotButton_Click" Grid.Column="2" Width="126" />

<TextBlock x:Name="StatusTextBlock" Grid.Column="2" HorizontalAlignment="Left" Margin="10,213,0,0" Grid.Row="3" TextWrapping="Wrap" Text="Status" VerticalAlignment="Top" Height="83" Width="318"/>
<Button x:Name="recordBtn" Content="Start Recording" IsEnabled="False" Click="recordBtn_Click" Grid.Column="2" HorizontalAlignment="Left" Margin="208,102,0,0" Grid.Row="3" VerticalAlignment="Top" Width="122" Height="27"/>
<TextBlock x:Name="RecordingTextBlock" Grid.Column="2" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="3" TextWrapping="Wrap" Text="not Recording" VerticalAlignment="Top" Height="87" Width="318"/>
Expand Down
46 changes: 6 additions & 40 deletions KinectRecorder/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,46 +408,6 @@ private void writerHelper()

}

private void ScreenshotButton_Click(object sender, RoutedEventArgs e)
{
saveCapture();
}

// a image save with bitmap encoder
private void saveCapture()
{
if (this.colorBitmap != null)
{
// create a png bitmap encoder which knows how to save a .png file
BitmapEncoder encoder = new PngBitmapEncoder();

// create frame from the writable bitmap and add to encoder
encoder.Frames.Add(BitmapFrame.Create(this.colorBitmap));

string time = System.DateTime.Now.ToString("hh'-'mm'-'ss", CultureInfo.CurrentUICulture.DateTimeFormat);

string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

string path = System.IO.Path.Combine(myPhotos, "KinectScreenshot-Color-" + time + ".png");

// write the new file to disk
try
{
// FileStream is IDisposable
using (FileStream fs = new FileStream(path, FileMode.Create))
{
encoder.Save(fs);
}

this.StatusTextBlock.Text = string.Format("Screanshot saved at: {0}", path);
}
catch (IOException)
{
this.StatusTextBlock.Text = string.Format("Screanshot chouldn't saved at: {0}", path);
}
}
}

private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
// ColorFrame is IDisposable
Expand Down Expand Up @@ -503,7 +463,13 @@ private void Reader_DepthFrameArrived(object sender, DepthFrameArrivedEventArgs
depthHandler.Height.ToString());
}
}
/*if (depthHandler.frameCount != colorHandler.frameCount)
{
Console.WriteLine("aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
Console.WriteLine("color and depth frame count {0} {1}", colorHandler.frameCount,depthHandler.frameCount);
}*/
if (depthFrameProcessed && depthHandler.show)
{
RenderDepthPixels();
Expand Down
21 changes: 14 additions & 7 deletions KinectRecorder/SkeletonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,27 @@ public void closeReader()

public void Read()
{
using (DrawingContext dc = this.previewDrawingGroup.Open())
try
{
dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
using (DrawingContext dc = this.previewDrawingGroup.Open())
{
dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));

depthSkeleton = skeletons[skeletonCounter].getDepthValues();
depthSkeleton = skeletons[skeletonCounter].getDepthValues();

this.DrawPreviewBody(depthSkeleton, dc);
this.DrawPreviewBody(depthSkeleton, dc);

//this.DrawPreviewHand(body.HandLeftState, jointPoints[JointType.HandLeft], dc);
//this.DrawPreviewHand(body.HandRightState, jointPoints[JointType.HandRight], dc);
//this.DrawPreviewHand(body.HandLeftState, jointPoints[JointType.HandLeft], dc);
//this.DrawPreviewHand(body.HandRightState, jointPoints[JointType.HandRight], dc);



skeletonCounter++;
skeletonCounter++;
}
}
catch (Exception e){
Console.WriteLine("iskelet datası yok");
Console.WriteLine(e);
}

}
Expand Down
6 changes: 3 additions & 3 deletions KinectRecorder/UtilityClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ namespace KinectRecorder
{
class UtilityClass
{
internal static Bitmap ByteArrayToBitmap(byte[] array, int width, int height, System.Drawing.Imaging.PixelFormat pixelFormat)
internal static void ByteArrayToBitmap(ref Bitmap bitmapFrame, byte[] array, int width, int height)
{

Bitmap bitmapFrame = new Bitmap(width, height, pixelFormat);


System.Drawing.Imaging.BitmapData bitmapData = bitmapFrame.LockBits(new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmapFrame.PixelFormat);

IntPtr intPointer = bitmapData.Scan0;
Marshal.Copy(array, 0, intPointer, array.Length);

bitmapFrame.UnlockBits(bitmapData);
return bitmapFrame;


}
internal static BitmapImage BitmapToImageSource(Bitmap bitmap)
Expand Down
2 changes: 1 addition & 1 deletion KinectRecorder/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<packages>
<package id="Accord" version="3.8.0" targetFramework="net45" />
<package id="Accord.Video" version="3.8.0" targetFramework="net45" />
<package id="Accord.Video.FFMPEG" version="3.8.0" targetFramework="net45" />
<package id="Accord.Video.FFMPEG.x64" version="3.8.0" targetFramework="net45" />
<package id="CsvHelper" version="7.1.1" targetFramework="net45" />
</packages>

0 comments on commit e4af0b9

Please sign in to comment.