From 6fab3a8b85c66dfa44723116dca27414c47d6ed3 Mon Sep 17 00:00:00 2001 From: Kinar Date: Wed, 13 Dec 2023 19:59:36 -0800 Subject: [PATCH] Simplified copying data from Eigen matrix to C-style matrix and addressed some issues --- .../components/containers/matrix_converter.cc | 12 +++++---- .../face_landmarker/face_landmarker_result.h | 14 +++++----- .../face_landmarker/face_landmarker_test.cc | 26 +++++++++---------- .../hand_landmarker/hand_landmarker_test.cc | 16 +++++++----- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/mediapipe/tasks/c/components/containers/matrix_converter.cc b/mediapipe/tasks/c/components/containers/matrix_converter.cc index 6d823a424..9320178cb 100644 --- a/mediapipe/tasks/c/components/containers/matrix_converter.cc +++ b/mediapipe/tasks/c/components/containers/matrix_converter.cc @@ -27,11 +27,13 @@ void CppConvertToMatrix(const Eigen::MatrixXf& in, ::Matrix* out) { out->cols = in.cols(); out->data = new float[out->rows * out->cols]; - // Copy data from Eigen matrix to C matrix in column-major order - for (int col = 0; col < out->cols; ++col) { - for (int row = 0; row < out->rows; ++row) { - out->data[col * out->rows + row] = in(row, col); - } + // Copy data from Eigen matrix to C-style matrix. + // This operation copies the elements sequentially as they appear in the Eigen + // matrix's internal storage, regardless of whether it's stored in row-major + // or column-major order and ensures the integrity of data during the + // transfer. + for (int i = 0; i < out->rows * out->cols; ++i) { + out->data[i] = in.data()[i]; } } diff --git a/mediapipe/tasks/c/vision/face_landmarker/face_landmarker_result.h b/mediapipe/tasks/c/vision/face_landmarker/face_landmarker_result.h index 27d698d13..0d86b7956 100644 --- a/mediapipe/tasks/c/vision/face_landmarker/face_landmarker_result.h +++ b/mediapipe/tasks/c/vision/face_landmarker/face_landmarker_result.h @@ -33,19 +33,19 @@ extern "C" { // The hand landmarker result from HandLandmarker, where each vector // element represents a single hand detected in the image. struct FaceLandmarkerResult { - // Optional face blendshapes results. - struct Categories* face_blendshapes; - - // The number of elements in the face_blendshapes array. - uint32_t face_blendshapes_count; - // Detected face landmarks in normalized image coordinates. struct NormalizedLandmarks* face_landmarks; // The number of elements in the face_landmarks array. uint32_t face_landmarks_count; - // Optional facial transformation matrix. + // Optional face blendshapes results. + struct Categories* face_blendshapes; + + // The number of elements in the face_blendshapes array. + uint32_t face_blendshapes_count; + + // Optional facial transformation matrixes. struct Matrix* facial_transformation_matrixes; // The number of elements in the facial_transformation_matrixes array. diff --git a/mediapipe/tasks/c/vision/face_landmarker/face_landmarker_test.cc b/mediapipe/tasks/c/vision/face_landmarker/face_landmarker_test.cc index d96d7f32a..fb2e1edff 100644 --- a/mediapipe/tasks/c/vision/face_landmarker/face_landmarker_test.cc +++ b/mediapipe/tasks/c/vision/face_landmarker/face_landmarker_test.cc @@ -48,10 +48,10 @@ std::string GetFullPath(absl::string_view file_name) { return JoinPath("./", kTestDataDirectory, file_name); } -void MatchesFaceLandmarkerResult(FaceLandmarkerResult* result, - const float blendshapes_precision, - const float landmark_precision, - const float matrix_precison) { +void ExpectFaceLandmarkerResultCorrect(FaceLandmarkerResult* result, + const float blendshapes_precision, + const float landmark_precision, + const float matrix_precison) { // Expects to have the same number of faces detected. EXPECT_EQ(result->face_blendshapes_count, 1); @@ -115,9 +115,9 @@ TEST(FaceLandmarkerTest, ImageModeTest) { FaceLandmarkerResult result; face_landmarker_detect_image(landmarker, mp_image, &result, /* error_msg */ nullptr); - MatchesFaceLandmarkerResult(&result, kBlendshapesPrecision, - kLandmarksPrecision, - kFacialTransformationMatrixPrecision); + ExpectFaceLandmarkerResultCorrect(&result, kBlendshapesPrecision, + kLandmarksPrecision, + kFacialTransformationMatrixPrecision); face_landmarker_close_result(&result); face_landmarker_close(landmarker, /* error_msg */ nullptr); } @@ -157,9 +157,9 @@ TEST(FaceLandmarkerTest, VideoModeTest) { face_landmarker_detect_for_video(landmarker, mp_image, i, &result, /* error_msg */ nullptr); - MatchesFaceLandmarkerResult(&result, kBlendshapesPrecision, - kLandmarksPrecision, - kFacialTransformationMatrixPrecision); + ExpectFaceLandmarkerResultCorrect(&result, kBlendshapesPrecision, + kLandmarksPrecision, + kFacialTransformationMatrixPrecision); face_landmarker_close_result(&result); } face_landmarker_close(landmarker, /* error_msg */ nullptr); @@ -176,9 +176,9 @@ struct LiveStreamModeCallback { int64_t timestamp, char* error_msg) { ASSERT_NE(landmarker_result, nullptr); ASSERT_EQ(error_msg, nullptr); - MatchesFaceLandmarkerResult(landmarker_result, kBlendshapesPrecision, - kLandmarksPrecision, - kFacialTransformationMatrixPrecision); + ExpectFaceLandmarkerResultCorrect(landmarker_result, kBlendshapesPrecision, + kLandmarksPrecision, + kFacialTransformationMatrixPrecision); EXPECT_GT(image.image_frame.width, 0); EXPECT_GT(image.image_frame.height, 0); EXPECT_GT(timestamp, last_timestamp); diff --git a/mediapipe/tasks/c/vision/hand_landmarker/hand_landmarker_test.cc b/mediapipe/tasks/c/vision/hand_landmarker/hand_landmarker_test.cc index ed7b4646f..7cd8ec164 100644 --- a/mediapipe/tasks/c/vision/hand_landmarker/hand_landmarker_test.cc +++ b/mediapipe/tasks/c/vision/hand_landmarker/hand_landmarker_test.cc @@ -47,9 +47,9 @@ std::string GetFullPath(absl::string_view file_name) { return JoinPath("./", kTestDataDirectory, file_name); } -void MatchesHandLandmarkerResult(HandLandmarkerResult* result, - const float score_precision, - const float landmark_precision) { +void ExpectHandLandmarkerResultCorrect(HandLandmarkerResult* result, + const float score_precision, + const float landmark_precision) { // Expects to have the same number of hands detected. EXPECT_EQ(result->handedness_count, 1); @@ -104,7 +104,8 @@ TEST(HandLandmarkerTest, ImageModeTest) { HandLandmarkerResult result; hand_landmarker_detect_image(landmarker, mp_image, &result, /* error_msg */ nullptr); - MatchesHandLandmarkerResult(&result, kScorePrecision, kLandmarkPrecision); + ExpectHandLandmarkerResultCorrect(&result, kScorePrecision, + kLandmarkPrecision); hand_landmarker_close_result(&result); hand_landmarker_close(landmarker, /* error_msg */ nullptr); } @@ -141,7 +142,8 @@ TEST(HandLandmarkerTest, VideoModeTest) { hand_landmarker_detect_for_video(landmarker, mp_image, i, &result, /* error_msg */ nullptr); - MatchesHandLandmarkerResult(&result, kScorePrecision, kLandmarkPrecision); + ExpectHandLandmarkerResultCorrect(&result, kScorePrecision, + kLandmarkPrecision); hand_landmarker_close_result(&result); } hand_landmarker_close(landmarker, /* error_msg */ nullptr); @@ -158,8 +160,8 @@ struct LiveStreamModeCallback { int64_t timestamp, char* error_msg) { ASSERT_NE(landmarker_result, nullptr); ASSERT_EQ(error_msg, nullptr); - MatchesHandLandmarkerResult(landmarker_result, kScorePrecision, - kLandmarkPrecision); + ExpectHandLandmarkerResultCorrect(landmarker_result, kScorePrecision, + kLandmarkPrecision); EXPECT_GT(image.image_frame.width, 0); EXPECT_GT(image.image_frame.height, 0); EXPECT_GT(timestamp, last_timestamp);