Embed Face API Results in Power BI

ByDr. SubraMANI Paramasivam

Embed Face API Results in Power BI

As you know, the result of any APIs from Azure Cognitive services is a JSON file. The structure of the JSON file is not in a proper way to handle them effectively and easily inside Power BI.

In this article, I am explaining the easiest way to get the result in a proper way inside Power BI.

To accomplish the result, I am using a python script. As Power BI starts support python as one of the data sources we can easily pass the python script and get the API result.

Azure Cognitive services have a bunch of APIs and documentation and API reference for each API. Since I am using python script, I can easily get the python Face API reference and use it directly.

Requirements

  1. Python 3
  2. Power BI Desktop

Use the below python code. Update your Face API subscription key & url.

from urllib.request import urlopen

import json, os, io, requests

from io import BytesIO

import pandas as pd

subscription_key = "your_subscription_key"
base_url = "https://your_region.api.cognitive.microsoft.com/face/v1.0/"
detect_url=base_url+"detect"

headers    = {'Ocp-Apim-Subscription-Key': subscription_key,

              'Content-Type': 'application/octet-stream'}

params     = {'returnFaceId': 'true',

    'returnFaceLandmarks': 'false',

    'returnFaceAttributes': 'age,gender,smile,facialHair,headPose,glasses,emotion,hair,makeup,accessories,blur,exposure,noise'}

Image_Path="https://img.etimg.com/thumb/msid-61020784,width-643,imgsize-228069,resizemode-4/3-lessons-that-satya-nadella-took-from-the-cricket-field-to-the-ceos-office.jpg"

with urlopen(Image_Path) as url:

    image_data = io.BytesIO(url.read())
     
    response = requests.post(

          detect_url, headers=headers, params=params, data=image_data)
 
    face=json.loads(response.content)

    smile= [face[0]['faceAttributes']['smile']]

    gender = [str(face[0]['faceAttributes']['gender'])]

    age= [face[0]['faceAttributes']['age']]

    glass=[str(face[0]['faceAttributes']['glasses'])]

    anger=[face[0]['faceAttributes']['emotion']['anger']]

    contempt=[face[0]['faceAttributes']['emotion']['contempt']]

    disgust=[face[0]['faceAttributes']['emotion']['disgust']]

    fear=[face[0]['faceAttributes']['emotion']['fear']]

    happy=[face[0]['faceAttributes']['emotion']['happiness']]

    neutral = [face[0]['faceAttributes']['emotion']['neutral']]

    sad=[face[0]['faceAttributes']['emotion']['sadness']]

    surprise=[face[0]['faceAttributes']['emotion']['surprise']]

    eyemakeup=[face[0]['faceAttributes']['makeup']['eyeMakeup']]

    lipmakeup=[face[0]['faceAttributes']['makeup']['lipMakeup']]

    bald=[face[0]['faceAttributes']['hair']['bald']]

    haircolor=[face[0]['faceAttributes']['hair']['hairColor']]

    face_ds = pd.DataFrame({

        "smile": smile,

        "gender":gender,

        "age":age,

        "glass":glass,

        "anger":anger,

        "contempt":contempt,

        "disgust":disgust,

        "fear":fear,

        "happy":happy,

        "neutral":neutral,

        "sad":sad,

        "surprise":surprise,

        "eyemakeup":eyemakeup,

        "lipmakeup":lipmakeup,

        "bald":bald,

        "haircolor":haircolor

    })

You can test the above code on your python IDE and can see the result which will be in a table format.

Power BI Desktop Report

Follow the below steps.

Open Power BI Desktop and choose “Python script” as a data source.

Copy and paste the above code on the editor window.

Click ok and it will load and display the table as like below.

Load the data and you can use those fields on your report.

As of now, the image path is hardcoded by you can dynamically pass it by using the parameters.

The sample look and feel of the report.

About the Author

Dr. SubraMANI Paramasivam administrator

Dr.SubraMANI Paramasivam is a Data Platform MVP, Power BI Group Leader, International Speaker, Microsoft Certified Trainer and Principal Solutions Architect at DAGEOP Ltd, having 20+ years of DB, BI & Analytics experience. Dr.SubraMANI Paramasivam is a frequent speaker in SQLBITS, PASS Summit, PASS SQL Saturdays, SQLRelay, Data Platform Summit, MCT Summit, SQLServerGeeks Summit, INSIDESQL UK, Data Awareness Programmes, Data Day Events. He likes to enlighten the education & data and started his own journey with FREE Data Awareness Programme (www.dataap.org), in remote areas who are in need. Mani speaks a lot about Visualization, Data Science, R Analytics, Azure concepts, Performance Tuning, DB & SQL Server Architecture, DBA, Partitioning, Resource Governor, Disaster Recovery, High Availability, Development, T-SQL Programming and Microsoft certifications.

Comments Are Closed!!!