Flutter 3D姿态识别插件apple_vision_pose_3d的使用

Flutter 3D姿态识别插件apple_vision_pose_3d的使用

Apple Vision Pose Detection 是一个 Flutter 插件,使 Flutter 应用能够使用 Apple Vision Pose Detection 3D

该插件不是由 Apple 赞助或维护。作者是一些希望为 macOS 创建类似 Google ML Kit 的开发者。

要求

MacOS

  • 最低 osx 部署目标:14.0
  • Xcode 13 或更新版本
  • Swift 5
  • ML Kit 只支持 64 位架构(x86_64 和 arm64)

iOS

  • 最低 ios 部署目标:17.0
  • Xcode 13 或更新版本
  • Swift 5
  • ML Kit 只支持 64 位架构(x86_64 和 arm64)

开始使用

首先导入 ‘package:apple_vision_pose_3d/apple_vision_pose_3d.dart’;

final GlobalKey cameraKey = GlobalKey(debugLabel: "cameraKey");
late AppleVisionPose3DController visionController = AppleVisionPose3DController();
InsertCamera camera = InsertCamera();
Size imageSize = const Size(640, 640 * 9 / 16);
String? deviceId;
bool loading = true;

List<PoseData3D>? poseData;
late double deviceWidth;
late double deviceHeight;

[@override](/user/override)
void initState() {
  camera.setupCameras().then((value) {
    setState(() {
      loading = false;
    });
    camera.startLiveFeed((InputImage i) {
      if (i.metadata?.size != null) {
        imageSize = i.metadata!.size;
      }
      if (mounted) {
        Uint8List? image = i.bytes;
        visionController.processImage(image!, i.metadata!.size).then((data) {
          poseData = data;
          setState(() {});
        });
      }
    });
  });
  super.initState();
}

[@override](/user/override)
void dispose() {
  camera.dispose();
  super.dispose();
}

[@override](/user/override)
Widget build(BuildContext context) {
  deviceWidth = MediaQuery.of(context).size.width;
  deviceHeight = MediaQuery.of(context).size.height;
  return Stack(
    children: <Widget>[
      SizedBox(
        width: imageSize.width,
        height: imageSize.height,
        child: loading ? Container() : CameraSetup(camera: camera, size: imageSize),
      ),
    ] + showPoints()
  );
}

List<Widget> showPoints() {
  if (poseData == null || poseData!.isEmpty) return [];
  Map<Joint3D, Color> colors = {
    Joint3D.rightAnkle: Colors.orange,
    Joint3D.rightKnee: Colors.orange,
    Joint3D.rightHip: Colors.orange,

    Joint3D.rightWrist: Colors.purple,
    Joint3D.rightElbow: Colors.purple,

    Joint3D.rightShoulder: Colors.pink,
    Joint3D.leftShoulder: Colors.pink,

    Joint3D.leftElbow: Colors.indigo,
    Joint3D.leftWrist: Colors.indigo,

    Joint3D.leftHip: Colors.grey,
    Joint3D.leftKnee: Colors.grey,
    Joint3D.leftAnkle: Colors.grey,

    Joint3D.root: Colors.yellow,
    Joint3D.centerShoulder: Colors.yellow,
    Joint3D.spine: Colors.yellow,

    Joint3D.centerHead: Colors.cyanAccent,
    Joint3D.topHead: Colors.cyanAccent
  };
  List<Widget> widgets = [];
  for (int j = 0; j < poseData!.length; j++) {
    for (int i = 0; i < poseData![j].poses.length; i++) {
      widgets.add(
        Positioned(
          top: imageSize.height - poseData![j].poses[i].location.y * imageSize.height,
          left: poseData![j].poses[i].location.x * imageSize.width,
          child: Container(
            width: 10,
            height: 10,
            decoration: BoxDecoration(
              color: colors[poseData![j].poses[i].joint],
              borderRadius: BorderRadius.circular(5)
            ),
          )
        )
      );
      if (poseData![j].poses[i].joint != Joint3D.root) {
        widgets.add(
          Positioned(
            top: 100 - poseData![j].poses[i].location3D.y * 100,
            left: 100 + poseData![j].poses[i].location3D.x * 100,
            child: Container(
              width: 10,
              height: 10,
              decoration: BoxDecoration(
                color: colors[poseData![j].poses[i].joint],
                borderRadius: BorderRadius.circular(5)
              ),
            )
          ),
        );
      }
    }
  }
  return widgets;
}

示例

在以下链接中可以找到此 API 的示例代码:

import 'package:apple_vision_pose_3d/apple_vision_pose_3d.dart';
import 'package:flutter/material.dart';
import '../camera/camera_insert.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'camera/input_image.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const VisionPose(),
    );
  }
}

class VisionPose extends StatefulWidget {
  const VisionPose({
    Key? key,
    this.onScanned
  }) : super(key: key);

  final Function(dynamic data)? onScanned;

  [@override](/user/override)
  _VisionPose createState() => _VisionPose();
}

class _VisionPose extends State<VisionPose> {
  final GlobalKey cameraKey = GlobalKey(debugLabel: "cameraKey");
  late AppleVisionPose3DController visionController = AppleVisionPose3DController();
  InsertCamera camera = InsertCamera();
  Size imageSize = const Size(640, 640 * 9 / 16);
  String? deviceId;
  bool loading = true;

  List<PoseData3D>? poseData;
  late double deviceWidth;
  late double deviceHeight;

