You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中使用google_mlkit姿势检测无绘制效果问题求助

Flutter google_mlkit_pose_detection 无姿势绘制问题修复

以下是导致你看不到姿势绘制的几个核心问题及对应修复:

1. 生命周期方法拼写错误

home_screen.dart 中把 initState 误写成 instState,修正后确保生命周期正常执行:

@override
void initState() {
  super.initState();
}

2. PoseDetector 配置优化

默认的轻量模型可能导致检测精度不足,明确指定基础模型并调整置信度阈值,提升检测成功率:

final PoseDetector _poseDetector = PoseDetector(
  options: PoseDetectorOptions(
    model: PoseDetectionModel.base,
    minPoseDetectionConfidence: 0.5,
    minPoseTrackingConfidence: 0.5,
    minPoseLandmarkConfidence: 0.5,
  ),
);

3. 坐标转换逻辑修正

painter.dart 中的坐标转换逻辑存在平台判断错误,导致绘制线条偏移到屏幕外,修改如下:

double translateX(double x, InputImageRotation rotation, Size size, Size absoluteImageSize) {
  double scaledX;
  switch (rotation) {
    case InputImageRotation.rotation90deg:
    case InputImageRotation.rotation270deg:
      scaledX = x * size.width / absoluteImageSize.height;
      break;
    default:
      scaledX = x * size.width / absoluteImageSize.width;
      break;
  }
  if (rotation == InputImageRotation.rotation270deg) {
    scaledX = size.width - scaledX;
  }
  return scaledX;
}

double translateY(double y, InputImageRotation rotation, Size size, Size absoluteImageSize) {
  double scaledY;
  switch (rotation) {
    case InputImageRotation.rotation90deg:
    case InputImageRotation.rotation270deg:
      scaledY = y * size.height / absoluteImageSize.width;
      break;
    default:
      scaledY = y * size.height / absoluteImageSize.height;
      break;
  }
  return scaledY;
}

4. 同步 CameraPreview 与 CustomPaint 缩放比例

CameraPreview 会根据屏幕尺寸自动拉伸,而 CustomPaint 默认使用父容器尺寸导致绘制错位,修改 camera_view.dart 的 build 方法:

@override
Widget build(BuildContext context) {
  if (!_controller!.value.isInitialized) {
    return Container();
  }
  final previewSize = _controller!.value.previewSize!;
  final size = MediaQuery.of(context).size;
  final scale = size.aspectRatio > previewSize.aspectRatio 
      ? size.width / previewSize.width 
      : size.height / previewSize.height;

  return Container(
    alignment: Alignment.center,
    color: Colors.black,
    child: Stack(
      children: <Widget>[
        Transform.scale(
          scale: scale,
          child: Center(child: CameraPreview(_controller!)),
        ),
        if (widget.customPaint != null) 
          Transform.scale(
            scale: scale,
            child: Center(child: widget.customPaint!),
          ),
      ],
    ),
  );
}

5. 添加调试日志确认检测结果

在 home_screen.dart 的 processImage 方法中添加日志,排查是否检测到姿势:

final poses = await _poseDetector.processImage(inputImage);
print('检测到姿势数量: ${poses.length}');
if (poses.isEmpty) {
  print('未检测到任何姿势,请调整拍摄角度或光线');
}

完成以上修改后重新运行项目,即可看到黄色姿势线条正常绘制在相机画面上。

内容的提问来源于stack exchange,提问作者fattty_R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 23:10:55