How to: mitigate latency when using the Face service

You may encounter latency when using the Face service. Latency refers to any kind of delay that occurs when communicating over a network. In general, possible causes of latency include:

  • The physical distance each packet must travel from source to destination.
  • Problems with the transmission medium.
  • Errors in routers or switches along the transmission path.
  • The time required by antivirus applications, firewalls, and other security mechanisms to inspect packets.
  • Malfunctions in client or server applications.

This topic talks about possible causes of latency specific to using the Azure Cognitive Services, and how you can mitigate these causes.

Note

Azure Cognitive Services do not provide any Service Level Agreement (SLA) regarding latency.

Possible causes of latency

Slow connection between the Cognitive Service and a remote URL

Some Azure Cognitive Services provide methods that obtain data from a remote URL that you provide. For example, when you call the DetectWithUrlAsync method of the Face service, you can specify the URL of an image in which the service tries to detect faces.

var faces = await client.Face.DetectWithUrlAsync("https://www.biography.com/.image/t_share/MTQ1MzAyNzYzOTgxNTE0NTEz/john-f-kennedy---mini-biography.jpg");

The Face service must then download the image from the remote server. If the connection from the Face service to the remote server is slow, that will impact the response time of the Detect method.

To mitigate this, consider storing the image in Azure Premium Blob Storage. For example:

var faces = await client.Face.DetectWithUrlAsync("https://csdx.blob.core.windows.net/resources/Face/Images/Family1-Daughter1.jpg");

Large upload size

Some Azure Cognitive Services provide methods that obtain data from a file that you upload. For example, when you call the DetectWithStreamAsync method of the Face service, you can upload an image in which the service tries to detect faces.

using FileStream fs = File.OpenRead(@"C:\images\face.jpg");
System.Collections.Generic.IList<DetectedFace> faces = await client.Face.DetectWithStreamAsync(fs, detectionModel: DetectionModel.Detection02);

If the file to upload is large, that will impact the response time of the DetectWithStreamAsync method, for the following reasons:

  • It takes longer to upload the file.
  • It takes the service longer to process the file, in proportion to the file size.

Mitigations:

var faces = await client.Face.DetectWithUrlAsync("https://csdx.blob.core.windows.net/resources/Face/Images/Family1-Daughter1.jpg");
  • Consider uploading a smaller file.
    • See the guidelines regarding input data for face detection and input data for face recognition.
    • For face detection, when using detection model DetectionModel.Detection01, reducing the image file size will increase processing speed. When using detection model DetectionModel.Detection02, reducing the image file size will only increase processing speed if the image file is smaller than 1920x1080.
    • For face recognition, reducing the face size to 200x200 pixels does not affect the accuracy of the recognition model.
    • The performance of the DetectWithUrlAsync and DetectWithStreamAsync methods also depends on how many faces are in an image. The Face service can return up to 100 faces for an image. Faces are ranked by face rectangle size from large to small.
    • If you need to call multiple service methods, consider calling them in parallel if your application design allows for it. For example, if you need to detect faces in two images to perform a face comparison:
var faces_1 = client.Face.DetectWithUrlAsync("https://www.biography.com/.image/t_share/MTQ1MzAyNzYzOTgxNTE0NTEz/john-f-kennedy---mini-biography.jpg");
var faces_2 = client.Face.DetectWithUrlAsync("https://www.biography.com/.image/t_share/MTQ1NDY3OTIxMzExNzM3NjE3/john-f-kennedy---debating-richard-nixon.jpg");
Task.WaitAll (new Task<IList<DetectedFace>>[] { faces_1, faces_2 });
IEnumerable<DetectedFace> results = faces_1.Result.Concat (faces_2.Result);

Slow connection between your compute resource and the Face service

If your computer has a slow connection to the Face service, that will impact the response time of service methods.

Mitigations:

  • When you create your Face subscription, make sure to choose the region closest to where your application is hosted.
  • If you need to call multiple service methods, consider calling them in parallel if your application design allows for it. See the previous section for an example.
  • If longer latencies impact the user experience, choose a timeout threshold (e.g. maximum 5s) before retrying the API call.

Next steps

In this guide, you learned how to mitigate latency when using the Face service. Next, learn how to scale up from existing PersonGroup and FaceList objects to LargePersonGroup and LargeFaceList objects, respectively.