Page 1 of 1

C# / VeriLook SDK / Async & await

PostPosted: May 30, 2018 09:45
by micklepick
HI!
I`m trying to write async method for face detection which looks like :
Code: Select all
NBiometricClient nClient = new NBiometricClient();

public async Task<List<CustomObject>> DoDetection(byte[] image)
{
 List<CustomObject> result= new List<CustomObject>();
 NImage nImage = NImage.FromMemory(image);
 NFace nFace = await nClient.DetectFacesAsync(nImage);
 if (nFace != null && nFace.Objects.Any())
 {
  for (int i =0; i< nFace.Objects.Count ; i++)
  {
   NFace detectedFace = NFace.FromImageAndAttributes(nImage, new NLAttributes() { BoundingRect = nFace.Objects[i].BoundingRect };
   NSubject subject = new NSubject();
   subject.Faces.Add(detectedFace);
   NBiometricStatus status = await nClient.CreateTemplateAsync(subject);
   result.Add(new CustomObject(subject));// template goes here
  }
 }
return result;
}

If I`m calling this method without CreateTemplateAsync, only perform DetectFacesAsync, everything is fine, no limits in generating tasks.
But with CreateTemplateAsync, my method produces terrible memory leaks.
Is there any possible decision? I tried disposing NImage and NSubject objects manually, nClient.Clear(), but nothing changed.

Thanks, waiting for your reply.

Re: C# / VeriLook SDK / Async & await

PostPosted: Jun 11, 2018 12:56
by MartynasV
micklepick wrote:HI!
I`m trying to write async method for face detection which looks like :
Code: Select all
NBiometricClient nClient = new NBiometricClient();

public async Task<List<CustomObject>> DoDetection(byte[] image)
{
 List<CustomObject> result= new List<CustomObject>();
 NImage nImage = NImage.FromMemory(image);
 NFace nFace = await nClient.DetectFacesAsync(nImage);
 if (nFace != null && nFace.Objects.Any())
 {
  for (int i =0; i< nFace.Objects.Count ; i++)
  {
   NFace detectedFace = NFace.FromImageAndAttributes(nImage, new NLAttributes() { BoundingRect = nFace.Objects[i].BoundingRect };
   NSubject subject = new NSubject();
   subject.Faces.Add(detectedFace);
   NBiometricStatus status = await nClient.CreateTemplateAsync(subject);
   result.Add(new CustomObject(subject));// template goes here
  }
 }
return result;
}

If I`m calling this method without CreateTemplateAsync, only perform DetectFacesAsync, everything is fine, no limits in generating tasks.
But with CreateTemplateAsync, my method produces terrible memory leaks.
Is there any possible decision? I tried disposing NImage and NSubject objects manually, nClient.Clear(), but nothing changed.

Thanks, waiting for your reply.

Hello micklepick,

What do you mean by memory leaks? Does it crash with "Out of memory" message or just memory consumption grows a lot?
How much memory does this method consume?
Do you call this method only once or many times?
How many faces does that image contain?
Have you tried forcing garbage collector to see if it clears memory?(maybe it didn't activate)

You should use "using (){}" blocks to handle disposable objects(as it is done in our tutorials).

result.Add(new CustomObject(subject));// template goes here

NSubject is not template, if you want just template, then you have to call "subject.GetTempalte()" method.

nClient.Clear()

It clears local memory of NBiometricClient, so if you had any templates enrolled to NBiometricClient, then they would be stored in RAM and this method would remove them.

Re: C# / VeriLook SDK / Async & await

PostPosted: Jan 29, 2019 01:37
by zyunting
Hi,

This might be useful as well.You could use
Code: Select all
 deleteAsync(id)

to clear the memory of a particular subject, in case
Code: Select all
using () {}

does not provide enough flexibility.