Flutter 3D手部识别插件apple_vision_hand_3d的使用
Flutter 3D手部识别插件apple_vision_hand_3d的使用
apple_vision_hand_3d
是一个 Flutter 插件,它使 Flutter 应用能够使用 TensorFlow 的 Blaze 手部关键点检测模型。
要求
MacOS
- 最低 osx 部署目标版本:12.0
- Xcode 13 或更新版本
- Swift 5
- ML Kit 仅支持 64 位架构(x86_64 和 arm64)
iOS
- 最低 ios 部署目标版本:14.0
- Xcode 13 或更新版本
- Swift 5
- ML Kit 仅支持 64 位架构(x86_64 和 arm64)
开始使用
首先需要导入 apple_vision
包:
import 'package:apple_vision_hand_3d/apple_vision_hand_3d.dart';
以下是一个完整的示例代码:
import 'package:flutter/material.dart';
import 'package:apple_vision_hand_3d/apple_vision_hand_3d.dart';
import 'package:apple_vision_commons/apple_vision_commons.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 VisionHand(),
);
}
}
class VisionHand extends StatefulWidget {
const VisionHand({
Key? key,
this.onScanned
}) : super(key: key);
final Function(dynamic data)? onScanned;
[@override](/user/override)
_VisionHand createState() => _VisionHand();
}
class _VisionHand extends State<VisionHand> {
final GlobalKey cameraKey = GlobalKey(debugLabel: "cameraKey");
AppleVisionHand3DController visionController = AppleVisionHand3DController();
InsertCamera camera = InsertCamera();
Size imageSize = const Size(640, 640 * 9 / 16);
String? deviceId;
bool loading = true;
List<HandMesh>? handData;
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!, imageSize, ImageOrientation.up).then((data) {
handData = 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: [
SizedBox(
width: imageSize.width,
height: imageSize.height,
child: loading ? Container() : CameraSetup(camera: camera, size: imageSize),
),
] + showPoints()
);
}
List<Widget> showPoints() {
if (handData == null || handData!.isEmpty) return [];
List<Widget> widgets = [];
Map<FingerJoint3D, Color> colors = {
FingerJoint3D.thumbCMC: Colors.amber,
FingerJoint3D.thumbIP: Colors.amber,
FingerJoint3D.thumbMCP: Colors.amber,
FingerJoint3D.thumbTip: Colors.amber,
FingerJoint3D.indexDIP: Colors.green,
FingerJoint3D.indexMCP: Colors.green,
FingerJoint3D.indexPIP: Colors.green,
FingerJoint3D.indexTip: Colors.green,
FingerJoint3D.middleDIP: Colors.purple,
FingerJoint3D.middleMCP: Colors.purple,
FingerJoint3D.middlePIP: Colors.purple,
FingerJoint3D.middleTip: Colors.purple,
FingerJoint3D.ringDIP: Colors.pink,
FingerJoint3D.ringMCP: Colors.pink,
FingerJoint3D.ringPIP: Colors.pink,
FingerJoint3D.ringTip: Colors.pink,
FingerJoint3D.littleDIP: Colors.cyanAccent,
FingerJoint3D.littleMCP: Colors.cyanAccent,
FingerJoint3D.littlePIP: Colors.cyanAccent,
FingerJoint3D.littleTip: Colors.cyanAccent,
FingerJoint3D.wrist: Colors.black
};
for (int j = 0; j < handData!.length; j++) {
for (int i = 0; i < handData![j].poses.length; i++) {
HandPoint3D points = handData![j].poses[i].location;
widgets.add(
Positioned(
left: points.x * imageSize.width / 2.56 + handData![0].image.origin.x,
bottom: imageSize.height / 2 - points.y * imageSize.width / 2.56 + handData![0].image.origin.y + 120,
child: Container(
width: 10,
height: 10,
decoration: BoxDecoration(
color: colors[handData![j].poses[i].joint],
borderRadius: BorderRadius.circular(5)
),
)
)
);
}
}
return widgets;
}
Widget loadingWidget() {
return Container(
width: deviceWidth,
height: deviceHeight,
color: Theme.of(context).canvasColor,
alignment: Alignment.center,
child: const CircularProgressIndicator(color: Colors.blue)
);
}
}
更多关于Flutter 3D手部识别插件apple_vision_hand_3d的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter 3D手部识别插件apple_vision_hand_3d的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
apple_vision_hand_3d
是一个 Flutter 插件,用于在 iOS 设备上使用 Apple 的 Vision 框架进行 3D 手部识别。这个插件允许你检测和跟踪手部的 3D 位置、姿势和手势。以下是如何使用 apple_vision_hand_3d
插件的简要步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 apple_vision_hand_3d
插件的依赖:
dependencies:
flutter:
sdk: flutter
apple_vision_hand_3d: ^0.0.1 # 请检查最新版本
然后运行 flutter pub get
来获取依赖。
2. 导入插件
在你的 Dart 文件中导入插件:
import 'package:apple_vision_hand_3d/apple_vision_hand_3d.dart';
3. 初始化插件
在使用插件之前,你需要初始化它。通常,你可以在 initState
方法中进行初始化:
AppleVisionHand3D _handDetector;
[@override](/user/override)
void initState() {
super.initState();
_handDetector = AppleVisionHand3D();
}
4. 检测手部
你可以使用 detectHands
方法来检测手部。这个方法通常需要传入一个图像数据和一些配置参数。例如,你可以从相机或图像文件中获取图像数据:
Future<void> detectHands() async {
try {
// 获取图像数据
final imageData = ...; // 这里你需要提供图像数据
// 配置参数
final config = Hand3DConfiguration(
// 根据需要配置参数
);
// 开始检测手部
final results = await _handDetector.detectHands(imageData, config);
// 处理检测结果
if (results != null) {
for (var hand in results) {
print('Hand detected at position: ${hand.position}');
print('Hand pose: ${hand.pose}');
print('Hand gesture: ${hand.gesture}');
}
}
} catch (e) {
print('Error detecting hands: $e');
}
}
5. 处理检测结果
detectHands
方法会返回一个包含检测到手部的列表。每个手部对象通常包含以下信息:
position
: 手部在图像中的位置。pose
: 手部的姿势信息。gesture
: 手部的手势信息。
你可以根据这些信息来更新 UI 或执行其他操作。
6. 销毁资源
在不再需要使用手部检测时,应该释放资源:
[@override](/user/override)
void dispose() {
_handDetector.dispose();
super.dispose();
}
示例代码
以下是一个完整的示例代码:
import 'package:flutter/material.dart';
import 'package:apple_vision_hand_3d/apple_vision_hand_3d.dart';
class HandDetectionScreen extends StatefulWidget {
[@override](/user/override)
_HandDetectionScreenState createState() => _HandDetectionScreenState();
}
class _HandDetectionScreenState extends State<HandDetectionScreen> {
AppleVisionHand3D _handDetector;
[@override](/user/override)
void initState() {
super.initState();
_handDetector = AppleVisionHand3D();
detectHands();
}
Future<void> detectHands() async {
try {
// 获取图像数据
final imageData = ...; // 这里你需要提供图像数据
// 配置参数
final config = Hand3DConfiguration(
// 根据需要配置参数
);
// 开始检测手部
final results = await _handDetector.detectHands(imageData, config);
// 处理检测结果
if (results != null) {
for (var hand in results) {
print('Hand detected at position: ${hand.position}');
print('Hand pose: ${hand.pose}');
print('Hand gesture: ${hand.gesture}');
}
}
} catch (e) {
print('Error detecting hands: $e');
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hand Detection'),
),
body: Center(
child: Text('Hand Detection with Apple Vision'),
),
);
}
[@override](/user/override)
void dispose() {
_handDetector.dispose();
super.dispose();
}
}