DetectWithStreamAsync takes long time

asked 2021-04-27T09:53:17.123+00:00
yuan wong 1 Reputation point

I find a sample code that uses Azure Face API ->https://github.com/jernejk/RealTimeFaceApi

I found DetectFace time + Recognize Facetime is too long so I tried to optimize by cropping the image to only the face (with 10-30% padding) to have <80kB upload. Although the time has become shorter, it is not "real-time". Please refer to the below Log:

91657-11.png

This is the code segment↓

     private static async Task StartRecognizing(Mat image)  
            {  
                try  
                {  
                    // TODO: Optimize by cropping the image to only the face (with 10-30% padding) to have <50kB upload.  
                    var stream = image.ToMemoryStream();  
      
                    Console.WriteLine(DateTime.Now + ": Sending " + (stream.Length / 1024) + "kB to recognize face.");  
      
                    Stopwatch stopwatch = Stopwatch.StartNew();  
                    detectedFaces = await _faceClient.Face.DetectWithStreamAsync(stream, true, false);  
                    var faceIds = detectedFaces.Where(f => f.FaceId.HasValue).Select(f => f.FaceId.Value).ToList();  
                    
      
                    Console.WriteLine(DateTime.Now + ": Found " + faceIds.Count + " faces in " + stopwatch.ElapsedMilliseconds + "ms.");  
      
                    stopwatch.Stop();  
      
                    if (faceIds.Any())  
                    {  
                        stopwatch.Restart();  
      
                        var potentialUsers = await _faceClient.Face.IdentifyAsync(faceIds, FaceGroupId);  
      
                        stopwatch.Stop();  
      
                        Console.WriteLine(DateTime.Now + ": Recognized " + potentialUsers.Count + " candidates in " + stopwatch.ElapsedMilliseconds + "ms.");  
                        foreach (var candidate in potentialUsers.Select(u => u.Candidates.FirstOrDefault()))  
                        {  
                            var candidateName = await GetCandidateName(candidate?.PersonId);  
                            Console.WriteLine($"{DateTime.Now}: {candidateName} ({candidate?.PersonId})");  
                        }  
                    }  
                    else  
                    {  
                        Console.WriteLine(DateTime.Now + $": No clear shot on the faces.");  
                    }  
                }  
                catch (APIErrorException apiError)  
                {  
                    Console.WriteLine(DateTime.Now + ": Cognitive service error: " + apiError?.Body?.Error?.Message);  
                }  
                catch (Exception e)  
                {  
                    Console.WriteLine(DateTime.Now + ": Getting identity failed: " + e.ToString());  
                }  
      
                _faceRecognitionTask = null;  
            }  
  

 

What I want to know is
Does the speed of FaceAPI completely depend on network speed?

Azure Face API
Azure Face API
An Azure service that provides artificial intelligence algorithms that detect, recognize, and analyze human faces in images.
97 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. answered 2021-04-27T20:43:19.113+00:00
    GiftA-MSFT 11,091 Reputation points

    Hi, thanks for reaching out. Some possible causes for latency when using Face service include:

    • Large upload size
    • Slow connection between the Cognitive Service and a remote URL
    • Slow connection between your compute resource and the Face service

    Please refer to the following document for more details as well as ways to mitigate latency. Hope this helps.

    No comments