sensitive_content_detection 0.1.1
sensitive_content_detection: ^0.1.1 copied to clipboard
Privacy-first, on-device sensitive image content detection for Flutter.
sensitive_content_detection #
On-device sensitive-content detection for Flutter. Images are processed locally with the bundled TensorFlow Lite model and are not uploaded to a server.
Features #
- Detect images on Android and iOS.
- Analyze a
Fileor encoded image bytes. - Reuse one initialized detector for multiple images.
- Configure the confidence threshold, image size, and inference threads.
Installation #
Add the package to pubspec.yaml:
dependencies:
sensitive_content_detection: ^0.1.0
Then fetch dependencies:
flutter pub get
The TensorFlow Lite model is included in the package. No separate model download is required.
Basic Usage #
import 'dart:io';
import 'package:sensitive_content_detection/sensitive_content_detection.dart';
final detector = SensitiveContentDetector();
Future<void> checkImage(File imageFile) async {
try {
final result = await detector.detect(imageFile);
if (result.isSensitive) {
print('Sensitive image: ${result.confidence}');
} else {
print('Image is safe: ${result.confidence}');
}
print(result.toJson());
} finally {
await detector.dispose();
}
}
For repeated checks, create the detector once and dispose it when the owning widget or service is destroyed:
final detector = SensitiveContentDetector();
Future<DetectionResult> analyze(File imageFile) {
return detector.detect(imageFile);
}
Future<void> close() => detector.dispose();
Analyze Image Bytes #
Use detectBytes when the image is already loaded in memory:
import 'dart:typed_data';
final Uint8List imageBytes = await imageFile.readAsBytes();
final result = await detector.detectBytes(imageBytes);
The bytes must contain a decodable image, such as JPEG or PNG data.
Configuration #
const config = SensitiveDetectionConfig(
threshold: 0.40,
maxImageDimension: 1024,
numThreads: 2,
);
final detector = SensitiveContentDetector(config: config);
Configuration options:
| Option | Default | Description |
|---|---|---|
threshold |
0.40 |
Minimum sensitive-content confidence from 0.0 to 1.0. |
maxImageDimension |
1024 |
Maximum width or height used during preprocessing. |
numThreads |
2 |
Number of CPU threads used by TensorFlow Lite. |
modelAssetPath |
Bundled model | Asset path for a compatible TensorFlow Lite model. |
Detection Results #
DetectionResult provides:
isSensitive: whether the image meets the configured threshold.isSafe: the inverse ofisSensitive.primaryCategory:SensitiveCategory.safe,sensitiveContent, orunknown.confidence: confidence for the selected primary category.detections: raw model detections.processingTime: elapsed processing time.modelVersion: model version when available.
Flutter Example #
The repository contains a complete example in the example folder.
It uses image_picker to select a gallery image and passes the selected file to
SensitiveContentDetector.detect.
Run it with:
cd example
flutter pub get
flutter run
Development #
Run the package tests from the repository root:
flutter test
flutter analyze
License #
See LICENSE. The bundled TensorFlow Lite model must be redistributed only when its model license permits it.