Azure Image Moderator API with Power Automate – Part 2

In the previous blog we made a basic request to the Content Moderator Text Moderation API, which evaluated the Text String using Curl(Post Man) by making an HTTP POST request. We invoked the ‘Search’ text method and detected Profane terms, PII information and also classified the text in various categories.

The image moderator API can help us:

  • Evaluate for Adult and Racy content
  • Detecting text with Optical Character Recognition.
  • Detecting faces.
  • Match an image against one of your custom image lists

In this blog we will explore the ‘Image Moderation API’ with Power Automate(Microsoft Flow) and Automate approval process using Content Moderator.

Use Case: Content Moderator will Convert the list item to PDF only if the attachment does not contain ‘Racy’ image.

Before we begin:

  • A Microsoft Azure account. Start a free trial or sign in.
  • Create a Content Moderator resource in the Azure portal and use the free tier.
  • The key and endpoint that was generated for you during sign-up.
  • Muhimbi PDF Converter Services Online full, free or trial subscription(Sign up).
  • Basic knowledge of Managing SharePoint List.
  • Appropriate privileges to create Flows.

Part 1Configuring the SharePoint List:

  • Create a SharePoint Online List.
  • Create the SharePoint Online Columns:
  • For this demo, I want to allow the users to upload only one attachment, so I have customised the list in Power Apps.

  • In Power Apps select the attachment control and set ‘Maximum’ attachments to one.


Part 2 – Create a Content Moderator Power Automate(Microsoft Flow) which will Convert the list item to PDF only if the attachment does not contain ‘Racy’ image.

  • From a High Level our Workflow looks like the screenshot below:

Step 1: For this demo, we will use the SharePoint Online Trigger ‘When an item is created or modified’, add the ‘Site Address’ and ‘List Name’.

Step 2: Add a ‘Condition’ in Flow and Configure it with reference to the screenshot below. The ‘Review Status’  is the output of ‘When an item is Created or modified’ Condition ‘is equal to’ ‘true’.

Step 3: If the value evaluates to ‘True’ continue the Flow. If ‘False'(No) Terminate the Flow.

Step 4:  Add a ‘Condition’ in Flow and Configure it with reference to the screenshot below. The ‘Has attachment’ is the output of the ‘When an item is Created or modified’ Condition ‘is equal to’ ‘true’.

Step 5: If the value evaluates to ‘True’ continue the Flow.

  • If ‘False'(No), Add the ‘Update item’ action and update it with reference to the screenshot below.

VERY IMPORTANT!!  – Set the Review Status to NO or else your Flow with get into an infinite loop.

Step 6: If the value evaluates to ‘True’ .

  • Add the ‘Get attachments’ SharePoint Action, add the ‘Site Address’ , ‘List Name’ and ‘ID’ output of the ‘When an item is Created or modified’

Step 7: Add the ‘Get attachment content’ SharePoint Action, add the ‘Site Address’ , ‘List Name’,  ‘ID’ output of the ‘When an item is Created or modified’ and ‘Id’ output of ‘Get attachments’ action.

Note: The ‘Apply to each’ loop will be automatically added because the ‘Id’ in the of type ‘array’ . All the Steps below are inside the ‘Apply to each’ loop.

Step 8: Add Azure Content Moderator ‘Check if an image contains racy or adult content’.

  • In the ‘Image Source’ select ‘Image Content’.
  • In the ‘Image Content’ select the ‘Attachment Content’ the output of the SharePoint ‘Get attachment content’ action.

Step 9: Add a ‘Condition’ in Flow. Add the ‘Is Image classified as Racy’ is the output of the ‘Check if an image contains racy or adult content’ Condition’ is equal to’ ‘false’.

Step 10:

  • If (No), Add the ‘Update item’ action and update it with reference to the screenshot below.
  • You can also update the ‘Content Moderator review’ SharePoint Column with additional information as how racy was the image. I am including the Text for reference below:

You Image is Classified  as Adult or Racy:

Adult Classification Score -@{outputs('Check_if_an_image_contains_racy_or_adult_content')?['body/AdultClassificationScore']}

Racy Classification Score -@{outputs('Check_if_an_image_contains_racy_or_adult_content')?['body/RacyClassificationScore']}

VERY IMPORTANT!!  – Set the Review Status to NO or else your Flow with get into an infinite loop.

Step 11: If (yes), Add the ‘Convert HTML to PDF’ action. In the action I have added some Custom HTML to create a Post Card.

<!DOCTYPE html>
<html>
<head>
<style>
.card {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  max-width: auto;
  margin: auto;
  text-align: center;
  font-family: arial;
}
</style>
</head>
<body>
<h2 style="text-align:center">Product Card</h2>
<div class="card">
  <img src="data:image/png;base64, @{base64(outputs('Get_attachment_content')?['body'])}"  style="width:100%">
  <h1>@{triggerOutputs()?['body/Title']}</h1>
  <p>Some text about the product. @{triggerOutputs()?['body/Addsometextabouttheproduct_x002e']}</p>
</div>
</body>
</html>

Step 12: Use the ‘Create file’ SharePoint action to store the PDF document into SharePoint document library.

  • File name: For this Demo, we will user ‘Title ‘ (output variable of the ‘When an item is created or modified’ action).
  • File content: Processed file content the (output variable of the ‘Convert HTML to PDF’ )action.

Step 13: Finally we need to update the ”Content Moderator review’  to No(False) to make sure that the next time the item is updated the body of the Flow will not be executed. Please Configure the ‘Update item’ action with reference to the screenshot below.

Step 14: All done! Now its time to test our Flow.

  • Create a list item with ‘Racy image’ as attachment, the Flow will not create a PDF and list item column ‘Content Moderator review’  will be updated with details as mentioned in screenshot below:

  • Create a list item with ‘Non Racy image’ as attachment, the Flow will not create a PDF.

Subscribe to this blog for the latest updates about SharePoint Online, Microsoft Flow, Power Apps and document conversion and manipulation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s