Kinect SDK 2.0: how to track one body - c#

I am using Kinect SDK 2.0 and I want to track just one skeleton.
How do I achieve this?
I already tried the FindClosestSkeleton method in HD Face Basics sample, but that stopped my program from updating every frame, and I didn't entirely understand it.
Can someone kindly explain and show me how to do this?

The following code is based on the BodyBasics-WPF sample.
public class KinectManager
{
// Active Kinect sensor
private KinectSensor kinectSensor = null;
// Reader for body frames
private BodyFrameReader bodyFrameReader = null;
// Array for the bodies
private Body[] bodies = null;
// index for the currently tracked body
private int bodyIndex;
// flag to asses if a body is currently tracked
private bool bodyTracked = false;
public KinectManager()
{
this.kinectSensor = KinectSensor.GetDefault();
// open the reader for the body frames
this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader();
// open the sensor
this.kinectSensor.Open();
this.bodyFrameReader.FrameArrived += this.Reader_FrameArrived;
}
// Handles the body frame data arriving from the sensor
private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
bool dataReceived = false;
using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
{
if (bodyFrame != null)
{
if (this.bodies == null)
{
this.bodies = new Body[bodyFrame.BodyCount];
}
bodyFrame.GetAndRefreshBodyData(this.bodies);
dataReceived = true;
}
}
if (dataReceived)
{
Body body = null;
if(this.bodyTracked) {
if(this.bodies[this.bodyIndex].IsTracked) {
body = this.bodies[this.bodyIndex];
} else {
bodyTracked = false;
}
}
if(!bodyTracked) {
for (int i=0; i<this.bodies.Length; ++i)
{
if(this.bodies[i].IsTracked) {
this.bodyIndex = i;
this.bodyTracked = true;
break;
}
}
}
if (body != null && this.bodyTracked && body.IsTracked)
{
// body represents your single tracked skeleton
}
}
}
}

Related

WasapiLoopbackCapture.DataAvailable reading silence bytes

I am trying to record the speaker sound to a wave file using NAudio's WasapiLoopbackCapture by writing the stream of bytes available. The WasapiLoopbackCapture.DataAvailable BytesRecorded will be 0 is there is no sound. however in my case i am getting bytecount in BytesRecorded even though the speakers are silent. could you please let me know whats wrong here.
class CallResponse
{
private WaveFileWriter _writer;
private WasapiLoopbackCapture _waveIn;
private string _inFile;
private string _inFileCompressed;
private int _duration;
public bool _isRecording;
public bool _speechDetected;
public CallResponse()
{
_inFile = #"C:\Naresh\test.wav";
_inFileCompressed = #"C:\Naresh\test16Hz.wav";
_waveIn = new WasapiLoopbackCapture();
_waveIn.DataAvailable += (s, e) =>
{
Console.WriteLine(e.BytesRecorded);
_writer.Write(e.Buffer, 0, e.BytesRecorded);
if (_writer.Position > _waveIn.WaveFormat.AverageBytesPerSecond * _duration)
{
Console.Write("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bRecording stopped...");
_waveIn.StopRecording();
}
};
_waveIn.RecordingStopped += (s, e) =>
{
if (_writer != null)
{
_writer.Close();
_writer.Dispose();
_writer = null;
}
Console.Write("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bCompressing Audio...");
using (var reader = new AudioFileReader(_inFile))
{
var resampler = new WdlResamplingSampleProvider(reader, 16000);
WaveFileWriter.CreateWaveFile16(_inFileCompressed, resampler);
}
_isRecording = false;
};
}
public void DisposeObjects()
{
if (_waveIn != null)
{
_waveIn.Dispose();
_waveIn = null;
}
}
public void StartRecording(int duration = 5)
{
_writer = new WaveFileWriter(_inFile, _waveIn.WaveFormat);
this._duration = duration;
_speechDetected = false;
_isRecording = true;
Console.WriteLine("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bRecording....");
_waveIn.StartRecording();
}
}
if something is playing audio, then WasapiLoopbackCapture will capture that audio, even if it contains silence. So there's nothing particularly wrong or surprising that you are getting non-zero BytesRecorded values. In fact, if no applications are sending audio to the device being captured, then what typically happens is that you won't get any DataAvailable callbacks at all.

