Flutter人脸识别插件apple_vision_selfie的使用
Flutter人脸识别插件apple_vision_selfie的使用
Apple Vision Selfie Detection 是一个 Flutter 插件,使 Flutter 应用能够使用 Apple Vision Selfie Segmentation。此插件并非由 Apple 赞助或维护。作者是一些希望为 macOS 开发类似 Google ML Kit 的开发者。
需求
MacOS
- 最低 osx 部署目标:12.0
- Xcode 13 或更新版本
- Swift 5
- ML Kit 只支持 64 位架构(x86_64 和 arm64)
iOS
- 目前尚不支持开发
- 最低 ios 部署目标:13.0
- Xcode 13 或更新版本
- Swift 5
- ML Kit 只支持 64 位架构(x86_64 和 arm64)
入门指南
首先导入 'package:apple_vision_selfie/apple_vision_selfie.dart';
:
final GlobalKey cameraKey = GlobalKey(debugLabel: "cameraKey");
late AppleVisionSelfieController visionController = AppleVisionSelfieController();
InsertCamera camera = InsertCamera();
Size imageSize = const Size(640, 640 * 9 / 16);
String? deviceId;
bool loading = true;
List<Uint8List?>? selfieImage;
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, PictureFormat.png).then((data) {
selfieImage = 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),
),
if (selfieImage?[0] != null)
SizedBox(
width: imageSize.width,
height: imageSize.height,
child: Image.memory(selfieImage![0]!, fit: BoxFit.fill),
),
],
);
}
Widget loadingWidget() {
return Container(
width: deviceWidth,
height: deviceHeight,
color: Theme.of(context).canvasColor,
alignment: Alignment.center,
child: const CircularProgressIndicator(color: Colors.blue),
);
}
示例
以下是完整的示例代码,展示了如何使用 apple_vision_selfie
插件:
import 'package:apple_vision_selfie/apple_vision_selfie.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 VisionSelfie(),
);
}
}
class VisionSelfie extends StatefulWidget {
const VisionSelfie({
Key? key,
this.onScanned,
}) : super(key: key);
final Function(dynamic data)? onScanned;
[@override](/user/override)
_VisionSelfie createState() => _VisionSelfie();
}
class _VisionSelfie extends State<VisionSelfie> {
final GlobalKey cameraKey = GlobalKey(debugLabel: "cameraKey");
late AppleVisionSelfieController visionController = AppleVisionSelfieController();
InsertCamera camera = InsertCamera();
Size imageSize = const Size(640, 640 * 9 / 16);
String? deviceId;
bool loading = true;
List<Uint8List?>? selfieImage;
late double deviceWidth;
late double deviceHeight;
Uint8List? bg;
[@override](/user/override)
void initState() {
rootBundle.load('assets/WaterOnTheMoonFull.jpg').then((value) {
bg = value.buffer.asUint8List();
});
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(
SelfieSegmentationData(
image: image!,
imageSize: i.metadata!.size,
quality: SelfieQuality.fast,
backGround: bg,
),
).then((data) {
selfieImage = 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 ListView(
children: [
SizedBox(
width: 320,
height: 320 * 9 / 16,
child: loading ? Container() : CameraSetup(camera: camera, size: imageSize),
),
if (selfieImage?[0] != null)
SizedBox(
width: 320,
height: 320 * 9 / 16,
child: Stack(
children: [
Image.memory(
selfieImage![0]!,
fit: BoxFit.fitHeight,
),
],
),
),
],
);
}
Widget loadingWidget() {
return Container(
width: deviceWidth,
height: deviceHeight,
color: Theme.of(context).canvasColor,
alignment: Alignment.center,
child: const CircularProgressIndicator(color: Colors.blue),
);
}
}
更多关于Flutter人脸识别插件apple_vision_selfie的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复