diff --git a/mediapipe/tasks/c/vision/image_embedder/BUILD b/mediapipe/tasks/c/vision/image_embedder/BUILD new file mode 100644 index 000000000..3300b4a0a --- /dev/null +++ b/mediapipe/tasks/c/vision/image_embedder/BUILD @@ -0,0 +1,70 @@ +# Copyright 2023 The MediaPipe Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +package(default_visibility = ["//mediapipe/tasks:internal"]) + +licenses(["notice"]) + +cc_library( + name = "image_embedder_lib", + srcs = ["image_embedder.cc"], + hdrs = ["image_embedder.h"], + visibility = ["//visibility:public"], + deps = [ + "//mediapipe/framework/formats:image", + "//mediapipe/framework/formats:image_frame", + "//mediapipe/tasks/c/components/containers:embedding_result", + "//mediapipe/tasks/c/components/containers:embedding_result_converter", + "//mediapipe/tasks/c/components/processors:embedder_options", + "//mediapipe/tasks/c/components/processors:embedder_options_converter", + "//mediapipe/tasks/c/core:base_options", + "//mediapipe/tasks/c/core:base_options_converter", + "//mediapipe/tasks/cc/vision/image_embedder", + "//mediapipe/tasks/cc/vision/utils:image_utils", + "@com_google_absl//absl/log:absl_log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings:str_format", + "@com_google_absl//absl/time", + ], + alwayslink = 1, +) + +cc_test( + name = "image_embedder_test", + srcs = ["image_embedder_test.cc"], + data = [ + "//mediapipe/framework/formats:image_frame_opencv", + "//mediapipe/framework/port:opencv_core", + "//mediapipe/framework/port:opencv_imgproc", + "//mediapipe/tasks/testdata/vision:test_images", + "//mediapipe/tasks/testdata/vision:test_models", + ], + linkstatic = 1, + deps = [ + ":image_embedder_lib", + "//mediapipe/framework/deps:file_path", + "//mediapipe/framework/formats:image", + "//mediapipe/framework/formats:image_frame", + "//mediapipe/framework/formats:image_frame_opencv", + "//mediapipe/framework/port:gtest", + "//mediapipe/framework/port:opencv_core", + "//mediapipe/framework/port:opencv_imgproc", + "//mediapipe/tasks/c/components/containers:category", + "//mediapipe/tasks/cc/vision/utils:image_utils", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/strings", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/mediapipe/tasks/c/vision/image_embedder/image_embedder.cc b/mediapipe/tasks/c/vision/image_embedder/image_embedder.cc new file mode 100644 index 000000000..4b42034cb --- /dev/null +++ b/mediapipe/tasks/c/vision/image_embedder/image_embedder.cc @@ -0,0 +1,143 @@ +/* Copyright 2023 The MediaPipe Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include "mediapipe/tasks/c/vision/image_embedder/image_embedder.h" + +#include +#include + +#include "absl/log/absl_log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "mediapipe/framework/formats/image.h" +#include "mediapipe/framework/formats/image_frame.h" +#include "mediapipe/tasks/c/components/containers/embedding_result_converter.h" +#include "mediapipe/tasks/c/components/processors/embedder_options_converter.h" +#include "mediapipe/tasks/c/core/base_options_converter.h" +#include "mediapipe/tasks/cc/vision/image_embedder/image_embedder.h" +#include "mediapipe/tasks/cc/vision/utils/image_utils.h" + +namespace mediapipe::tasks::c::vision::image_embedder { + +namespace { + +using ::mediapipe::tasks::c::components::containers::CppCloseEmbeddingResult; +using ::mediapipe::tasks::c::components::containers:: + CppConvertToEmbeddingResult; +using ::mediapipe::tasks::c::components::processors:: + CppConvertToEmbedderOptions; +using ::mediapipe::tasks::c::core::CppConvertToBaseOptions; +using ::mediapipe::tasks::vision::CreateImageFromBuffer; +using ::mediapipe::tasks::vision::image_embedder::ImageEmbedder; + +int CppProcessError(absl::Status status, char** error_msg) { + if (error_msg) { + *error_msg = strdup(status.ToString().c_str()); + } + return status.raw_code(); +} + +} // namespace + +ImageEmbedder* CppImageEmbedderCreate(const ImageEmbedderOptions& options, + char** error_msg) { + auto cpp_options = std::make_unique< + ::mediapipe::tasks::vision::image_embedder::ImageEmbedderOptions>(); + + CppConvertToBaseOptions(options.base_options, &cpp_options->base_options); + CppConvertToEmbedderOptions(options.embedder_options, + &cpp_options->embedder_options); + + auto embedder = ImageEmbedder::Create(std::move(cpp_options)); + if (!embedder.ok()) { + ABSL_LOG(ERROR) << "Failed to create ImageEmbedder: " << embedder.status(); + CppProcessError(embedder.status(), error_msg); + return nullptr; + } + return embedder->release(); +} + +int CppImageEmbedderEmbed(void* embedder, const MpImage* image, + ImageEmbedderResult* result, char** error_msg) { + if (image->type == MpImage::GPU_BUFFER) { + absl::Status status = + absl::InvalidArgumentError("gpu buffer not supported yet"); + + ABSL_LOG(ERROR) << "Classification failed: " << status.message(); + return CppProcessError(status, error_msg); + } + + const auto img = CreateImageFromBuffer( + 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(); + return CppProcessError(img.status(), error_msg); + } + + auto cpp_embedder = static_cast(embedder); + auto cpp_result = cpp_embedder->Embed(*img); + if (!cpp_result.ok()) { + ABSL_LOG(ERROR) << "Classification failed: " << cpp_result.status(); + return CppProcessError(cpp_result.status(), error_msg); + } + CppConvertToEmbeddingResult(*cpp_result, result); + return 0; +} + +void CppImageEmbedderCloseResult(ImageEmbedderResult* result) { + CppCloseEmbeddingResult(result); +} + +int CppImageEmbedderClose(void* embedder, char** error_msg) { + auto cpp_embedder = static_cast(embedder); + auto result = cpp_embedder->Close(); + if (!result.ok()) { + ABSL_LOG(ERROR) << "Failed to close ImageEmbedder: " << result; + return CppProcessError(result, error_msg); + } + delete cpp_embedder; + return 0; +} + +} // namespace mediapipe::tasks::c::vision::image_embedder + +extern "C" { + +void* image_embedder_create(struct ImageEmbedderOptions* options, + char** error_msg) { + return mediapipe::tasks::c::vision::image_embedder::CppImageEmbedderCreate( + *options, error_msg); +} + +int image_embedder_embed_image(void* embedder, const MpImage* image, + ImageEmbedderResult* result, char** error_msg) { + return mediapipe::tasks::c::vision::image_embedder::CppImageEmbedderEmbed( + embedder, image, result, error_msg); +} + +void image_embedder_close_result(ImageEmbedderResult* result) { + mediapipe::tasks::c::vision::image_embedder::CppImageEmbedderCloseResult( + result); +} + +int image_embedder_close(void* embedder, char** error_ms) { + return mediapipe::tasks::c::vision::image_embedder::CppImageEmbedderClose( + embedder, error_ms); +} + +} // extern "C" diff --git a/mediapipe/tasks/c/vision/image_embedder/image_embedder.h b/mediapipe/tasks/c/vision/image_embedder/image_embedder.h new file mode 100644 index 000000000..cefe97b1b --- /dev/null +++ b/mediapipe/tasks/c/vision/image_embedder/image_embedder.h @@ -0,0 +1,133 @@ +/* Copyright 2023 The MediaPipe Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef MEDIAPIPE_TASKS_C_VISION_IMAGE_EMBEDDER_IMAGE_EMBEDDER_H_ +#define MEDIAPIPE_TASKS_C_VISION_IMAGE_EMBEDDER_IMAGE_EMBEDDER_H_ + +#include + +#include "mediapipe/tasks/c/components/containers/embedding_result.h" +#include "mediapipe/tasks/c/components/processors/embedder_options.h" +#include "mediapipe/tasks/c/core/base_options.h" + +#ifndef MP_EXPORT +#define MP_EXPORT __attribute__((visibility("default"))) +#endif // MP_EXPORT + +#ifdef __cplusplus +extern "C" { +#endif + +typedef EmbeddingResult ImageEmbedderResult; + +// Supported image formats. +enum ImageFormat { + UNKNOWN = 0, + SRGB = 1, + SRGBA = 2, + GRAY8 = 3, + SBGRA = 11 // compatible with Flutter `bgra8888` format. +}; + +// Supported processing modes. +enum RunningMode { + IMAGE = 1, + VIDEO = 2, + LIVE_STREAM = 3, +}; + +// Structure to hold image frame. +struct ImageFrame { + enum ImageFormat format; + const uint8_t* image_buffer; + int width; + int height; +}; + +// TODO: Add GPU buffer declaration and proccessing logic for it. +struct GpuBuffer { + int width; + int height; +}; + +// The object to contain an image, realizes `OneOf` concept. +struct MpImage { + enum { IMAGE_FRAME, GPU_BUFFER } type; + union { + struct ImageFrame image_frame; + struct GpuBuffer gpu_buffer; + }; +}; + +// The options for configuring a MediaPipe image embedder task. +struct ImageEmbedderOptions { + // Base options for configuring MediaPipe Tasks, such as specifying the model + // file with metadata, accelerator options, op resolver, etc. + struct BaseOptions base_options; + + // The running mode of the task. Default to the image mode. + // Image embedder has three running modes: + // 1) The image mode for embedding image on single image inputs. + // 2) The video mode for embedding image on the decoded frames of a video. + // 3) The live stream mode for embedding image on the live stream of input + // data, such as from camera. In this mode, the "result_callback" below must + // be specified to receive the embedding results asynchronously. + RunningMode running_mode; + + // Options for configuring the embedder behavior, such as l2_normalize and + // quantize. + struct EmbedderOptions embedder_options; + + // The user-defined result callback for processing live stream data. + // The result callback should only be specified when the running mode is set + // to RunningMode::LIVE_STREAM. + typedef void (*result_callback_fn)(ImageEmbedderResult*, const MpImage*, + int64_t); + result_callback_fn result_callback; +}; + +// Creates an ImageEmbedder from provided `options`. +// Returns a pointer to the image embedder on success. +// If an error occurs, returns `nullptr` and sets the error parameter to an +// an error message (if `error_msg` is not nullptr). You must free the memory +// allocated for the error message. +MP_EXPORT void* image_embedder_create(struct ImageEmbedderOptions* options, + char** error_msg = nullptr); + +// Performs embedding extraction on the input `image`. Returns `0` on success. +// If an error occurs, returns an error code and sets the error parameter to an +// an error message (if `error_msg` is not nullptr). You must free the memory +// allocated for the error message. +// +// TODO: Add API for video and live stream processing. +MP_EXPORT int image_embedder_embed_image(void* embedder, const MpImage* image, + ImageEmbedderResult* result, + char** error_msg = nullptr); + +// Frees the memory allocated inside a ImageEmbedderResult result. +// Does not free the result pointer itself. +MP_EXPORT void image_embedder_close_result(ImageEmbedderResult* result); + +// Frees image embedder. +// If an error occurs, returns an error code and sets the error parameter to an +// an error message (if `error_msg` is not nullptr). You must free the memory +// allocated for the error message. +MP_EXPORT int image_embedder_close(void* embedder, char** error_msg = nullptr); + +#ifdef __cplusplus +} // extern C +#endif + +#endif // MEDIAPIPE_TASKS_C_VISION_IMAGE_EMBEDDER_IMAGE_EMBEDDER_H_ diff --git a/mediapipe/tasks/c/vision/image_embedder/image_embedder_test.cc b/mediapipe/tasks/c/vision/image_embedder/image_embedder_test.cc new file mode 100644 index 000000000..fb6b0b628 --- /dev/null +++ b/mediapipe/tasks/c/vision/image_embedder/image_embedder_test.cc @@ -0,0 +1,119 @@ +/* Copyright 2023 The MediaPipe Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include "mediapipe/tasks/c/vision/image_embedder/image_embedder.h" + +#include +#include + +#include "absl/flags/flag.h" +#include "absl/strings/string_view.h" +#include "mediapipe/framework/deps/file_path.h" +#include "mediapipe/framework/formats/image.h" +#include "mediapipe/framework/port/gmock.h" +#include "mediapipe/framework/port/gtest.h" +#include "mediapipe/tasks/c/components/containers/category.h" +#include "mediapipe/tasks/cc/vision/utils/image_utils.h" + +namespace { + +using ::mediapipe::file::JoinPath; +using ::mediapipe::tasks::vision::DecodeImageFromFile; +using testing::HasSubstr; + +constexpr char kTestDataDirectory[] = "/mediapipe/tasks/testdata/vision/"; +constexpr char kModelName[] = "mobilenet_v3_small_100_224_embedder.tflite"; +constexpr char kImageFile[] = "burger.jpg"; +constexpr float kPrecision = 1e-6; + +std::string GetFullPath(absl::string_view file_name) { + return JoinPath("./", kTestDataDirectory, file_name); +} + +TEST(ImageEmbedderTest, SmokeTest) { + const auto image = DecodeImageFromFile(GetFullPath("burger.jpg")); + ASSERT_TRUE(image.ok()); + + const std::string model_path = GetFullPath(kModelName); + ImageEmbedderOptions options = { + /* base_options= */ {/* model_asset_buffer= */ nullptr, + /* model_asset_path= */ model_path.c_str()}, + /* running_mode= */ RunningMode::IMAGE, + /* embedder_options= */ + {/* l2_normalize= */ true, + /* quantize= */ false}, + }; + + void* embedder = image_embedder_create(&options); + EXPECT_NE(embedder, nullptr); + + const MpImage mp_image = { + .type = MpImage::IMAGE_FRAME, + .image_frame = { + .format = static_cast( + image->GetImageFrameSharedPtr()->Format()), + .image_buffer = image->GetImageFrameSharedPtr()->PixelData(), + .width = image->GetImageFrameSharedPtr()->Width(), + .height = image->GetImageFrameSharedPtr()->Height()}}; + + ImageEmbedderResult result; + image_embedder_embed_image(embedder, &mp_image, &result); + EXPECT_EQ(result.embeddings_count, 1); + EXPECT_NEAR(result.embeddings[0].float_embedding[0], -0.0142344, kPrecision); + image_embedder_close_result(&result); + image_embedder_close(embedder); +} + +TEST(ImageEmbedderTest, InvalidArgumentHandling) { + // It is an error to set neither the asset buffer nor the path. + ImageEmbedderOptions options = { + /* base_options= */ {/* model_asset_buffer= */ nullptr, + /* model_asset_path= */ nullptr}, + /* embedder_options= */ {}, + }; + + char* error_msg; + void* embedder = image_embedder_create(&options, &error_msg); + EXPECT_EQ(embedder, nullptr); + + EXPECT_THAT(error_msg, HasSubstr("ExternalFile must specify")); + + free(error_msg); +} + +TEST(ImageEmbedderTest, FailedEmbeddingHandling) { + const std::string model_path = GetFullPath(kModelName); + ImageEmbedderOptions options = { + /* base_options= */ {/* model_asset_buffer= */ nullptr, + /* model_asset_path= */ model_path.c_str()}, + /* running_mode= */ RunningMode::IMAGE, + /* embedder_options= */ + {/* l2_normalize= */ false, + /* quantize= */ false}, + }; + + void* embedder = image_embedder_create(&options); + EXPECT_NE(embedder, nullptr); + + const MpImage mp_image = {.type = MpImage::GPU_BUFFER, .gpu_buffer = {}}; + ImageEmbedderResult result; + char* error_msg; + image_embedder_embed_image(embedder, &mp_image, &result, &error_msg); + EXPECT_THAT(error_msg, HasSubstr("gpu buffer not supported yet")); + free(error_msg); + image_embedder_close(embedder); +} + +} // namespace