Xamarin Android take screenshot

I am working on a project which makes drawing.
I don't use axml because I do my drawing in a class called filledpolygon and calling the function in MainActivity. I just want to take screenshot in my project. Is there any basic function, which I can call in onCreate method? So, when the program runs, it will automatically take the screenshot. I found answers except Xamarin platform.
Since Android 28 DrawingCacheEnabled is deprecated and without it we are forcing our view to to redraw on our custom canvas wich can cause artifacts with custom controls and renderers and the screenshot version might be different from what we see on screen.
The legacy code that is still working on simple cases is:
public byte[] CaptureScreenshot()
{
var view=
Xamarin.Essentials.Platform.CurrentActivity.Window.DecorView.RootView;
if (view.Height < 1 || view.Width < 1)
return null;
byte[] buffer = null;
view.DrawingCacheEnabled = true;
using (var screenshot = Bitmap.CreateBitmap(
view.Width,
view.Height,
Bitmap.Config.Argb8888))
{
var canvas = new Canvas(screenshot);
view.Draw(canvas);
using (var stream = new MemoryStream())
{
screenshot.Compress(Bitmap.CompressFormat.Png, 90, stream);
buffer = stream.ToArray();
}
}
view.DrawingCacheEnabled = false;
return buffer;
}
Use legacy method above as follows
if ((int)Android.OS.Build.VERSION.SdkInt < 28)
{
//legacy
}
The DrawingCacheEnabled obsolete warning redirects us to use PixelCopy. This method is acting with a callback so to use it synchronously have made some helpers:
Usage:
public byte[] CaptureScreenshot()
{
using var helper = new ScreenshotHelper(
Xamarin.Essentials.Platform.CurrentActivity.Window.DecorView.RootView,
Xamarin.Essentials.Platform.CurrentActivity);
byte[] buffer = null;
bool wait = true;
Task.Run(async () =>
{
helper.Capture((Bitmap bitmap) =>
{
try
{
if (!helper.Error)
{
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 90, stream);
buffer = stream.ToArray();
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
wait = false;
}
});
}).ConfigureAwait(false);
while (wait)
{
Task.Delay(10).Wait();
}
return buffer;
}
The helper:
public class ScreenshotHelper : Java.Lang.Object, PixelCopy.IOnPixelCopyFinishedListener
{
public void OnPixelCopyFinished(int copyResult)
{
var stop = true;
if (copyResult == (int) PixelCopyResult.Success)
{
Error = false;
//todo CallbackGotScreenshot();
_callback(_bitmap);
}
else
{
Error = true;
}
_callback(_bitmap);
}
public bool Error { get; protected set; }
public ScreenshotHelper(Android.Views.View view, Activity activity)
{
_view = view;
_activity = activity;
_bitmap = Bitmap.CreateBitmap(
_view.Width,
_view.Height,
Bitmap.Config.Argb8888);
}
// Starts a background thread and its {#link Handler}.
private void StartBackgroundThread()
{
_BackgroundThread = new HandlerThread("ScreeshotMakerBackground");
_BackgroundThread.Start();
_BackgroundHandler = new Handler(_BackgroundThread.Looper);
}
// Stops the background thread and its {#link Handler}.
private void StopBackgroundThread()
{
try
{
_BackgroundThread.QuitSafely();
_BackgroundThread.Join();
_BackgroundThread = null;
_BackgroundHandler = null;
}
catch (Exception e)
{
//e.PrintStackTrace();
}
}
public void Capture(Action<Bitmap> callback)
{
//var locationOfViewInWindow = new int[2];
//_view.GetLocationInWindow(locationOfViewInWindow);
_callback = callback;
try
{
StartBackgroundThread();
//todo could create-use background handler
PixelCopy.Request(_activity.Window, _bitmap, this,
_BackgroundHandler);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Task.Run(StopBackgroundThread);
}
}
private Android.Views.View _view;
private Activity _activity;
private Bitmap _bitmap;
private HandlerThread _BackgroundThread;
private Handler _BackgroundHandler;
private Action<Bitmap> _callback;
public new void Dispose()
{
_bitmap?.Dispose();
_bitmap= null;
_activity = null;
_view = null;
_callback = null;
base.Dispose();
}
}
In your View you could run the following code which will take a screenshot. I have not tried running it in OnCreate() before so you may need to test that out to make sure the view has been fully rendered.
*Edit: According to this post you may have trouble running this code in OnCreate() so you will need to find a better place. I was unable to figure out what post the user was referring to in the link he posted.
*Edit #2: Just found out that Compress() does not take the quality parameter (which is listed as 0 below) into account since PNG is lossless, but if you change the format to JPEG for example, then you may want to turn up the quality parameter since your image will look like garbage.
public byte[] SaveImage() {
DrawingCacheEnabled = true; //Enable cache for the next method below
Bitmap bitmap = GetDrawingCache(true); //Gets the image from the cache
byte[] bitmapData;
using(MemoryStream stream = new MemoryStream()) {
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
return bitmapData;
}

Unity with Naudio RadioStreaming

I'm working in a script based on the NAudio Demo modified for streaming a Shoutcast inside my Unity game.
I have tried to remove the original while loop using the update from the MonoBehvaiour class, I only get some noises but not music during the time I'm streaming with this script.
I usually get an error while the execution related with the format
MmException: AcmNotPossible calling acmStreamConvert
NAudio.MmException.Try (MmResult result, System.String function)
NAudio.Wave.Compression.AcmStreamHeader.Convert (Int32 bytesToConvert,
System.Int32& sourceBytesConverted)
NAudio.Wave.Compression.AcmStream.Convert (Int32 bytesToConvert,
System.Int32& sourceBytesConverted)
NAudio.Wave.AcmMp3FrameDecompressor.DecompressFrame
(NAudio.Wave.Mp3Frame frame, System.Byte[] dest, Int32 destOffset)
I have tried with different radios online, but I always get that error. I don't know what is happening... Any help?
public class NAudioStreamer : MonoBehaviour {
private IWavePlayer mWaveOutDevice;
private WaveStream mMainOutputStream;
private WaveChannel32 mVolumeStream;
private VolumeWaveProvider16 volumeProvider;
private string m_Url = "http://37.59.32.115:8122/";
enum StreamingPlaybackState
{
Stopped,
Playing,
Buffering,
Paused
}
private volatile StreamingPlaybackState playbackState = StreamingPlaybackState.Stopped;
private bool fullyDownloaded = false;
public bool m_Play = false;
float timer;
void Update()
{
if (m_Play)
{
playbackState = StreamingPlaybackState.Buffering;
StreamMP3(m_Url);
m_Play = false;
}
switch (playbackState)
{
case StreamingPlaybackState.Buffering:
case StreamingPlaybackState.Playing:
StreamMP3(m_Url);
break;
default:
break;
}
}
HttpWebRequest webRequest;
BufferedWaveProvider bufferedWaveProvider = null;
byte[] buffer = new byte[16384 * 4];
private void StreamMP3(string lUrl)
{
this.fullyDownloaded = false;
webRequest = (HttpWebRequest)WebRequest.Create(lUrl);
int metaInt = 0; // blocksize of mp3 data
webRequest.Headers.Clear();
webRequest.Headers.Add("GET", "/ HTTP/1.0");
webRequest.Headers.Add("Icy-MetaData", "1");
webRequest.UserAgent = "WinampMPEG/5.09";
HttpWebResponse resp = null;
try
{
resp = (HttpWebResponse)webRequest.GetResponse();
}
catch(WebException e)
{
if (e.Status != WebExceptionStatus.RequestCanceled)
{
Debug.LogError(e.Message);
}
return;
}
// needs to be big enough to hold a decompressed frame
try
{
// read blocksize to find metadata block
metaInt = Convert.ToInt32(resp.GetResponseHeader("icy-metaint"));
}
catch
{
}
IMp3FrameDecompressor decompressor = null;
try
{
using (var responseStream = resp.GetResponseStream())
{
ReadFullyStream readFullyStream = new ReadFullyStream(responseStream);
//do
{
if (bufferedWaveProvider != null && bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes < bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4)
{
Debug.LogError("Buffer getting full, taking a break");
Thread.Sleep(500);
}
else
{
Mp3Frame frame = null;
try
{
frame = Mp3Frame.LoadFromStream(readFullyStream, true);
}
catch (EndOfStreamException)
{
this.fullyDownloaded = true;
Debug.LogError("reached the end of the MP3 file / stream");
// reached the end of the MP3 file / stream
// break;
}
catch (WebException)
{
// probably we have aborted download from the GUI thread
// break;
}
if (decompressor == null && frame != null)
{
// don't think these details matter too much - just help ACM select the right codec
// however, the buffered provider doesn't know what sample rate it is working at
// until we have a frame
WaveFormat waveFormat = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate);
decompressor = new AcmMp3FrameDecompressor(waveFormat);
if(bufferedWaveProvider == null)
{
this.bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat);
this.bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20); // allow us to get well ahead of ourselves
}
}
int decompressed = decompressor.DecompressFrame(frame, buffer, 0);
if(bufferedWaveProvider != null)
{
bufferedWaveProvider.AddSamples(buffer, 0, decompressed);
}
}
}
if (this.mWaveOutDevice == null && this.bufferedWaveProvider != null)
{
Debug.Log("Creating WaveOut Device");
this.mWaveOutDevice = new WaveOut();
this.volumeProvider = new VolumeWaveProvider16(bufferedWaveProvider);
this.volumeProvider.Volume = 100.0f;
mWaveOutDevice.Init(volumeProvider);
}
else if (bufferedWaveProvider != null)
{
double bufferedSeconds = bufferedWaveProvider.BufferedDuration.TotalSeconds;
if(bufferedSeconds > 0.2f && playbackState == StreamingPlaybackState.Buffering)
{
Debug.Log("PLaying music...");
mWaveOutDevice.Play();
playbackState = StreamingPlaybackState.Playing;
}
}
}
}
finally
{
if (decompressor != null)
{
decompressor.Dispose();
}
}
}
}
The ACM error either means there is no ACM MP3 decoder on the machine, or that possibly a corrupt frame has been received (or some album art misinterpreted as a frame). It it's the latter, you can just catch the error and ignore it. If the former, you'll need to install a decoder, or use a different MP3 frame decompressor. (Possibly the NLayer one).

