Refactor GestureRecognizerResult conversion for default initialization

This commit is contained in:
Kinar 2023-11-30 09:13:10 -08:00
parent d19d5a50be
commit 9a5aa1b360
4 changed files with 43 additions and 50 deletions

View File

@ -47,18 +47,15 @@ void CppConvertToGestureRecognizerResult(
CppCategory cpp_category; CppCategory cpp_category;
// Set fields from the Classification protobuf // Set fields from the Classification protobuf
if (classification.has_index()) { cpp_category.index =
cpp_category.index = classification.index(); classification.has_index() ? classification.index() : 0;
} cpp_category.score =
if (classification.has_score()) { classification.has_score() ? classification.score() : 0.0f;
cpp_category.score = classification.score(); cpp_category.category_name =
} classification.has_label() ? classification.label() : "";
if (classification.has_label()) { cpp_category.display_name = classification.has_display_name()
cpp_category.category_name = classification.label(); ? classification.display_name()
} : "";
if (classification.has_display_name()) {
cpp_category.display_name = classification.display_name();
}
CppConvertToCategory(cpp_category, &out->gestures[i][j]); CppConvertToCategory(cpp_category, &out->gestures[i][j]);
} }
@ -78,18 +75,15 @@ void CppConvertToGestureRecognizerResult(
CppCategory cpp_category; CppCategory cpp_category;
// Set fields from the Classification protobuf // Set fields from the Classification protobuf
if (classification.has_index()) { cpp_category.index =
cpp_category.index = classification.index(); classification.has_index() ? classification.index() : 0;
} cpp_category.score =
if (classification.has_score()) { classification.has_score() ? classification.score() : 0.0f;
cpp_category.score = classification.score(); cpp_category.category_name =
} classification.has_label() ? classification.label() : "";
if (classification.has_label()) { cpp_category.display_name = classification.has_display_name()
cpp_category.category_name = classification.label(); ? classification.display_name()
} : "";
if (classification.has_display_name()) {
cpp_category.display_name = classification.display_name();
}
CppConvertToCategory(cpp_category, &out->handedness[i][j]); CppConvertToCategory(cpp_category, &out->handedness[i][j]);
} }

View File

