Flutter不良内容检测插件nsfw_detector_flutter的使用
Flutter不良内容检测插件nsfw_detector_flutter的使用
简介
nsfw_detector_flutter
是一个用于检测NSFW(Not Safe For Work / NUDE / adults)内容并分类安全内容的Flutter插件,无需下载或设置任何资源文件。该插件通过机器学习模型来判断图片是否包含成人内容,并返回一个置信度评分。
安装
在您的 pubspec.yaml
文件中添加依赖:
dependencies:
nsfw_detector_flutter: ^latest_version
然后运行以下命令以安装包:
flutter pub add nsfw_detector_flutter
使用示例
初始化和加载检测器
import 'package:nsfw_detector_flutter/nsfw_detector_flutter.dart';
NsfwDetector detector = await NsfwDetector.load(); // 默认阈值为0.7
检测NSFW内容
您可以从文件、字节数组或图像对象中检测NSFW内容。
从文件检测
File imageFile = File('path/to/image.jpg');
NsfwResult? result = await detector.detectNSFWFromFile(imageFile);
从字节数组检测
final ByteData data = await rootBundle.load('assets/nsfw.jpeg');
final Uint8List imageData = data.buffer.asUint8List();
NsfwResult? result = await detector.detectNSFWFromBytes(imageData);
从图像对象检测
import 'package:image/image.dart' as img;
img.Image image = img.decodeImage(File('path/to/image.jpg').readAsBytesSync())!;
NsfwResult? result = await detector.detectNSFWFromImage(image);
示例应用
以下是一个完整的示例应用,展示如何在Flutter应用中使用 nsfw_detector_flutter
插件:
import 'package:flutter/material.dart';
import 'package:nsfw_detector_flutter/nsfw_detector_flutter.dart';
import 'package:flutter/services.dart';
import 'package:image/image.dart' as img;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _result = "Loading...";
@override
void initState() {
super.initState();
_detectNSFW();
}
Future<void> _detectNSFW() async {
try {
// 加载图片文件
final ByteData data = await rootBundle.load('assets/nsfw.jpeg');
final Uint8List imageData = data.buffer.asUint8List();
img.Image image = img.decodeImage(imageData)!;
// 加载并初始化NSFW检测器
NsfwDetector detector = await NsfwDetector.load();
NsfwResult? result = await detector.detectNSFWFromImage(image);
setState(() {
_result = 'NSFW score: ${result?.score}, Detected: ${result?.isNsfw}';
});
} catch (e) {
setState(() {
_result = 'Error: $e';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('NSFW Detector Example'),
),
body: Center(
child: Text(_result),
),
),
);
}
}
注意事项
iOS注意事项
如果在iOS上遇到问题,请确保在XCode中将 Build Settings > Deployment > Strip Linked Product 设置为 No。
Android注意事项
此插件依赖于 tflite_flutter
包,因此Android的 minSdkVersion
必须设置为26或更高。请检查 android/app/build.gradle
文件中的设置:
android {
defaultConfig {
minSdkVersion 26
}
}
测试
有关如何为此包运行集成测试的信息,请参阅 example README。
许可证
本项目采用MIT许可证,详情请参阅 LICENSE 文件。
默认使用的模型来自 open_nsfw_android 仓库,这是 yahoo/open_nsfw 模型的一个移植版本。本包遵守yahoo/open_nsfw仓库的许可条款。
BSD 3-Clause License
Copyright 2016, Yahoo Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the Yahoo Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
希望这些信息能帮助您更好地理解和使用 nsfw_detector_flutter
插件。如果有任何问题或需要进一步的帮助,请随时提问!
更多关于Flutter不良内容检测插件nsfw_detector_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter不良内容检测插件nsfw_detector_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中集成和使用nsfw_detector_flutter
插件来进行不良内容检测的示例代码。这个插件通常用于检测图像是否包含成人或不适宜公开的内容。
1. 添加依赖
首先,你需要在pubspec.yaml
文件中添加nsfw_detector_flutter
的依赖:
dependencies:
flutter:
sdk: flutter
nsfw_detector_flutter: ^最新版本号 # 请替换为实际的最新版本号
然后运行flutter pub get
来安装依赖。
2. 导入插件
在你的Dart文件中(例如main.dart
),导入nsfw_detector_flutter
插件:
import 'package:nsfw_detector_flutter/nsfw_detector_flutter.dart';
3. 使用NSFWDetector
接下来,你可以使用NSFWDetector
类来加载和检测图像。以下是一个简单的示例,展示如何从设备相册中选择一张图片并进行检测:
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:nsfw_detector_flutter/nsfw_detector_flutter.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('NSFW Detector Demo'),
),
body: Center(
child: NSFWDetectorDemo(),
),
),
);
}
}
class NSFWDetectorDemo extends StatefulWidget {
@override
_NSFWDetectorDemoState createState() => _NSFWDetectorDemoState();
}
class _NSFWDetectorDemoState extends State<NSFWDetectorDemo> {
final ImagePicker _picker = ImagePicker();
File? _imageFile;
String? _result;
Future<void> _pickImage() async {
final pickedFile = await _picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
setState(() {
_imageFile = File(pickedFile.path);
});
_detectNSFW(_imageFile!);
}
}
Future<void> _detectNSFW(File imageFile) async {
try {
final detector = NSFWDetector();
final result = await detector.predictImage(imageFile);
setState(() {
_result = result.nsfwScore > 0.5 ? 'NSFW' : 'SFW';
});
} catch (e) {
setState(() {
_result = 'Error: ${e.message}';
});
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _pickImage,
child: Text('Pick Image'),
),
if (_imageFile != null)
Image.file(_imageFile!, width: 300, height: 300),
if (_result != null)
Text(
'Result: $_result',
style: TextStyle(fontSize: 20, color: Colors.red),
),
],
);
}
}
注意事项
- 权限:别忘了在
AndroidManifest.xml
和Info.plist
中添加必要的权限,以允许应用访问设备相册。 - 图像选择器:上面的示例使用了
image_picker
插件来选择图像。如果你还没有这个插件,你需要在pubspec.yaml
中添加它。 - 错误处理:在实际应用中,你应该添加更多的错误处理逻辑,以处理可能发生的各种异常情况。
这个示例提供了一个基本的框架,展示了如何在Flutter应用中集成和使用nsfw_detector_flutter
插件来进行不良内容检测。根据你的需求,你可以进一步扩展和自定义这个示例。