Windows Mobile application with symbol scanner dll crash at close application

I have a question that I cant come up with a solution for. Its About my application on a motorola MC3190-G Handheld Application on wm 6.5 and Compact Framework 2.0
After implementation of the Symbol Package for using the scanner trigger inside my application, I get the following error everytime I close my application / or a form which has the scanner enabled:
(Even if I never received data from scanning)
I searched for a solution on stackoverflow and other sites with no solutions to fix it.
I am new to mobile application development. I try and catched everything but the error seem to can't be catched with exceptiontype "exception".
sorry for the poor english.
Error:
NullReferenceException
at MainForm.SymbolReader_ReadNotifiy() at Task.Invoke()
at System.Windows.Forms.Control._InvokeAll() at
System.Windows.Forms.Control.WinProc()
Informations:
Motorola EMDK 2.6
Device: MC3190-G
Sourcecode:
Scanner.cs
namespace MyScann
{
/// <summary>
/// Summary description for Scanner.
/// </summary>
public class Scanner
{
public static Symbol.Barcode.Reader SymbolReader = null;
public static Symbol.Barcode.ReaderData SymbolReaderData = null;
public static System.EventHandler SymbolEventHandler = null;
public static DataSet CodeDataSet;
//public static Utils.Sound ReadErrorSound;
public static bool ScannerEnabled = false;
public static void ActivateScanner()
{
// If we have both a reader and a reader-data Object
if ( Scanner.SymbolReader != null &&
Scanner.SymbolReaderData != null &&
! Scanner.SymbolReaderData.IsPending )
{
// Submit 'Read'
try
{
Scanner.SymbolReader.Actions.Read(Scanner.SymbolReaderData);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
if ( err.InnerException != null )
MessageBox.Show(err.InnerException.Message);
}
}
}
public static void DeactivateScanner()
{
// If we have both a reader and a reader-data Object
if ( Scanner.SymbolReader != null &&
Scanner.SymbolReaderData != null &&
Scanner.SymbolReaderData.IsPending )
{
// Submit 'CancelRead'
try
{
Scanner.SymbolReader.Actions.CancelRead(Scanner.SymbolReaderData);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
if ( err.InnerException != null )
MessageBox.Show(err.InnerException.Message);
}
}
}
/// <summary>
/// Initialize the reader.
/// </summary>
/// <returns>False if an error accurs</returns>
public static bool InitSymbolReader()
{
// If reader is already present then fail initialize
if ( SymbolReader != null )
{
return false;
}
// Create new reader, first available reader will be used.
SymbolReader = new Symbol.Barcode.Reader();
// Create reader data
SymbolReaderData = new Symbol.Barcode.ReaderData(
Symbol.Barcode.ReaderDataTypes.Text,
Symbol.Barcode.ReaderDataLengths.DefaultText);
// set scanner read error sound
//ReadErrorSound = new Utils.Sound(ErrorSound);
return true;
}
/// <summary>
/// Stop reading and disable/close reader
/// </summary>
public static void DeinitSymbolReader()
{
try
{
// If we have a reader
if ( SymbolReader != null )
{
//Cancel Incoming Requests
SymbolReader.Actions.Flush();
// Disable the reader
SymbolReader.Actions.Disable();
// Free it up
SymbolReader.Dispose();
// Indicate we no longer have one
SymbolReader = null;
}
// If we have a reader data
if ( SymbolReaderData != null )
{
// Free it up
SymbolReaderData.Dispose();
// Indicate we no longer have one
SymbolReaderData = null;
}
if(SymbolEventHandler!=null){
//SymbolEventHandler(this,null);
SymbolEventHandler = null;
}
}
catch (Exception e){MessageBox.Show(e.StackTrace.ToString());
}
}
/// <summary>
/// Enable most barcode type with no limitation on length
/// </summary>
public static void OpenAllBarcodes()
{
SymbolReader.Decoders.CODABAR.Enabled = true;
SymbolReader.Decoders.CODABAR.MinimumLength = 0;
SymbolReader.Decoders.CODABAR.MaximumLength = 0;
SymbolReader.Decoders.CODE11.Enabled = true;
SymbolReader.Decoders.CODE11.MinimumLength = 0;
SymbolReader.Decoders.CODE11.MaximumLength = 0;
SymbolReader.Decoders.CODE39.Enabled = true;
SymbolReader.Decoders.CODE39.MinimumLength = 0;
SymbolReader.Decoders.CODE39.MaximumLength = 0;
SymbolReader.Decoders.CODE93.Enabled = true;
SymbolReader.Decoders.CODE93.MinimumLength = 0;
SymbolReader.Decoders.CODE93.MaximumLength = 0;
SymbolReader.Decoders.CODE128.Enabled = true;
SymbolReader.Decoders.CODE128.MinimumLength = 0;
SymbolReader.Decoders.CODE128.MaximumLength = 0;
SymbolReader.Decoders.DATAMATRIX.Enabled = true;
SymbolReader.Decoders.DATAMATRIX.MinimumLength = 0;
SymbolReader.Decoders.DATAMATRIX.MaximumLength = 0;
SymbolReader.Decoders.D2OF5.Enabled = true;
SymbolReader.Decoders.D2OF5.MinimumLength = 0;
SymbolReader.Decoders.D2OF5.MaximumLength = 0;
SymbolReader.Decoders.I2OF5.Enabled = true;
SymbolReader.Decoders.I2OF5.MinimumLength = 0;
SymbolReader.Decoders.I2OF5.MaximumLength = 0;
/*
SymbolReader.Decoders.EAN13.Enabled = true;
SymbolReader.Decoders.EAN13.MinimumLength = 0;
SymbolReader.Decoders.EAN13.MaximumLength = 0;
SymbolReader.Decoders.EAN8.Enabled = true;
SymbolReader.Decoders.EAN8.MinimumLength = 0;
SymbolReader.Decoders.EAN8.MaximumLength = 0;
SymbolReader.Decoders.PDF417.Enabled = true;
SymbolReader.Decoders.PDF417.MinimumLength = 0;
SymbolReader.Decoders.PDF417.MaximumLength = 0;
SymbolReader.Decoders.UPCA.Enabled = true;
SymbolReader.Decoders.UPCA.MinimumLength = 0;
SymbolReader.Decoders.UPCA.MaximumLength = 0;
SymbolReader.Decoders.UPCE1.Enabled = true;
SymbolReader.Decoders.UPCE1.MinimumLength = 0;
SymbolReader.Decoders.UPCE1.MaximumLength =0;
SymbolReader.Decoders.UPCE0.Enabled = true;
SymbolReader.Decoders.UPCE0.MinimumLength = 0;
SymbolReader.Decoders.UPCE0.MaximumLength = 0;
*/
SymbolReader.Parameters.CodeIdType = Symbol.Barcode.CodeIdTypes.None;
SymbolReader.Parameters.ScanType = Symbol.Barcode.ScanTypes.Background;
SymbolReader.Parameters.LocalFeedback = Symbol.Barcode.DisabledEnabled.Enabled;
Scanner.SymbolReader.Parameters.Feedback.Success.BeepTime = 0;
}
}
}
}
Inside MainForm.cs
public void InitScanner(byte BatteryLevel)
{
// Create event handler delegate
if (Scanner.MyScann.Scanner.SymbolEventHandler == null)
{
Scanner.MyScann.Scanner.SymbolEventHandler = new EventHandler(this.SymbolReader_ReadNotify);
// Enable reader, with wait cursor
Scanner.MyScann.Scanner.SymbolReader.Actions.Enable();
Scanner.MyScann.Scanner.OpenAllBarcodes();
Scanner.MyScann.Scanner.ScannerEnabled = true;
}
// If we have both a reader and a reader data
if ((Scanner.MyScann.Scanner.SymbolReader != null) &&
(Scanner.MyScann.Scanner.SymbolReaderData != null))
{
// Submit a read
Scanner.MyScann.Scanner.SymbolReader.ReadNotify += Scanner.MyScann.Scanner.SymbolEventHandler;
Scanner.MyScann.Scanner.SymbolReader.Actions.Read(Scanner.MyScann.Scanner.SymbolReaderData);
}
}
public void SymbolReader_ReadNotify(object sender, EventArgs e)
{
Symbol.Barcode.ReaderData TheReaderData = Scanner.MyScann.Scanner.SymbolReader.GetNextReaderData();
if (TheReaderData.Result == Symbol.Results.SUCCESS /*&& (txtBarcode.Focused == true)*/)
{
// if (txtBarcode.Focused == true)
// {
// txtBarcode.Text = TheReaderData.Text.ToString();
MessageBox.Show(TheReaderData.Text.ToString());
SymbolReader_CycleScannerReader();
return;
// }
}
SymbolReader_CycleScannerReader();
}
public void SymbolReader_CycleScannerReader()
{
Scanner.MyScann.Scanner.SymbolReader.Actions.Read(Scanner.MyScann.Scanner.SymbolReaderData);
}
public void StartScanner()
{
bool flag = false;
try
{
Scanner.MyScann.Scanner.SymbolEventHandler = null;
Scanner.MyScann.Scanner.DeinitSymbolReader();
Scanner.MyScann.Scanner.ScannerEnabled = false;
if (!Scanner.MyScann.Scanner.ScannerEnabled)
{
Scanner.MyScann.Scanner.SymbolEventHandler = null;
Scanner.MyScann.Scanner.InitSymbolReader();
InitScanner(100);
flag = true;
}
}
catch
{
Scanner.MyScann.Scanner.SymbolEventHandler = null;
Scanner.MyScann.Scanner.InitSymbolReader();
InitScanner(100);
flag = true;
}
finally
{
if (!flag)
{
MessageBox.Show("Scanner Error");
}
}
}
void public void CloseScanner()
{
Scanner.MyScann.Scanner.SymbolEventHandler = null;
Scanner.MyScann.Scanner.DeinitSymbolReader();
}
void MainFormLoad(object sender, EventArgs e)
{
StartScanner();
}
void MainFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
CloseScanner();
}
void ButtonExitClick(object sender, EventArgs e)
{
Scanner.MyScann.Scanner.DeinitSymbolReader();
this.Close();
Application.Exit();
}
Any help would be appreciated!
I'm not familiar with the symbol library, but from what i can see the problem is the ReadNotify event being raised at some point after the "deinitialization" of the scanner.
What you should do in the MainForm.CloseScanner() void is to first remove ALL handles added in the MainForm.InitScanner() and then call Scanner.DeinitSymbolReader().
Edit:
You should remove the handle with the -= operator, not assigning null reference.
Hope this helps.
try not calling 'DeinitSymbolReader' on exit, and see if that fixes it
Pete
My impression is that your application doesn't exit correctly with or without 'deinitSymbolReader'. It is possible that by calling deinitSymbolReader on a resource that has already been closed you are causing an access violation.
Maybe you should try giving up deinintSymbolReader, and try to also Dispose the scanner/trigger in the Form's Disposing Event.
What seems to be the problem is that you are Disposing of complex data structures, and right that you're changing them to null which basically means you're changing the reference and probably ensuring the GC will never get to free all the un-managed resources.
One indication that you are dealing with the improper manipulation of an un-managed resource is the fact that the second deinintSymbolReader shouldn't do anything (since all your resources are null) much less cause an error.
I have an application that uses triggers and scanner and it only needs an "enableScanner = false" and an "Scanner.Dispose()" if the scanner is not null.

