C# / VeriLook SDK / Async & await

General information about Neurotechnology and its software

C# / VeriLook SDK / Async & await

Postby micklepick » May 30, 2018 09:45

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.
micklepick
 
Posts: 6
Joined: Mar 29, 2018 09:21

Re: C# / VeriLook SDK / Async & await

Postby MartynasV » Jun 11, 2018 12:56

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.
Martynas V
Neurotechnology
MartynasV
Neurotechnology
 
Posts: 397
Joined: Feb 27, 2015 11:24
Location: Vilnius, Lithuania


Return to Public

Who is online

Users browsing this forum: No registered users and 3 guests

cron