  [@override](/user/override)
  void initState() {
    camera.setupCameras().then((value) {
      setState(() {
        loading = false;
      });
      camera.startLiveFeed((InputImage i) {
        if (i.metadata?.size != null) {
          imageSize = i.metadata!.size;
        }
        if (mounted) {
          Uint8List? image = i.bytes;
          visionController.processImage(image!, i.metadata!.size).then((data) {
            poseData = data;
            setState(() {});
          });
        }
      });
    });
    super.initState();
  }

  [@override](/user/override)
  void dispose() {
    camera.dispose();
    super.dispose();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    deviceWidth = MediaQuery.of(context).size.width;
    deviceHeight = MediaQuery.of(context).size.height;
    return Stack(
      children: <Widget>[
        SizedBox(
          width: imageSize.width,
          height: imageSize.height,
          child: loading ? Container() : CameraSetup(camera: camera, size: imageSize)
        ),
      ] + showPoints()
    );
  }

  List<Widget> showPoints() {
    if (poseData == null || poseData!.isEmpty) return [];
    Map<Joint3D, Color> colors = {
      Joint3D.rightAnkle: Colors.orange,
      Joint3D.rightKnee: Colors.orange,
      Joint3D.rightHip: Colors.orange,

      Joint3D.rightWrist: Colors.purple,
      Joint3D.rightElbow: Colors.purple,

      Joint3D.rightShoulder: Colors.pink,
      Joint3D.leftShoulder: Colors.pink,

      Joint3D.leftElbow: Colors.indigo,
      Joint3D.leftWrist: Colors.indigo,

      Joint3D.leftHip: Colors.grey,
      Joint3D.leftKnee: Colors.grey,
      Joint3D.leftAnkle: Colors.grey,

      Joint3D.root: Colors.yellow,
      Joint3D.centerShoulder: Colors.yellow,
      Joint3D.spine: Colors.yellow,

      Joint3D.centerHead: Colors.cyanAccent,
      Joint3D.topHead: Colors.cyanAccent
    };
    List<Widget> widgets = [];
    for (int j = 0; j < poseData!.length; j++) {
      for (int i = 0; i < poseData![j].poses.length; i++) {
        widgets.add(
          Positioned(
            top: imageSize.height - poseData![j].poses[i].location.y * imageSize.height,
            left: poseData![j].poses[i].location.x * imageSize.width,
            child: Container(
              width: 10,
              height: 10,
              decoration: BoxDecoration(
                color: colors[poseData![j].poses[i].joint],
                borderRadius: BorderRadius.circular(5)
              ),
            )
          )
        );
        if (poseData![j].poses[i].joint != Joint3D.root) {
          widgets.add(
            Positioned(
              top: 100 - poseData![j].poses[i].location3D.y * 100,
              left: 100 + poseData![j].poses[i].location3D.x * 100,
              child: Container(
                width: 10,
                height: 10,
                decoration: BoxDecoration(
                  color: colors[poseData![j].poses[i].joint],
                  borderRadius: BorderRadius.circular(5)
                ),
              )
            ),
          );
        }
      }
    }
    return widgets;
  }
}

更多关于Flutter 3D姿态识别插件apple_vision_pose_3d的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter 3D姿态识别插件apple_vision_pose_3d的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


apple_vision_pose_3d 是一个 Flutter 插件,用于在 iOS 设备上进行 3D 姿态识别。它利用 Apple 的 Vision 框架来实现对人体姿态的 3D 检测和跟踪。以下是如何在 Flutter 项目中使用 apple_vision_pose_3d 的基本步骤和示例代码。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 apple_vision_pose_3d 插件作为依赖项:

dependencies:
  flutter:
    sdk: flutter
  apple_vision_pose_3d: ^0.0.1  # 使用最新版本

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

2. 导入插件

在你的 Dart 文件中导入插件:

import 'package:apple_vision_pose_3d/apple_vision_pose_3d.dart';

3. 初始化插件并调用姿态识别

你可以使用 AppleVisionPose3D 类来进行 3D 姿态识别。以下是一个简单的示例,展示如何在 Flutter 中使用该插件:

import 'package:flutter/material.dart';
import 'package:apple_vision_pose_3d/apple_vision_pose_3d.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';

class Pose3DRecognitionScreen extends StatefulWidget {
  [@override](/user/override)
  _Pose3DRecognitionScreenState createState() => _Pose3DRecognitionScreenState();
}

class _Pose3DRecognitionScreenState extends State<Pose3DRecognitionScreen> {
  File? _image;
  List<Pose3D>? _poses;

  final ImagePicker _picker = ImagePicker();

  Future<void> _pickImage() async {
    final pickedFile = await _picker.pickImage(source: ImageSource.gallery);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
      } else {
        print('No image selected.');
      }
    });

    if (_image != null) {
      await _detectPoses();
    }
  }

  Future<void> _detectPoses() async {
    if (_image == null) return;

    final poses = await AppleVisionPose3D.detectPoses(_image!);

    setState(() {
      _poses = poses;
    });

    if (_poses != null) {
      for (var pose in _poses!) {
        print('Pose detected: ${pose.keypoints}');
      }
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('3D Pose Recognition'),
      ),
      body: Column(
        children: <Widget>[
          _image == null
              ? Text('No image selected.')
              : Image.file(_image!),
          if (_poses != null)
            Expanded(
              child: ListView.builder(
                itemCount: _poses!.length,
                itemBuilder: (context, index) {
                  final pose = _poses![index];
                  return ListTile(
                    title: Text('Pose ${index + 1}'),
                    subtitle: Text('Keypoints: ${pose.keypoints}'),
                  );
                },
              ),
            ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _pickImage,
        tooltip: 'Pick Image',
        child: Icon(Icons.image),
      ),
    );
  }
}
回到顶部