Giter Site home page Giter Site logo

doppeltilde / sensitive_content_analysis Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 304 KB

Dart package for detecting and alerting users to nudity in images and videos on-device, utilizing the SensitiveContentAnalysis Framework by Apple.

Home Page: https://pub.dev/packages/sensitive_content_analysis

License: BSD 3-Clause "New" or "Revised" License

Kotlin 0.51% Dart 31.30% Ruby 16.06% Swift 51.99% Objective-C 0.13%
apple nsfw-detection sensitivecontentanalysis ios nsfw-recognition nudity-detection ipados macos flutter swift

sensitive_content_analysis's Introduction

Sensitive Content Analysis

Provide a safer experience in your app by detecting and alerting users to nudity in images and videos before displaying them onscreen.

Pub

Dart package for interacting with Apple's SensitiveContentAnalysis Framework.

Minimum requirements

Lower deployment versions may be targeted, however, it's important to note that the SCA Framework is exclusively compatible with:

  • iOS/iPadOS >=17.0
  • macOS >=14.0

Other Notice's

  • In order to maintain the package's lightweight nature and grant you complete control over your UI's appearance, this package intentionally refrains from incorporating an overlay or blur feature.
  • The framework only works on physical devices. #3

Install

Add the app entitlement:

The OS requires the com.apple.developer.sensitivecontentanalysis.client entitlement in your app’s code signature to use SensitiveContentAnalysis. Calls to the framework fail to return positive results without it. You can can add this entitlement to your app by enabling the Sensitive Content Analysis capability in Xcode.

<key>com.apple.developer.sensitivecontentanalysis.client</key>
<array>
	<string>analysis</string>
</array>

Install the test profile

For testing purposes, Apple offers a test profile that enables you to evaluate the frameworks functionality without the necessity of installing actual NSFW content.

See: https://developer.apple.com/documentation/sensitivecontentanalysis/testing-your-app-s-response-to-sensitive-media


Usage and Examples

Check Policy:

final sca = SensitiveContentAnalysis();

int? policy = await sca.checkPolicy();
if (policy != null) {
  return policy;
}

case disabled = 0 If disabled the framework doesn’t detect nudity. The system disables sensitive content analysis under any of the following conditions:

  • The app lacks the necessary com.apple.developer.sensitivecontentanalysis.client entitlement.
  • Neither the Sensitive Content Warning user preference nor the Communication Safety parental control in Screen Time are active.
  • The user disables the Sensitive Content Warnings toggle in your app’s Settings.

case simpleInterventions = 1 simpleInterventions indicates that the user enables both of the following:

  • Sensitive Content Warnings user preference
  • Sensitive Content Warnings in your app’s settings

When your app detects nudity under this policy, your app needs to:

  • Keep the intervention minimal by describing the issue briefly and updating your app’s UI unobstructively. For example, consider blurring and annotating the area that otherwise presents the sensitive content versus raising a new fullscreen alert.
  • Intervene on the receipt of sensitve content over the network but allow the app to transmit content over the network unchecked.

case descriptiveInterventions = 2 descriptiveInterventions indicates that the user enables both of the following:

  • Communication Safety parental control in Screen Time
  • Sensitive Content Warnings in your app’s settings

When your app detects nudity under this policy, your app needs to:

  • Use child-appropriate language, such as broadly understood vocabulary
  • Present an alert that fills the full screen.
  • Intervene on the receipt of sensitve content over a network and before transmitting sensitive content over a network.

Analyze Image

File Image:

  try {
    final sca = SensitiveContentAnalysis();
    final ImagePicker picker = ImagePicker();

    // Pick an image.
    final XFile? image = await picker.pickImage(source: ImageSource.gallery);

    if (image != null) {
        Uint8List imageData = await image.readAsBytes();

        // Analyze the image for sensitive content.
        final bool? isSensitive = await sca.analyzeImage(imageData);
        if (isSensitive != null) {
            return isSensitive;
        } else {
            debugPrint("Enable ”Sensitive Content Warning” in Settings -> Privacy & Security.");
            return null;
        }
    }
  } catch (e) {
      return null;
  }

Network Image:

final String? analyzeUrl = "https://docs-assets.developer.apple.com/published/517e263450/rendered2x-1685188934.png";

  try {
    final sca = SensitiveContentAnalysis();

    if (analyzeUrl != null) {
        final bool? isSensitive = await sca.analyzeNetworkImage(url: analyzeUrl);
        if (isSensitive != null) {
            return isSensitive;
        } else {
            debugPrint("Enable ”Sensitive Content Warning” in Settings -> Privacy & Security.");
            return null;
        }
    }
  } catch (e) {
      return null;
  }

Analyze Video

Network Video:

  Future<void> analyzeNetworkVideo() async {
    try {
      Dio dio = Dio();
      Directory tempDir = await getTemporaryDirectory();

      const url = "https://developer.apple.com/sample-code/web/qr-sca.mov";
      final videoName = p.basename(url);
      final file = File("${tempDir.path}/$videoName");
      final response = await dio.download(url, file.path);

      if (response.statusCode == 200) {
        bool? isSensitive = await sca.analyzeVideo(url: file.path);
        debugPrint("SENSITIVE: $isSensitive");
        await file.delete();
      }
    } catch (e) {
      debugPrint(e.toString());
    }
  }

Local Video:

  Future<void> analyzeLocalVideo() async {
    try {
      const XTypeGroup typeGroup = XTypeGroup(
        label: 'video',
        extensions: <String>['mp4', 'mkv', 'avi', 'mov'],
      );
      final XFile? selectedFile =
          await openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);

      if (selectedFile != null) {
        bool? isSensitive = await sca.analyzeVideo(url: selectedFile.path);
        debugPrint("SENSITIVE: $isSensitive");
      }
    } catch (e) {
      debugPrint(e.toString());
    }
  }

Apps using:


Caveats

Unlike with other ML models, the SensitiveContentAnalysis Framework:

  • Does not return a list of probabilities.
  • Does not allow additional training and finetuning.
  • Is not open source.
  • Only works with Apple devices. (iOS 17.0+, macOS 14.0+, Mac Catalyst 17.0+, iPadOS 17.0+)

Notice: This package was initally created to be used in-house, as such the development is first and foremost aligned with the internal requirements.

sensitive_content_analysis's People

Contributors

jonafeucht avatar

Stargazers

 avatar  avatar

Watchers

 avatar

sensitive_content_analysis's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.