Monday, January 27, 2025

Azure AI services - Image Analysis with Azure AI Content Safety

Azure AI services - Image Analysis with Azure AI Content Safety:

Source: 
https://learn.microsoft.com/en-us/python/api/overview/azure/ai-contentsafety-readme?view=azure-python 

import os
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData, ImageCategory
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError

def analyze_image():
    endpoint = "https://contentsafety.cognitiveservices.azure.com/"
    key = "72OU9CFnMIOX5dtStPFqq3fTzjNkOYeBjFXJ3w3AAAHACOG8nbQ"
    image_path = "download_1.jpg"

    # Create an Azure AI Content Safety client
    client = ContentSafetyClient(endpoint, AzureKeyCredential(key))

    # Build request
    with open(image_path, "rb") as file:
        request = AnalyzeImageOptions(image=ImageData(content=file.read()))

    # Analyze image
    try:
        response = client.analyze_image(request)
    except HttpResponseError as e:
        print("Analyze image failed.")
        if e.error:
            print(f"Error code: {e.error.code}")
            print(f"Error message: {e.error.message}")
            raise
        print(e)
        raise

    hate_result = next(item for item in response.categories_analysis if item.category     == ImageCategory.HATE)
    self_harm_result = next(item for item in response.categories_analysis if item.category     == ImageCategory.SELF_HARM)
    sexual_result = next(item for item in response.categories_analysis if item.category     == ImageCategory.SEXUAL)
    violence_result = next(item for item in response.categories_analysis if item.category     == ImageCategory.VIOLENCE)

    if hate_result:
        print(f"Hate severity: {hate_result.severity}")
    if self_harm_result:
        print(f"SelfHarm severity: {self_harm_result.severity}")
    if sexual_result:
        print(f"Sexual severity: {sexual_result.severity}")
    if violence_result:
        print(f"Violence severity: {violence_result.severity}")

if __name__ == "__main__":
    analyze_image()


Input Image:


OutPut:



5 comments:

  1. Honorifically this website platform continues sharing mediumbloggers valuable content through consistent and engaging posts. I appreciate the informative structure, smooth navigation, and reliable updates that help readers stay informed and enjoy reading high quality articles regularly every single time here.

    ReplyDelete
  2. Zealously this platform keeps delivering flymenews.com informative updates through reliable and well-structured content. I appreciate the clear navigation, timely reporting, and helpful resources that enhance the reading experience.

    ReplyDelete
  3. Vividly this platform consistently provides NewsDailySources informative resources with excellent clarity and structure. I appreciate the reliable updates, engaging content, and welcoming design that encourages readers to enjoy discovering new and useful information regularly.

    ReplyDelete

Featured Post

Microsoft Copilot Studio: The Complete Beginner to Advanced Guide

Microsoft Copilot Studio: The Complete Beginner to Advanced Guide Version: July 2026 | Audience: Beginners, Citizen Developers, Busine...

Popular posts