From 9a5aa1b360e9f42a365a8154bcf561c686d7fbf6 Mon Sep 17 00:00:00 2001 From: Kinar Date: Thu, 30 Nov 2023 09:13:10 -0800 Subject: [PATCH] Refactor GestureRecognizerResult conversion for default initialization --- .../gesture_recognizer_result_converter.cc | 42 ++++++++----------- .../gesture_recognizer/gesture_recognizer.cc | 36 ++++++++-------- .../gesture_recognizer/gesture_recognizer.h | 6 +-- .../gesture_recognizer_test.cc | 9 ++-- 4 files changed, 43 insertions(+), 50 deletions(-) diff --git a/mediapipe/tasks/c/components/containers/gesture_recognizer_result_converter.cc b/mediapipe/tasks/c/components/containers/gesture_recognizer_result_converter.cc index 6ac8b1370..5aca374fc 100644 --- a/mediapipe/tasks/c/components/containers/gesture_recognizer_result_converter.cc +++ b/mediapipe/tasks/c/components/containers/gesture_recognizer_result_converter.cc @@ -47,18 +47,15 @@ void CppConvertToGestureRecognizerResult( CppCategory cpp_category; // Set fields from the Classification protobuf - if (classification.has_index()) { - cpp_category.index = classification.index(); - } - if (classification.has_score()) { - cpp_category.score = classification.score(); - } - if (classification.has_label()) { - cpp_category.category_name = classification.label(); - } - if (classification.has_display_name()) { - cpp_category.display_name = classification.display_name(); - } + cpp_category.index = + classification.has_index() ? classification.index() : 0; + cpp_category.score = + classification.has_score() ? classification.score() : 0.0f; + cpp_category.category_name = + classification.has_label() ? classification.label() : ""; + cpp_category.display_name = classification.has_display_name() + ? classification.display_name() + : ""; CppConvertToCategory(cpp_category, &out->gestures[i][j]); } @@ -78,18 +75,15 @@ void CppConvertToGestureRecognizerResult( CppCategory cpp_category; // Set fields from the Classification protobuf - if (classification.has_index()) { - cpp_category.index = classification.index(); - } - if (classification.has_score()) { - cpp_category.score = classification.score(); - } - if (classification.has_label()) { - cpp_category.category_name = classification.label(); - } - if (classification.has_display_name()) { - cpp_category.display_name = classification.display_name(); - } + cpp_category.index = + classification.has_index() ? classification.index() : 0; + cpp_category.score = + classification.has_score() ? classification.score() : 0.0f; + cpp_category.category_name = + classification.has_label() ? classification.label() : ""; + cpp_category.display_name = classification.has_display_name() + ? classification.display_name() + : ""; CppConvertToCategory(cpp_category, &out->handedness[i][j]); } diff --git a/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer.cc b/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer.cc index f8c42dcaf..692e3776b 100644 --- a/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer.cc +++ b/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer.cc @@ -138,10 +138,10 @@ GestureRecognizer* CppGestureRecognizerCreate( return recognizer->release(); } -int CppGestureRecognizerRecognize(void* recognizer, const MpImage* image, +int CppGestureRecognizerRecognize(void* recognizer, const MpImage& image, GestureRecognizerResult* result, char** error_msg) { - if (image->type == MpImage::GPU_BUFFER) { + if (image.type == MpImage::GPU_BUFFER) { const absl::Status status = absl::InvalidArgumentError("GPU Buffer not supported yet."); @@ -150,9 +150,9 @@ int CppGestureRecognizerRecognize(void* recognizer, const MpImage* image, } const auto img = CreateImageFromBuffer( - static_cast(image->image_frame.format), - image->image_frame.image_buffer, image->image_frame.width, - image->image_frame.height); + static_cast(image.image_frame.format), + image.image_frame.image_buffer, image.image_frame.width, + image.image_frame.height); if (!img.ok()) { ABSL_LOG(ERROR) << "Failed to create Image: " << img.status(); @@ -170,11 +170,11 @@ int CppGestureRecognizerRecognize(void* recognizer, const MpImage* image, } int CppGestureRecognizerRecognizeForVideo(void* recognizer, - const MpImage* image, + const MpImage& image, int64_t timestamp_ms, GestureRecognizerResult* result, char** error_msg) { - if (image->type == MpImage::GPU_BUFFER) { + if (image.type == MpImage::GPU_BUFFER) { absl::Status status = absl::InvalidArgumentError("GPU Buffer not supported yet"); @@ -183,9 +183,9 @@ int CppGestureRecognizerRecognizeForVideo(void* recognizer, } const auto img = CreateImageFromBuffer( - static_cast(image->image_frame.format), - image->image_frame.image_buffer, image->image_frame.width, - image->image_frame.height); + static_cast(image.image_frame.format), + image.image_frame.image_buffer, image.image_frame.width, + image.image_frame.height); if (!img.ok()) { ABSL_LOG(ERROR) << "Failed to create Image: " << img.status(); @@ -202,9 +202,9 @@ int CppGestureRecognizerRecognizeForVideo(void* recognizer, return 0; } -int CppGestureRecognizerRecognizeAsync(void* recognizer, const MpImage* image, +int CppGestureRecognizerRecognizeAsync(void* recognizer, const MpImage& image, int64_t timestamp_ms, char** error_msg) { - if (image->type == MpImage::GPU_BUFFER) { + if (image.type == MpImage::GPU_BUFFER) { absl::Status status = absl::InvalidArgumentError("GPU Buffer not supported yet"); @@ -213,9 +213,9 @@ int CppGestureRecognizerRecognizeAsync(void* recognizer, const MpImage* image, } const auto img = CreateImageFromBuffer( - static_cast(image->image_frame.format), - image->image_frame.image_buffer, image->image_frame.width, - image->image_frame.height); + static_cast(image.image_frame.format), + image.image_frame.image_buffer, image.image_frame.width, + image.image_frame.height); if (!img.ok()) { ABSL_LOG(ERROR) << "Failed to create Image: " << img.status(); @@ -257,7 +257,7 @@ void* gesture_recognizer_create(struct GestureRecognizerOptions* options, CppGestureRecognizerCreate(*options, error_msg); } -int gesture_recognizer_recognize_image(void* recognizer, const MpImage* image, +int gesture_recognizer_recognize_image(void* recognizer, const MpImage& image, GestureRecognizerResult* result, char** error_msg) { return mediapipe::tasks::c::vision::gesture_recognizer:: @@ -265,7 +265,7 @@ int gesture_recognizer_recognize_image(void* recognizer, const MpImage* image, } int gesture_recognizer_recognize_for_video(void* recognizer, - const MpImage* image, + const MpImage& image, int64_t timestamp_ms, GestureRecognizerResult* result, char** error_msg) { @@ -274,7 +274,7 @@ int gesture_recognizer_recognize_for_video(void* recognizer, result, error_msg); } -int gesture_recognizer_recognize_async(void* recognizer, const MpImage* image, +int gesture_recognizer_recognize_async(void* recognizer, const MpImage& image, int64_t timestamp_ms, char** error_msg) { return mediapipe::tasks::c::vision::gesture_recognizer:: CppGestureRecognizerRecognizeAsync(recognizer, image, timestamp_ms, diff --git a/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer.h b/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer.h index 39f4a1734..4d59df62d 100644 --- a/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer.h +++ b/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer.h @@ -100,7 +100,7 @@ MP_EXPORT void* gesture_recognizer_create( // an error message (if `error_msg` is not `nullptr`). You must free the memory // allocated for the error message. MP_EXPORT int gesture_recognizer_recognize_image( - void* recognizer, const MpImage* image, GestureRecognizerResult* result, + void* recognizer, const MpImage& image, GestureRecognizerResult* result, char** error_msg); // Performs gesture recognition on the provided video frame. @@ -113,7 +113,7 @@ MP_EXPORT int gesture_recognizer_recognize_image( // an error message (if `error_msg` is not `nullptr`). You must free the memory // allocated for the error message. MP_EXPORT int gesture_recognizer_recognize_for_video( - void* recognizer, const MpImage* image, int64_t timestamp_ms, + void* recognizer, const MpImage& image, int64_t timestamp_ms, GestureRecognizerResult* result, char** error_msg); // Sends live image data to gesture recognition, and the results will be @@ -135,7 +135,7 @@ MP_EXPORT int gesture_recognizer_recognize_for_video( // an error message (if `error_msg` is not `nullptr`). You must free the memory // allocated for the error message. MP_EXPORT int gesture_recognizer_recognize_async(void* recognizer, - const MpImage* image, + const MpImage& image, int64_t timestamp_ms, char** error_msg); diff --git a/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer_test.cc b/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer_test.cc index ce3f5df5a..bd95d7e52 100644 --- a/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer_test.cc +++ b/mediapipe/tasks/c/vision/gesture_recognizer/gesture_recognizer_test.cc @@ -116,7 +116,7 @@ TEST(GestureRecognizerTest, ImageModeTest) { .height = image_frame->Height()}}; GestureRecognizerResult result; - gesture_recognizer_recognize_image(recognizer, &mp_image, &result, + gesture_recognizer_recognize_image(recognizer, mp_image, &result, /* error_msg */ nullptr); MatchesGestureRecognizerResult(&result, kScorePrecision, kLandmarkPrecision); gesture_recognizer_close_result(&result); @@ -166,7 +166,7 @@ TEST(GestureRecognizerTest, VideoModeTest) { for (int i = 0; i < kIterations; ++i) { GestureRecognizerResult result; - gesture_recognizer_recognize_for_video(recognizer, &mp_image, i, &result, + gesture_recognizer_recognize_for_video(recognizer, mp_image, i, &result, /* error_msg */ nullptr); MatchesGestureRecognizerResult(&result, kScorePrecision, @@ -242,7 +242,7 @@ TEST(GestureRecognizerTest, LiveStreamModeTest) { .height = image_frame->Height()}}; for (int i = 0; i < kIterations; ++i) { - EXPECT_GE(gesture_recognizer_recognize_async(recognizer, &mp_image, i, + EXPECT_GE(gesture_recognizer_recognize_async(recognizer, mp_image, i, /* error_msg */ nullptr), 0); } @@ -311,8 +311,7 @@ TEST(GestureRecognizerTest, FailedRecognitionHandling) { const MpImage mp_image = {.type = MpImage::GPU_BUFFER, .gpu_buffer = {}}; GestureRecognizerResult result; char* error_msg; - gesture_recognizer_recognize_image(recognizer, &mp_image, &result, - &error_msg); + gesture_recognizer_recognize_image(recognizer, mp_image, &result, &error_msg); EXPECT_THAT(error_msg, HasSubstr("GPU Buffer not supported yet")); free(error_msg); gesture_recognizer_close(recognizer, /* error_msg */ nullptr);