PyObjC framework SensitiveContentAnalysis

12.1 · active · verified Tue Apr 14

PyObjC-framework-SensitiveContentAnalysis provides Python wrappers for Apple's macOS SensitiveContentAnalysis framework (macOS 14.0+). This framework enables apps to check images and videos for nudity, supporting features like Sensitive Content Warning and Communication Safety parental controls. The library is part of the larger PyObjC project, which acts as a bridge between Python and Objective-C, allowing Python applications to leverage macOS's native Cocoa APIs. Version 12.1 is the latest release, with regular updates aligning with macOS SDK changes and Python versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `SCSensitivityAnalyzer` and call its `analyzeCGImage_completionHandler_` method. It includes a critical warning about the required `com.apple.developer.sensitivecontentanalysis.client` entitlement, which is a common footgun and necessary for the framework to function correctly. Without this entitlement, the analysis will likely fail or return no sensitive results. Due to the nature of the framework, a real `CGImage` object is needed, which would typically be obtained via a graphics framework like `Quartz` (e.g., `Quartz.CGImageSourceCreateWithURL`). The example uses a placeholder for brevity.

import SensitiveContentAnalysis
import objc
from PyObjCTools import AppHelper

# NOTE: For SensitiveContentAnalysis to return positive results,
# your macOS application MUST be signed with the
# 'com.apple.developer.sensitivecontentanalysis.client' entitlement.
# This is typically added via Xcode capabilities, not Python code.
# Without it, analysis might always return 'None' or errors like 'User Safety not enabled'.

# Placeholder for a CGImage. In a real app, you'd load this
# from a file (e.g., using Quartz.CGImageSourceCreateWithURL).
# For a runnable example, we define a dummy function.
def create_dummy_cgimage():
    # In a real application, you would load a CGImage from a file or buffer.
    # This is a conceptual placeholder.
    print("\n--- Placeholder: Create a dummy CGImage ---")
    print("In a real app, load an image using e.g., Quartz.CGImageSourceCreateWithURL.")
    # A real CGImage object from a framework like Quartz would be passed here.
    # For demonstration, we'll return None and assume external setup.
    return None # Replace with an actual CGImage object

def analysis_completion_handler(result, error):
    if error:
        print(f"Analysis Error: {error}")
        if 'User Safety either not entitled' in str(error):
            print("HINT: Ensure your app has the 'com.apple.developer.sensitivecontentanalysis.client' entitlement.")
    elif result:
        print(f"Sensitive Content Analysis Result: {result.isSensitive()}")
        if result.isSensitive():
            print("Intervention guidance:", result.interventionPolicy())
    else:
        print("No result or error provided.")
    AppHelper.stopEventLoop()

def perform_analysis():
    image = create_dummy_cgimage()
    if image is None:
        print("Cannot proceed without a real CGImage object. Please set up image loading.")
        return

    analyzer = SensitiveContentAnalysis.SCSensitivityAnalyzer.alloc().init()
    analyzer.analyzeCGImage_completionHandler_(image, analysis_completion_handler)

if __name__ == '__main__':
    print("Starting SensitiveContentAnalysis example...")
    # To run this in a context where the framework actually works,
    # you'd typically embed it in a py2app created application bundle
    # that has the necessary entitlements.
    AppHelper.callAfter(perform_analysis)
    AppHelper.runEventLoop()
    print("SensitiveContentAnalysis example finished.")

view raw JSON →