Flutter实时目标检测插件yolo_realtime_plugin的使用

发布于 1周前 作者 songsunli 来自 Flutter

好的,下面是一个完整的示例代码,展示了如何在Flutter应用中使用flutter_yolo_realtime_plugin进行实时目标检测。这个示例包括了初始化模型、处理检测结果以及展示检测框的功能。

import 'package:flutter/material.dart';
import 'package:yolo_realtime_plugin/yolo_realtime_plugin.dart';

class YoloRealTimeViewExample extends StatefulWidget {
  const YoloRealTimeViewExample({Key? key}) : super(key: key);

  [@override](/user/override)
  State<YoloRealTimeViewExample> createState() =>
      _YoloRealTimeViewExampleState();
}

class _YoloRealTimeViewExampleState extends State<YoloRealTimeViewExample> {
  YoloRealtimeController? yoloController;

  [@override](/user/override)
  void initState() {
    super.initState();

    yoloInit();
  }

  Future<void> yoloInit() async {
    yoloController = YoloRealtimeController(
      // common
      fullClasses: fullClasses,
      activeClasses: activeClasses,

      // android
      androidModelPath: 'assets/models/yolov5s_320.pt',
      androidModelWidth: 320,
      androidModelHeight: 320,
      androidConfThreshold: 0.5,
      androidIouThreshold: 0.5,

      // ios
      iOSModelPath: 'yolov5s',
      iOSConfThreshold: 0.5,
    );

    try {
      await yoloController?.initialize();
    } catch (e) {
      print('ERROR: $e');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    if (yoloController == null) {
      return Container();
    }

    return YoloRealTimeView(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      controller: yoloController!,
      drawBox: true,
      captureBox: (boxes) {
        // Print the detected boxes
        print(boxes);
      },
      captureImage: (data ) async {
        // Process and use the binary image as you wish.
        // For example, save the image to a file
        final tempDir = await getTemporaryDirectory();
        final file = await File('${tempDir.path}/${DateTime.now()}.png').create();
        await file.writeAsBytesSync(data);

        print('File saved: ${file.path}');
      },
    );
  }

  List<String> activeClasses = [
    "car",
    "person",
    "tv",
    "laptop",
    "mouse",
    "bottle",
    "cup",
    "keyboard",
    "cell phone",
  ];

  List<String> fullClasses = [
    "person",
    "bicycle",
    "car",
    "motorcycle",
    "airplane",
    "bus",
    "train",
    "truck",
    "boat",
    "traffic light",
    "fire hydrant",
    "stop sign",
    "parking meter",
    "bench",
    "bird",
    "cat",
    "dog",
    "horse",
    "sheep",
    "cow",
    "elephant",
    "bear",
    "zebra",
    "giraffe",
    "backpack",
    "umbrella",
    "handbag",
    "tie",
    "suitcase",
    "frisbee",
    "skis",
    "snowboard",
    "sports ball",
    "kite",
    "baseball bat",
    "baseball glove",
    "skateboard",
    "surfboard",
    "tennis racket",
    "bottle",
    "wine glass",
    "cup",
    "fork",
    "knife",
    "spoon",
    "bowl",
    "banana",
    "apple",
    "sandwich",
    "orange",
    "broccoli",
    "carrot",
    "hot dog",
    "pizza",
    "donut",
    "cake",
    "chair",
    "couch",
    "potted plant",
    "bed",
    "dining table",
    "toilet",
    "tv",
    "laptop",
    "mouse",
    "remote",
    "keyboard",
    "cell phone",
    "microwave",
    "oven",
    "toaster",
    "sink",
    "refrigerator",
    "book",
    "clock",
    "vase",
    "scissors",
    "teddy bear",
    "hair drier",
    "toothbrush"
  ];
}

更多关于Flutter实时目标检测插件yolo_realtime_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter实时目标检测插件yolo_realtime_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中集成并使用yolo_realtime_plugin进行实时目标检测的示例代码。这个插件假设你已经有一个预训练的YOLO模型,并且该模型已经转换为可以在移动设备上运行的格式(如TFLite模型)。

1. 添加依赖

首先,在你的pubspec.yaml文件中添加yolo_realtime_plugin依赖:

dependencies:
  flutter:
    sdk: flutter
  yolo_realtime_plugin: ^最新版本号  # 请替换为实际可用的最新版本号

然后运行flutter pub get来安装依赖。

2. 配置Android和iOS项目

由于yolo_realtime_plugin依赖于TensorFlow Lite进行模型推理,你可能需要在Android和iOS项目中进行一些额外的配置。这通常包括将TFLite模型和相关的标签文件添加到项目中,并修改相应的配置文件以启用这些资源。

Android配置

  • 将你的TFLite模型和标签文件(例如model.tflitelabels.txt)放置在android/app/src/main/assets/目录下。
  • 确保在android/app/build.gradle中设置了正确的minSdkVersiontargetSdkVersion

iOS配置

  • 将你的TFLite模型和标签文件添加到Xcode项目的Runner目标下的Assets.xcassets或者直接放在项目根目录,并在Info.plist中注册它们。
  • 确保在Xcode中启用了对TFLite的支持。

3. 使用插件进行实时目标检测

下面是一个简单的Flutter应用示例,展示如何使用yolo_realtime_plugin进行实时目标检测:

import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:yolo_realtime_plugin/yolo_realtime_plugin.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 请求相机权限
  final cameras = await availableCameras();
  final firstCamera = cameras.first;

  runApp(MyApp(camera: firstCamera));
}

class MyApp extends StatefulWidget {
  final CameraDescription camera;

  MyApp({required this.camera});

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late CameraController _controller;
  late YOLORealTimePlugin _yoloPlugin;
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();
    // 初始化相机控制器
    _controller = CameraController(widget.camera, ResolutionPreset.high);
    _controller.initialize().then((_) {
      if (!mounted) return;
      setState(() {});
    });

    // 初始化YOLO插件
    _yoloPlugin = YOLORealTimePlugin();
    _yoloPlugin.initialize();
  }

  @override
  void dispose() {
    _controller.dispose();
    _yoloPlugin.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (!_controller.value.isInitialized) {
      return Container();
    }

    return MaterialApp(
      home: Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(title: Text('YOLO Realtime Detection')),
        body: Center(
          child: AspectRatio(
            aspectRatio: _controller.value.aspectRatio,
            child: CameraPreview(_controller),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            try {
              // 获取相机图像帧并进行目标检测
              final frame = await _controller.startImageStream((image) async {
                final detections = await _yoloPlugin.detectObjects(image: image);
                // 在此处处理检测结果,例如绘制边界框
                print(detections);
              }).first;
            } catch (e) {
              print('Error: $e');
            }
          },
          tooltip: 'Start Detection',
          child: Icon(Icons.play_arrow),
        ),
      ),
    );
  }
}

注意

  1. 上述代码是一个简化的示例,实际使用时你可能需要处理更多的细节,例如相机预览的UI布局、检测结果的绘制、错误处理等。
  2. yolo_realtime_plugin的具体API可能会根据版本有所不同,请参考其官方文档或源代码以获取最新的使用方法和API。
  3. 相机图像的获取和处理可能需要根据实际需求进行调整,例如调整图像分辨率、格式等以匹配你的YOLO模型输入要求。
  4. 确保在实际部署前对应用进行充分的测试,特别是在不同设备和不同光照条件下的性能表现。
回到顶部