Refactor GestureRecognizerResult conversion for default initialization
This commit is contained in:
		
							parent
							
								
									d19d5a50be
								
							
						
					
					
						commit
						9a5aa1b360
					
				| 
						 | 
				
			
			@ -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]);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<ImageFormat::Format>(image->image_frame.format),
 | 
			
		||||
      image->image_frame.image_buffer, image->image_frame.width,
 | 
			
		||||
      image->image_frame.height);
 | 
			
		||||
      static_cast<ImageFormat::Format>(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<ImageFormat::Format>(image->image_frame.format),
 | 
			
		||||
      image->image_frame.image_buffer, image->image_frame.width,
 | 
			
		||||
      image->image_frame.height);
 | 
			
		||||
      static_cast<ImageFormat::Format>(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<ImageFormat::Format>(image->image_frame.format),
 | 
			
		||||
      image->image_frame.image_buffer, image->image_frame.width,
 | 
			
		||||
      image->image_frame.height);
 | 
			
		||||
      static_cast<ImageFormat::Format>(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,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user