Files
StackChan/app/lib/util/ml_kit_util.dart
袁智鸿 6314188835 prepare v1.1.4 release with native bridge and stability cleanups
- align Android/iOS native bridge implementations and audio handling paths
- improve Bluetooth provisioning/verification flow and related error handling
- refactor WebSocket, music, and device utility logic for more stable behavior
- clean up noisy debug logs and normalize comments across Flutter and native code
- update AR view, dance/agent/device pages, and platform integration details
2026-04-28 10:57:01 +08:00

67 lines
1.7 KiB
Dart

/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
import 'package:camera/camera.dart';
import 'package:flutter/cupertino.dart';
import 'package:google_mlkit_face_detection/google_mlkit_face_detection.dart';
class MlKitUtil {
MlKitUtil._internal();
static final MlKitUtil shared = MlKitUtil._internal();
late final FaceDetector _faceDetector = FaceDetector(
options: FaceDetectorOptions(
enableClassification: true,
enableTracking: true,
performanceMode: .accurate,
),
);
bool _isProcessing = false;
Future<void> testing(
CameraImage image,
int sensorOrientation,
Function(List<Face>) onFacesDetected,
) async {
if (_isProcessing) return;
_isProcessing = true;
try {
final inputImage = _convertToInputImage(image, sensorOrientation);
final faces = await _faceDetector.processImage(inputImage);
onFacesDetected(faces);
} catch (e) {
} finally {
_isProcessing = false;
}
}
InputImage _convertToInputImage(CameraImage image, int sensorOrientation) {
return InputImage.fromBytes(
bytes: image.planes.first.bytes,
metadata: InputImageMetadata(
size: Size(image.width.toDouble(), image.height.toDouble()),
rotation: _getRotation(sensorOrientation),
format: .nv21,
bytesPerRow: image.planes[0].bytesPerRow,
),
);
}
InputImageRotation _getRotation(int degrees) {
switch (degrees) {
case 90:
return InputImageRotation.rotation90deg;
case 180:
return InputImageRotation.rotation180deg;
case 270:
return InputImageRotation.rotation270deg;
default:
return InputImageRotation.rotation0deg;
}
}
}