Azure Face Recognition Service

Image from https://www.pexels.com/@cottonbro/
Image from https://www.pexels.com/@cottonbro/

In this blog, we looked at the Azure Face Recognition Python library. The Azure Face Recognition service is part of Azure Cognitive Service. Please follow this blog to create the cognitive service.

The Python code can be found in a gist.

The main part of the code is

def detect(image_url: str):
    detected_faces = face_client.face.detect_with_url(
        url=image_url,
        return_face_attributes=[
            "age",
            "gender",
            "headPose",
            "smile",
            "facialHair",
            "glasses",
            "emotion",
            "hair",
            "makeup",
            "occlusion",
            "accessories",
            "blur",
            "exposure",
            "noise"
        ])

    if detected_faces:
        print(json.dumps([info(face) for face in detected_faces], indent=4))
        response = requests.get(image_url)
        img = Image.open(BytesIO(response.content))

        draw = ImageDraw.Draw(img)
        for face in detected_faces:
            draw.rectangle(getRectangle(face), outline="red", width=3)

        img.show()

We get the face attribute and then draw a rectangle around the face. We tested with https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg.

and got


[
    {
        "face_id": "0eb2cc83-baa0-4398-8365-7aa60c40ad11",
        "face_attributes": {
            "age": 25.0,
            "gender": "female",
            "smile": 1.0,
            "facial_hair": {
                "moustache": 0.0,
                "beard": 0.0,
                "sideburns": 0.0
            },
            "glasses": "readingGlasses",
            "head_pose": {
                "roll": -13.6,
                "yaw": 5.0,
                "pitch": -12.0
            },
            "emotion": {
                "anger": 0.0,
                "contempt": 0.0,
                "disgust": 0.0,
                "fear": 0.0,
                "happiness": 1.0,
                "neutral": 0.0,
                "sadness": 0.0,
                "surprise": 0.0
            },
            "hair": {
                "bald": 0.1,
                "invisible": false,
                "hair_color": [
                    {
                        "color": "brown",
                        "confidence": 0.99
                    },
                    {
                        "color": "black",
                        "confidence": 0.45
                    },
                    {
                        "color": "blond",
                        "confidence": 0.42
                    },
                    {
                        "color": "red",
                        "confidence": 0.41
                    },
                    {
                        "color": "gray",
                        "confidence": 0.14
                    },
                    {
                        "color": "other",
                        "confidence": 0.12
                    },
                    {
                        "color": "white",
                        "confidence": 0.0
                    }
                ]
            },
            "makeup": {
                "eye_makeup": true,
                "lip_makeup": true
            },
            "occlusion": {
                "forehead_occluded": false,
                "eye_occluded": false,
                "mouth_occluded": false
            },
            "accessories": [
                {
                    "type": "glasses",
                    "confidence": 1.0
                }
            ],
            "blur": {
                "blur_level": "Low",
                "value": 0.0
            },
            "exposure": {
                "exposure_level": "GoodExposure",
                "value": 0.48
            },
            "noise": {
                "noise_level": "Low",
                "value": 0.11
            },
            "mask": null,
            "quality_for_recognition": null
        }
    }
]

The result is pretty good. I also tested with partial faces. and I got


It is unable to recognize one of these faces. It is evident that the black coverage interfered with the recognition heuristic.

Lastly, I tested it with faces of different ages, and it works perfectly well.


Summary

It is easy to use the Python library and the result is astonishing :-)


Comments

Popular posts from this blog

OpenAI: Functions Feature in 2023-07-01-preview API version

Storing embedding in Azure Database for PostgreSQL

Happy New Year, 2024 from DALL-E