@ -138,10 +138,10 @@ GestureRecognizer* CppGestureRecognizerCreate(
return recognizer->release(); return recognizer->release();
} }
int CppGestureRecognizerRecognize(void* recognizer, const MpImage* image, int CppGestureRecognizerRecognize(void* recognizer, const MpImage& image,
GestureRecognizerResult* result, GestureRecognizerResult* result,
char** error_msg) { char** error_msg) {
if (image->type == MpImage::GPU_BUFFER) { if (image.type == MpImage::GPU_BUFFER) {
const absl::Status status = const absl::Status status =
absl::InvalidArgumentError("GPU Buffer not supported yet."); absl::InvalidArgumentError("GPU Buffer not supported yet.");
@ -150,9 +150,9 @@ int CppGestureRecognizerRecognize(void* recognizer, const MpImage* image,
} }
const auto img = CreateImageFromBuffer( const auto img = CreateImageFromBuffer(
static_cast<ImageFormat::Format>(image->image_frame.format), static_cast<ImageFormat::Format>(image.image_frame.format),
image->image_frame.image_buffer, image->image_frame.width, image.image_frame.image_buffer, image.image_frame.width,
image->image_frame.height); image.image_frame.height);
if (!img.ok()) { if (!img.ok()) {
ABSL_LOG(ERROR) << "Failed to create Image: " << img.status(); ABSL_LOG(ERROR) << "Failed to create Image: " << img.status();
@ -170,11 +170,11 @@ int CppGestureRecognizerRecognize(void* recognizer, const MpImage* image,
} }
int CppGestureRecognizerRecognizeForVideo(void* recognizer, int CppGestureRecognizerRecognizeForVideo(void* recognizer,
const MpImage* image, const MpImage& image,
int64_t timestamp_ms, int64_t timestamp_ms,
GestureRecognizerResult* result, GestureRecognizerResult* result,
char** error_msg) { char** error_msg) {
if (image->type == MpImage::GPU_BUFFER) { if (image.type == MpImage::GPU_BUFFER) {
absl::Status status = absl::Status status =
absl::InvalidArgumentError("GPU Buffer not supported yet"); absl::InvalidArgumentError("GPU Buffer not supported yet");
@ -183,9 +183,9 @@ int CppGestureRecognizerRecognizeForVideo(void* recognizer,
} }
const auto img = CreateImageFromBuffer( const auto img = CreateImageFromBuffer(
static_cast<ImageFormat::Format>(image->image_frame.format), static_cast<ImageFormat::Format>(image.image_frame.format),
image->image_frame.image_buffer, image->image_frame.width, image.image_frame.image_buffer, image.image_frame.width,
image->image_frame.height); image.image_frame.height);
if (!img.ok()) { if (!img.ok()) {
ABSL_LOG(ERROR) << "Failed to create Image: " << img.status(); ABSL_LOG(ERROR) << "Failed to create Image: " << img.status();
@ -202,9 +202,9 @@ int CppGestureRecognizerRecognizeForVideo(void* recognizer,
return 0; return 0;
} }
int CppGestureRecognizerRecognizeAsync(void* recognizer, const MpImage* image, int CppGestureRecognizerRecognizeAsync(void* recognizer, const MpImage& image,
int64_t timestamp_ms, char** error_msg) { int64_t timestamp_ms, char** error_msg) {
if (image->type == MpImage::GPU_BUFFER) { if (image.type == MpImage::GPU_BUFFER) {
absl::Status status = absl::Status status =
absl::InvalidArgumentError("GPU Buffer not supported yet"); absl::InvalidArgumentError("GPU Buffer not supported yet");
@ -213,9 +213,9 @@ int CppGestureRecognizerRecognizeAsync(void* recognizer, const MpImage* image,
} }
const auto img = CreateImageFromBuffer( const auto img = CreateImageFromBuffer(
static_cast<ImageFormat::Format>(image->image_frame.format), static_cast<ImageFormat::Format>(image.image_frame.format),
image->image_frame.image_buffer, image->image_frame.width, image.image_frame.image_buffer, image.image_frame.width,
image->image_frame.height); image.image_frame.height);
if (!img.ok()) { if (!img.ok()) {
ABSL_LOG(ERROR) << "Failed to create Image: " << img.status(); ABSL_LOG(ERROR) << "Failed to create Image: " << img.status();
@ -257,7 +257,7 @@ void* gesture_recognizer_create(struct GestureRecognizerOptions* options,
CppGestureRecognizerCreate(*options, error_msg); 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, GestureRecognizerResult* result,
char** error_msg) { char** error_msg) {
return mediapipe::tasks::c::vision::gesture_recognizer:: 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, int gesture_recognizer_recognize_for_video(void* recognizer,
const MpImage* image, const MpImage& image,
int64_t timestamp_ms, int64_t timestamp_ms,
GestureRecognizerResult* result, GestureRecognizerResult* result,
char** error_msg) { char** error_msg) {
@ -274,7 +274,7 @@ int gesture_recognizer_recognize_for_video(void* recognizer,
result, error_msg); 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) { int64_t timestamp_ms, char** error_msg) {
return mediapipe::tasks::c::vision::gesture_recognizer:: return mediapipe::tasks::c::vision::gesture_recognizer::
CppGestureRecognizerRecognizeAsync(recognizer, image, timestamp_ms, CppGestureRecognizerRecognizeAsync(recognizer, image, timestamp_ms,

View File

@ -100,7 +100,7 @@ MP_EXPORT void* gesture_recognizer_create(
// an error message (if `error_msg` is not `nullptr`). You must free the memory // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int gesture_recognizer_recognize_image( MP_EXPORT int gesture_recognizer_recognize_image(
void* recognizer, const MpImage* image, GestureRecognizerResult* result, void* recognizer, const MpImage& image, GestureRecognizerResult* result,
char** error_msg); char** error_msg);
// Performs gesture recognition on the provided video frame. // 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 // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int gesture_recognizer_recognize_for_video( 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); GestureRecognizerResult* result, char** error_msg);
// Sends live image data to gesture recognition, and the results will be // 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 // an error message (if `error_msg` is not `nullptr`). You must free the memory
// allocated for the error message. // allocated for the error message.
MP_EXPORT int gesture_recognizer_recognize_async(void* recognizer, MP_EXPORT int gesture_recognizer_recognize_async(void* recognizer,
const MpImage* image, const MpImage& image,
int64_t timestamp_ms, int64_t timestamp_ms,
char** error_msg); char** error_msg);

View File

@ -116,7 +116,7 @@ TEST(GestureRecognizerTest, ImageModeTest) {
.height = image_frame->Height()}}; .height = image_frame->Height()}};
GestureRecognizerResult result; GestureRecognizerResult result;
gesture_recognizer_recognize_image(recognizer, &mp_image, &result, gesture_recognizer_recognize_image(recognizer, mp_image, &result,
/* error_msg */ nullptr); /* error_msg */ nullptr);
MatchesGestureRecognizerResult(&result, kScorePrecision, kLandmarkPrecision); MatchesGestureRecognizerResult(&result, kScorePrecision, kLandmarkPrecision);
gesture_recognizer_close_result(&result); gesture_recognizer_close_result(&result);
@ -166,7 +166,7 @@ TEST(GestureRecognizerTest, VideoModeTest) {
for (int i = 0; i < kIterations; ++i) { for (int i = 0; i < kIterations; ++i) {
GestureRecognizerResult result; 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); /* error_msg */ nullptr);
MatchesGestureRecognizerResult(&result, kScorePrecision, MatchesGestureRecognizerResult(&result, kScorePrecision,
@ -242,7 +242,7 @@ TEST(GestureRecognizerTest, LiveStreamModeTest) {
.height = image_frame->Height()}}; .height = image_frame->Height()}};
for (int i = 0; i < kIterations; ++i) { 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), /* error_msg */ nullptr),
0); 0);
} }
@ -311,8 +311,7 @@ TEST(GestureRecognizerTest, FailedRecognitionHandling) {
const MpImage mp_image = {.type = MpImage::GPU_BUFFER, .gpu_buffer = {}}; const MpImage mp_image = {.type = MpImage::GPU_BUFFER, .gpu_buffer = {}};
GestureRecognizerResult result; GestureRecognizerResult result;
char* error_msg; char* error_msg;
gesture_recognizer_recognize_image(recognizer, &mp_image, &result, gesture_recognizer_recognize_image(recognizer, mp_image, &result, &error_msg);
&error_msg);
EXPECT_THAT(error_msg, HasSubstr("GPU Buffer not supported yet")); EXPECT_THAT(error_msg, HasSubstr("GPU Buffer not supported yet"));
free(error_msg); free(error_msg);
gesture_recognizer_close(recognizer, /* error_msg */ nullptr); gesture_recognizer_close(recognizer, /* error_msg */ nullptr);