diff --git a/mediapipe/tasks/c/components/containers/BUILD b/mediapipe/tasks/c/components/containers/BUILD index c4697a9dd..f3a7cc5e9 100644 --- a/mediapipe/tasks/c/components/containers/BUILD +++ b/mediapipe/tasks/c/components/containers/BUILD @@ -99,17 +99,24 @@ cc_test( ], ) -cc_library( - name = "language_detection_result", - hdrs = ["language_detection_result.h"], -) - cc_library( name = "language_detection_result_converter", srcs = ["language_detection_result_converter.cc"], hdrs = ["language_detection_result_converter.h"], deps = [ - ":language_detection_result", + "//mediapipe/tasks/c/text/language_detector:language_detector", "//mediapipe/tasks/cc/text/language_detector:language_detector", ], ) + +cc_test( + name = "language_detection_result_converter_test", + srcs = ["language_detection_result_converter_test.cc"], + deps = [ + ":language_detection_result_converter", + "//mediapipe/framework/port:gtest", + "//mediapipe/tasks/c/text/language_detector:language_detector", + "//mediapipe/tasks/cc/text/language_detector:language_detector", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/mediapipe/tasks/c/components/containers/language_detection_result.h b/mediapipe/tasks/c/components/containers/language_detection_result.h deleted file mode 100644 index a2b7ee9db..000000000 --- a/mediapipe/tasks/c/components/containers/language_detection_result.h +++ /dev/null @@ -1,46 +0,0 @@ -/* 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_COMPONENTS_CONTAINERS_LANGUAGE_DETECTION_RESULT_H_ -#define MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_LANGUAGE_DETECTION_RESULT_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -// A language code and its probability. -struct LanguageDetectorPrediction { - // An i18n language / locale code, e.g. "en" for English, "uz" for Uzbek, - // "ja"-Latn for Japanese (romaji). - char* language_code; - - float probability; -}; - -// Task output. -struct LanguageDetectorResult { - struct LanguageDetectorPrediction* predictions; - - // Keep the count of predictions. - uint32_t predictions_count; -}; - -#ifdef __cplusplus -} // extern C -#endif - -#endif // MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_LANGUAGE_DETECTION_RESULT_H_ diff --git a/mediapipe/tasks/c/components/containers/language_detection_result_converter.cc b/mediapipe/tasks/c/components/containers/language_detection_result_converter.cc index e9a7b8bab..fda33efe1 100644 --- a/mediapipe/tasks/c/components/containers/language_detection_result_converter.cc +++ b/mediapipe/tasks/c/components/containers/language_detection_result_converter.cc @@ -17,7 +17,7 @@ limitations under the License. #include -#include "mediapipe/tasks/c/components/containers/language_detection_result.h" +#include "mediapipe/tasks/c/text/language_detector/language_detector.h" #include "mediapipe/tasks/cc/text/language_detector/language_detector.h" namespace mediapipe::tasks::c::components::containers { diff --git a/mediapipe/tasks/c/components/containers/language_detection_result_converter.h b/mediapipe/tasks/c/components/containers/language_detection_result_converter.h index 74535de7f..c9cfd55bd 100644 --- a/mediapipe/tasks/c/components/containers/language_detection_result_converter.h +++ b/mediapipe/tasks/c/components/containers/language_detection_result_converter.h @@ -16,7 +16,7 @@ limitations under the License. #ifndef MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_LANGUAGE_DETECTION_RESULT_CONVERTER_H_ #define MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_LANGUAGE_DETECTION_RESULT_CONVERTER_H_ -#include "mediapipe/tasks/c/components/containers/language_detection_result.h" +#include "mediapipe/tasks/c/text/language_detector/language_detector.h" #include "mediapipe/tasks/cc/text/language_detector/language_detector.h" namespace mediapipe::tasks::c::components::containers { diff --git a/mediapipe/tasks/c/components/containers/language_detection_result_converter_test.cc b/mediapipe/tasks/c/components/containers/language_detection_result_converter_test.cc new file mode 100644 index 000000000..21eff679b --- /dev/null +++ b/mediapipe/tasks/c/components/containers/language_detection_result_converter_test.cc @@ -0,0 +1,57 @@ +/* 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/components/containers/language_detection_result_converter.h" + +#include +#include + +#include "mediapipe/framework/port/gtest.h" +#include "mediapipe/tasks/c/text/language_detector/language_detector.h" +#include "mediapipe/tasks/cc/text/language_detector/language_detector.h" + +namespace mediapipe::tasks::c::components::containers { + +TEST(LanguageDetectionResultConverterTest, + ConvertsLanguageDetectionResultCustomResult) { + mediapipe::tasks::text::language_detector::LanguageDetectorResult + cpp_detector_result = {{/* language_code= */ "fr", + /* probability= */ 0.5}, + {/* language_code= */ "en", + /* probability= */ 0.5}}; + + LanguageDetectorResult c_detector_result; + CppConvertToLanguageDetectionResult(cpp_detector_result, &c_detector_result); + EXPECT_NE(c_detector_result.predictions, nullptr); + EXPECT_EQ(c_detector_result.predictions_count, 2); + EXPECT_NE(c_detector_result.predictions[0].language_code, "fr"); + EXPECT_EQ(c_detector_result.predictions[0].probability, 0.5); + + CppCloseLanguageDetectionResult(&c_detector_result); +} + +TEST(LanguageDetectionResultConverterTest, FreesMemory) { + mediapipe::tasks::text::language_detector::LanguageDetectorResult + cpp_detector_result = {{"fr", 0.5}}; + + LanguageDetectorResult c_detector_result; + CppConvertToLanguageDetectionResult(cpp_detector_result, &c_detector_result); + EXPECT_NE(c_detector_result.predictions, nullptr); + + CppCloseLanguageDetectionResult(&c_detector_result); + EXPECT_EQ(c_detector_result.predictions, nullptr); +} + +} // namespace mediapipe::tasks::c::components::containers diff --git a/mediapipe/tasks/c/text/language_detector/BUILD b/mediapipe/tasks/c/text/language_detector/BUILD index cd711b696..9a3ce21e7 100644 --- a/mediapipe/tasks/c/text/language_detector/BUILD +++ b/mediapipe/tasks/c/text/language_detector/BUILD @@ -22,7 +22,6 @@ cc_library( hdrs = ["language_detector.h"], visibility = ["//visibility:public"], deps = [ - "//mediapipe/tasks/c/components/containers:language_detection_result", "//mediapipe/tasks/c/components/containers:language_detection_result_converter", "//mediapipe/tasks/c/components/processors:classifier_options", "//mediapipe/tasks/c/components/processors:classifier_options_converter", @@ -69,6 +68,15 @@ cc_binary( deps = [":language_detector_lib"], ) +cc_library( + name = "language_detector", + hdrs = ["language_detector.h"], + deps = [ + "//mediapipe/tasks/c/components/processors:classifier_options", + "//mediapipe/tasks/c/core:base_options", + ], +) + cc_test( name = "language_detector_test", srcs = ["language_detector_test.cc"], diff --git a/mediapipe/tasks/c/text/language_detector/language_detector.h b/mediapipe/tasks/c/text/language_detector/language_detector.h index d19fc8ca1..e6e17f996 100644 --- a/mediapipe/tasks/c/text/language_detector/language_detector.h +++ b/mediapipe/tasks/c/text/language_detector/language_detector.h @@ -16,7 +16,6 @@ limitations under the License. #ifndef MEDIAPIPE_TASKS_C_TEXT_LANGUAGE_DETECTOR_LANGUAGE_DETECTOR_H_ #define MEDIAPIPE_TASKS_C_TEXT_LANGUAGE_DETECTOR_LANGUAGE_DETECTOR_H_ -#include "mediapipe/tasks/c/components/containers/language_detection_result.h" #include "mediapipe/tasks/c/components/processors/classifier_options.h" #include "mediapipe/tasks/c/core/base_options.h" @@ -28,6 +27,23 @@ limitations under the License. extern "C" { #endif +// A language code and its probability. +struct LanguageDetectorPrediction { + // An i18n language / locale code, e.g. "en" for English, "uz" for Uzbek, + // "ja"-Latn for Japanese (romaji). + char* language_code; + + float probability; +}; + +// Task output. +struct LanguageDetectorResult { + struct LanguageDetectorPrediction* predictions; + + // The count of predictions. + uint32_t predictions_count; +}; + // The options for configuring a MediaPipe language detector task. struct LanguageDetectorOptions { // Base options for configuring MediaPipe Tasks, such as specifying the model