WPF application very slow - Kinect SDK 1.7

I have one simple application for Kinect but it seems it consumes a lot of resources. It works normally for a 1-2 minutes and then lag becomes unbearable.
This is my configuration:
Intel Core i3 CPU M330 2.13 GHz
4 GB RAM
ATI Radeon HD 4570
This is code for application window:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.WindowState = System.Windows.WindowState.Maximized;
}
private void StopKinect(KinectSensor sensor)
{
if (sensor != null)
{
if (sensor.IsRunning)
{
//stop sensor
sensor.Stop();
//stop audio if not null
if (sensor.AudioSource != null)
{
sensor.AudioSource.Stop();
}
}
}
}
private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
{
StopKinect(Generics.GlobalKinectSensorChooser.Kinect);
}
}
This is code for main menu (first screen):
public partial class MainMenu : Page
{
#region "Kinect"
private KinectSensorChooser sensorChooser;
#endregion
public MainMenu()
{
this.InitializeComponent();
if (Generics.GlobalKinectSensorChooser == null)
{
// initialize the sensor chooser and UI
this.sensorChooser = new KinectSensorChooser();
this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
this.sensorChooserUi.KinectSensorChooser = this.sensorChooser;
this.sensorChooser.Start();
Generics.GlobalKinectSensorChooser = this.sensorChooser;
}
else
{ // initialize the sensor chooser and UI
this.sensorChooser = new KinectSensorChooser();
this.sensorChooser = Generics.GlobalKinectSensorChooser;
this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
this.sensorChooserUi.KinectSensorChooser = sensorChooser;
}
// Bind the sensor chooser's current sensor to the KinectRegion
var regionSensorBinding = new Binding("Kinect") { Source = this.sensorChooser };
BindingOperations.SetBinding(this.kinectRegion, KinectRegion.KinectSensorProperty, regionSensorBinding);
}
private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args)
{
bool error = false;
if (args.OldSensor != null)
{
try
{
args.OldSensor.DepthStream.Range = DepthRange.Default;
args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false;
args.OldSensor.DepthStream.Disable();
args.OldSensor.SkeletonStream.Disable();
args.OldSensor.ColorStream.Disable();
}
catch (InvalidOperationException)
{
// KinectSensor might enter an invalid state while enabling/disabling streams or stream features.
// E.g.: sensor might be abruptly unplugged.
error = true;
}
}
if (args.NewSensor != null)
{
try
{
args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
args.NewSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
args.NewSensor.SkeletonStream.Enable();
}
catch (InvalidOperationException)
{
error = true;
}
}
if (!error)
kinectRegion.KinectSensor = args.NewSensor;
}
private void Screen1ButtonOnClick(object sender, RoutedEventArgs e)
{
this.sensorChooser.KinectChanged -= SensorChooserOnKinectChanged;
(Application.Current.MainWindow.FindName("_mainFrame") as Frame).Source = new Uri("Depth.xaml", UriKind.Relative);
}
}
And this code for screen1:
public partial class Depth : Page
{
#region "Kinect"
private KinectSensorChooser sensorChooser;
#endregion
const float MaxDepthDistance = 4095; // max value returned
const float MinDepthDistance = 850; // min value returned
const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDistance;
public Depth()
{
this.InitializeComponent();
// initialize the sensor chooser and UI
this.sensorChooser = new KinectSensorChooser();
//Assign the sensor chooser with the sensor chooser from the mainwindow.
//We are reusing the sensorchoosing declared in the first window that can in contact with kinect
this.sensorChooser = Generics.GlobalKinectSensorChooser;
//subscribe to the sensorChooserOnKinectChanged event
this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
//Assign Kinect Sensorchooser to the sensorchooser we got from our static class
this.sensorChooserUi.KinectSensorChooser = sensorChooser;
// Bind the sensor chooser's current sensor to the KinectRegion
var regionSensorBinding = new Binding("Kinect") { Source = this.sensorChooser };
BindingOperations.SetBinding(this.kinectRegion, KinectRegion.KinectSensorProperty, regionSensorBinding);
}
private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args)
{
bool error = false;
if (args.OldSensor != null)
{
try
{
args.OldSensor.DepthStream.Range = DepthRange.Default;
args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false;
args.OldSensor.DepthStream.Disable();
args.OldSensor.SkeletonStream.Disable();
args.OldSensor.ColorStream.Disable();
}
catch (InvalidOperationException)
{
error = true;
}
}
if (args.NewSensor != null)
{
try
{
args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
args.NewSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
args.NewSensor.SkeletonStream.Enable();
}
catch (InvalidOperationException)
{
error = true;
}
}
if (!error)
kinectRegion.KinectSensor = args.NewSensor;
}
private void MenuButtonOnClick(object sender, RoutedEventArgs e)
{
//Unsubscribe to the sensorchooser's event SensorChooseronkinectChanged
this.sensorChooser.KinectChanged -= SensorChooserOnKinectChanged;
(Application.Current.MainWindow.FindName("_mainFrame") as Frame).Source = new Uri("MainScreen.xaml", UriKind.Relative);
}
}
There are a couple of more screens, I choosed this screen where I'm using KinectDepthViewer from WpfViewers. This screen has the worst lag, application is unusable almost instantly. Other screens where I have just buttons don't have a lag at the beginning but they get it after 2-3 minutes of usage.
I don't know am I doing something wrong in initialization maybe. I hope someone has some advice. I've read that people didn't have problems with development on even weaker configurations so I hope it's not because of that.
Thank you!
your graphics adapter suggests you're using a notebook computer. While there might be something in your code that could be improved, I really think the problem is your hardware. Notebooks with the specs you listed will have trouble running something as CPU intensive as what you're trying to do with the Kinect.

Categories

Resources