如何在Flutter中将单个人脸图像与图像列表进行匹配

2ledvvac  于 2023-03-24  发布在  Flutter
关注(0)|答案(1)|浏览(320)

我试图比较一个单一的面部图像与存储在firestore中的面部图像列表。但MatchFacesRequest只允许两个图像进行比较。第一个新图像和第二个已经存储。这里是我想要比较的快照数据的员工列表

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

这里,图像1是需要与图像列表进行比较的最新图像,图像2应该是EMP图像列表。

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_apiplugin,但这没有正确的文档。here是面部匹配的文档。但仅用于单个图像而不是图像列表。

pjngdqdw

pjngdqdw1#

您只需将正在比较的图像添加到请求的图像列表中:

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

之后,您可以检查匹配的面孔:

FaceSDK.matchFacesSimilarityThresholdSplit(jsonEncode(response.results), 0.75).then((splitResponse) {
    var split = Regula.MatchFacesSimilarityThresholdSplit.fromJson(json.decode(splitResponse));
    // ... check split.matchedFaces for the matched faces.
    // ... check split.unmatchedFaces for the matched faces.
});

这将产生如下两个MatchFaces列表:

我,实际上,改变了GIST中三个图像的官方示例,当然,如果你需要更多关于它们的信息,你只需要在响应的循环中工作。

var request = new Regula.MatchFacesRequest();
    request.images = [image1, image2, image3];
    Regula.FaceSDK.matchFaces(jsonEncode(request)).then((value) {
      var response = Regula.MatchFacesResponse.fromJson(json.decode(value));
      Regula.FaceSDK.matchFacesSimilarityThresholdSplit(
              jsonEncode(response!.results), 0.75)
          .then((str) {
        var split = Regula.MatchFacesSimilarityThresholdSplit.fromJson(
            json.decode(str));
        setState(() {
          if (split!.matchedFaces.length > 0) {
            _similarity = '';
            for (var i = 0; i < split.matchedFaces.length; i++) {
              _similarity +=
                  "${i + 1}. ${(split.matchedFaces[i]!.similarity! * 100).toStringAsFixed(2)} % \n";
            }
          } else {
            _similarity = "error";
          }
        });
      });
    });

Full main.dart GIST here

相关问题