Face Detection Rest API RRS feed

  • Question

  • Hi,

         my name is Saad Yousafi, I am trying to use Face Detection API from Cognitive Services in my college project where a program uses camera to take picture but I could not find any information about how can I use an image taken form the camera, in body field of Face Detect API post request. Please let me know if it is possible, I will be grateful. Thank you.


    Sunday, November 3, 2019 12:33 AM

All replies

  • Hi Saad,

    For using image from camera, you may want to save it in your local first and then use it. The code for local image is as following:

    import requests
    import matplotlib.pyplot as plt
    from PIL import Image
    from matplotlib import patches
    from io import BytesIO
    import os

    # If you are using a Jupyter notebook, uncomment the following line.
    #%matplotlib inline

    # Replace <Subscription Key> with your valid subscription key.
    subscription_key = "Type_in_Subscription_key_please" #DSFace API

    # Set image path from local file.
    image_path = os.path.join('Specify the image path on your local machine')

    assert subscription_key

    # You must use the same region in your REST call as you used to get your
    # subscription keys. For example, if you got your subscription keys from
    # westus, replace "westcentralus" in the URI below with "westus".
    # Free trial subscription keys are generated in the westcentralus region.
    # If you use a free trial subscription key, you shouldn't need to change
    # this region.
    face_api_url = ''

    image_data = open(image_path, "rb")

    headers = {'Content-Type': 'application/octet-stream',
               'Ocp-Apim-Subscription-Key': subscription_key}
    params = {
        'returnFaceId': 'true',
        'returnFaceLandmarks': 'false',
        'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,' +

    response =, params=params, headers=headers, data=image_data)
    faces = response.json()

    # Display the original image and overlay it with the face information.
    image_read = open(image_path, "rb").read()
    image =

    plt.figure(figsize=(8, 8))
    ax = plt.imshow(image, alpha=1)
    for face in faces:
        fr = face["faceRectangle"]
        fa = face["faceAttributes"]
        origin = (fr["left"], fr["top"])
        p = patches.Rectangle(
            origin, fr["width"], fr["height"], fill=False, linewidth=2, color='b')
        plt.text(origin[0], origin[1], "%s, %d"%(fa["gender"].capitalize(), fa["age"]),
                 fontsize=20, weight="bold", va="bottom")
    _ = plt.axis("off")


    Please let me know if you have any further challenge. Thanks.



    Monday, November 4, 2019 3:58 AM