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

Flutter中如何将单张人脸图片与图片列表进行匹配?

单张人脸与Firestore人脸列表比对实现方案(基于Regula flutter_face_api)

我需要将单张人脸图片和Firestore中存储的人脸图片列表做比对,但Regula的MatchFacesRequest仅支持两张图片对比(新图与单张存储图)。目前已获取到Firestore的员工快照数据列表:

List<Empl> emp =  snapshot.data!.docs.map((e) => e.data()).toList();

当前仅完成了和列表中第一个员工图片(emp[0])的比对,代码如下:

createButton(
      "Match",
      () {
        var image2 = Regula.MatchFacesImage();

        image2.bitmap = emp[0].imageBase64;
        image2.identifier = emp[0].name;
        image2.imageType = 1;

        setState(() => _similarity = "Processing...");
        var request = Regula.MatchFacesRequest();
        request.images = [image1, image2];

        Regula.FaceSDK.matchFaces(jsonEncode(request)).then(
          (value) {
            var response =
                Regula.MatchFacesResponse.fromJson(
                    json.decode(value));
            Regula.FaceSDK
                    .matchFacesSimilarityThresholdSplit(
                        jsonEncode(response!.detections),
                        0.75)
                .then(
              (str) {
                var split = Regula
                        .MatchFacesSimilarityThresholdSplit
                    .fromJson(json.decode(str));

                setState(
                  () => _similarity =
                      split!.matchedFaces.isNotEmpty
                          ? ((split.matchedFaces[0]!
                                          .similarity! *
                                      100)
                                  .toStringAsFixed(2) +
                              "%")
                          : "error",
                );
                // debugPrint(
                //     ' image index : ${split!.matchedFaces[0]!.first!.imageIndex!}');
              },
            );
          },
        );
      },
),

使用的是Regula的flutter_face_api插件,但插件文档不完善,官方文档仅介绍单张图片比对,未提及列表比对的实现方法。


解决方案:循环遍历列表逐一比对

由于官方API不支持批量比对,可通过循环遍历员工列表,将待比对图片image1依次与每个员工的图片做比对,最终筛选出符合阈值的结果。

实现代码示例

createButton(
  "Match All",
  () async {
    setState(() => _similarity = "Processing...");
    double highestSimilarity = 0.0;
    String matchedEmployee = "No match found";

    // 遍历所有员工图片执行比对
    for (var employee in emp) {
      var image2 = Regula.MatchFacesImage();
      image2.bitmap = employee.imageBase64;
      image2.identifier = employee.name;
      image2.imageType = 1;

      var request = Regula.MatchFacesRequest();
      request.images = [image1, image2];

      // 同步执行比对操作
      String matchResult = await Regula.FaceSDK.matchFaces(jsonEncode(request));
      var response = Regula.MatchFacesResponse.fromJson(json.decode(matchResult));
      
      String splitResult = await Regula.FaceSDK.matchFacesSimilarityThresholdSplit(
        jsonEncode(response!.detections),
        0.75
      );
      
      var split = Regula.MatchFacesSimilarityThresholdSplit.fromJson(json.decode(splitResult));
      
      // 记录相似度最高的员工
      if (split!.matchedFaces.isNotEmpty) {
        double currentSimilarity = split.matchedFaces[0]!.similarity! * 100;
        if (currentSimilarity > highestSimilarity) {
          highestSimilarity = currentSimilarity;
          matchedEmployee = "${employee.name} (${currentSimilarity.toStringAsFixed(2)}%)";
        }
      }
    }

    // 更新UI显示最终结果
    setState(() {
      _similarity = matchedEmployee;
    });
  },
),

优化建议

  • 若员工列表数量较多,建议将比对逻辑放在compute或独立隔离线程中,避免阻塞UI线程。
  • 可提前设置相似度阈值(如0.8),一旦找到符合阈值的结果就终止循环,提升执行效率。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 05:12:19