From 315982df0fa870e682016c172fd1e029c9c3e08d Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Mon, 11 Sep 2023 10:45:05 -0700 Subject: [PATCH] Ensure that C header don't import C++ types PiperOrigin-RevId: 564435119 --- mediapipe/tasks/c/components/containers/BUILD | 21 +++++++++++-- .../tasks/c/components/containers/category.h | 15 ++++------ .../{category.cc => category_converter.cc} | 2 +- .../containers/category_converter.h | 29 ++++++++++++++++++ .../containers/classification_result.h | 15 ++++------ ....cc => classification_result_converter.cc} | 3 +- .../classification_result_converter.h | 30 +++++++++++++++++++ mediapipe/tasks/c/components/processors/BUILD | 12 ++++++-- .../processors/classifier_options.h | 16 +++++----- ...ons.cc => classifier_options_converter.cc} | 3 +- .../processors/classifier_options_converter.h | 30 +++++++++++++++++++ mediapipe/tasks/c/core/BUILD | 12 ++++++-- mediapipe/tasks/c/core/base_options.h | 13 +++----- ...e_options.cc => base_options_converter.cc} | 2 +- .../tasks/c/core/base_options_converter.h | 29 ++++++++++++++++++ mediapipe/tasks/c/text/text_classifier/BUILD | 3 ++ .../c/text/text_classifier/text_classifier.cc | 3 ++ .../c/text/text_classifier/text_classifier.h | 5 ++++ 18 files changed, 194 insertions(+), 49 deletions(-) rename mediapipe/tasks/c/components/containers/{category.cc => category_converter.cc} (94%) create mode 100644 mediapipe/tasks/c/components/containers/category_converter.h rename mediapipe/tasks/c/components/containers/{classification_result.cc => classification_result_converter.cc} (95%) create mode 100644 mediapipe/tasks/c/components/containers/classification_result_converter.h rename mediapipe/tasks/c/components/processors/{classifier_options.cc => classifier_options_converter.cc} (99%) create mode 100644 mediapipe/tasks/c/components/processors/classifier_options_converter.h rename mediapipe/tasks/c/core/{base_options.cc => base_options_converter.cc} (94%) create mode 100644 mediapipe/tasks/c/core/base_options_converter.h diff --git a/mediapipe/tasks/c/components/containers/BUILD b/mediapipe/tasks/c/components/containers/BUILD index 0f55d18d7f..4b1841ef8f 100644 --- a/mediapipe/tasks/c/components/containers/BUILD +++ b/mediapipe/tasks/c/components/containers/BUILD @@ -18,17 +18,32 @@ licenses(["notice"]) cc_library( name = "category", - srcs = ["category.cc"], hdrs = ["category.h"], - deps = ["//mediapipe/tasks/cc/components/containers:category"], +) + +cc_library( + name = "category_converter", + srcs = ["category_converter.cc"], + hdrs = ["category_converter.h"], + deps = [ + ":category", + "//mediapipe/tasks/cc/components/containers:category", + ], ) cc_library( name = "classification_result", - srcs = ["classification_result.cc"], hdrs = ["classification_result.h"], +) + +cc_library( + name = "classification_result_converter", + srcs = ["classification_result_converter.cc"], + hdrs = ["classification_result_converter.h"], deps = [ ":category", + ":category_converter", + ":classification_result", "//mediapipe/tasks/cc/components/containers:classification_result", ], ) diff --git a/mediapipe/tasks/c/components/containers/category.h b/mediapipe/tasks/c/components/containers/category.h index c83140af61..b6eede40c5 100644 --- a/mediapipe/tasks/c/components/containers/category.h +++ b/mediapipe/tasks/c/components/containers/category.h @@ -16,9 +16,10 @@ limitations under the License. #ifndef MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_CATEGORY_H_ #define MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_CATEGORY_H_ -#include "mediapipe/tasks/cc/components/containers/category.h" - +#ifdef __cplusplus extern "C" { +#endif + // Defines a single classification result. // // The label maps packed into the TFLite Model Metadata [1] are used to populate @@ -41,13 +42,9 @@ struct Category { // packed in the TFLite Model Metadata if present. const char* display_name; }; -} - -namespace mediapie::tasks::c::components::containers { - -void CppConvertToCategory(mediapipe::tasks::components::containers::Category in, - Category* out); -} // namespace mediapie::tasks::c::components::containers +#ifdef __cplusplus +} // extern C +#endif #endif // MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_CATEGORY_H_ diff --git a/mediapipe/tasks/c/components/containers/category.cc b/mediapipe/tasks/c/components/containers/category_converter.cc similarity index 94% rename from mediapipe/tasks/c/components/containers/category.cc rename to mediapipe/tasks/c/components/containers/category_converter.cc index 2311f6372c..b819c83f90 100644 --- a/mediapipe/tasks/c/components/containers/category.cc +++ b/mediapipe/tasks/c/components/containers/category_converter.cc @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#include "mediapipe/tasks/c/components/containers/category.h" +#include "mediapipe/tasks/c/components/containers/category_converter.h" namespace mediapie::tasks::c::components::containers { diff --git a/mediapipe/tasks/c/components/containers/category_converter.h b/mediapipe/tasks/c/components/containers/category_converter.h new file mode 100644 index 0000000000..a8b2b6a0f2 --- /dev/null +++ b/mediapipe/tasks/c/components/containers/category_converter.h @@ -0,0 +1,29 @@ +/* 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_CATEGORY_CONVERTER_H_ +#define MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_CATEGORY_CONVERTER_H_ + +#include "mediapipe/tasks/c/components/containers/category.h" +#include "mediapipe/tasks/cc/components/containers/category.h" + +namespace mediapie::tasks::c::components::containers { + +void CppConvertToCategory(mediapipe::tasks::components::containers::Category in, + Category* out); + +} // namespace mediapie::tasks::c::components::containers + +#endif // MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_CATEGORY_CONVERTER_H_ diff --git a/mediapipe/tasks/c/components/containers/classification_result.h b/mediapipe/tasks/c/components/containers/classification_result.h index 77ec4ba807..ef2914e5db 100644 --- a/mediapipe/tasks/c/components/containers/classification_result.h +++ b/mediapipe/tasks/c/components/containers/classification_result.h @@ -19,9 +19,9 @@ limitations under the License. #include #include -#include "mediapipe/tasks/cc/components/containers/classification_result.h" - +#ifdef __cplusplus extern "C" { +#endif // Defines classification results for a given classifier head. struct Classifications { @@ -60,14 +60,9 @@ struct ClassificationResult { // Specifies whether the timestamp contains a valid value. bool has_timestamp_ms; }; -} - -namespace mediapipe::tasks::c::components::containers { - -void CppConvertToClassificationResult( - mediapipe::tasks::components::containers::ClassificationResult in, - ClassificationResult* out); -} // namespace mediapipe::tasks::c::components::containers +#ifdef __cplusplus +} // extern C +#endif #endif // MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_CLASSIFICATION_RESULT_H_ diff --git a/mediapipe/tasks/c/components/containers/classification_result.cc b/mediapipe/tasks/c/components/containers/classification_result_converter.cc similarity index 95% rename from mediapipe/tasks/c/components/containers/classification_result.cc rename to mediapipe/tasks/c/components/containers/classification_result_converter.cc index 4e6b1036e5..676955ab21 100644 --- a/mediapipe/tasks/c/components/containers/classification_result.cc +++ b/mediapipe/tasks/c/components/containers/classification_result_converter.cc @@ -13,9 +13,10 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#include "mediapipe/tasks/c/components/containers/classification_result.h" +#include "mediapipe/tasks/c/components/containers/classification_result_converter.h" #include "mediapipe/tasks/c/components/containers/category.h" +#include "mediapipe/tasks/c/components/containers/category_converter.h" namespace mediapipe::tasks::c::components::containers { diff --git a/mediapipe/tasks/c/components/containers/classification_result_converter.h b/mediapipe/tasks/c/components/containers/classification_result_converter.h new file mode 100644 index 0000000000..a81d76e82c --- /dev/null +++ b/mediapipe/tasks/c/components/containers/classification_result_converter.h @@ -0,0 +1,30 @@ +/* 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_CLASSIFICATION_RESULT_CONVERTER_H_ +#define MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_CLASSIFICATION_RESULT_CONVERTER_H_ + +#include "mediapipe/tasks/c/components/containers/classification_result.h" +#include "mediapipe/tasks/cc/components/containers/classification_result.h" + +namespace mediapipe::tasks::c::components::containers { + +void CppConvertToClassificationResult( + mediapipe::tasks::components::containers::ClassificationResult in, + ClassificationResult* out); + +} // namespace mediapipe::tasks::c::components::containers + +#endif // MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_CLASSIFICATION_RESULT_CONVERTER_H_ diff --git a/mediapipe/tasks/c/components/processors/BUILD b/mediapipe/tasks/c/components/processors/BUILD index 397e149de6..e90437d59a 100644 --- a/mediapipe/tasks/c/components/processors/BUILD +++ b/mediapipe/tasks/c/components/processors/BUILD @@ -18,7 +18,15 @@ licenses(["notice"]) cc_library( name = "classifier_options", - srcs = ["classifier_options.cc"], hdrs = ["classifier_options.h"], - deps = ["//mediapipe/tasks/cc/components/processors:classifier_options"], +) + +cc_library( + name = "classifier_options_converter", + srcs = ["classifier_options_converter.cc"], + hdrs = ["classifier_options_converter.h"], + deps = [ + ":classifier_options", + "//mediapipe/tasks/cc/components/processors:classifier_options", + ], ) diff --git a/mediapipe/tasks/c/components/processors/classifier_options.h b/mediapipe/tasks/c/components/processors/classifier_options.h index 7819743318..4658fb42b2 100644 --- a/mediapipe/tasks/c/components/processors/classifier_options.h +++ b/mediapipe/tasks/c/components/processors/classifier_options.h @@ -18,7 +18,9 @@ limitations under the License. #include -#include "mediapipe/tasks/cc/components/processors/classifier_options.h" +#ifdef __cplusplus +extern "C" { +#endif // Classifier options for MediaPipe C classification Tasks. struct ClassifierOptions { @@ -45,17 +47,13 @@ struct ClassifierOptions { // The denylist of category names. If non-empty, detection results whose // category name is in this set will be filtered out. Duplicate or unknown // category names are ignored. Mutually exclusive with category_allowlist. - char** category_denylist = {}; + char** category_denylist; // The number of elements in the category denylist. uint32_t category_denylist_count; }; -namespace mediapipe::tasks::c::components::processors { - -void CppConvertToClassifierOptions( - ClassifierOptions in, - mediapipe::tasks::components::processors::ClassifierOptions* out); - -} // namespace mediapipe::tasks::c::components::processors +#ifdef __cplusplus +} // extern C +#endif #endif // MEDIAPIPE_TASKS_C_COMPONENTS_PROCESSORS_CLASSIFIER_OPTIONS_H_ diff --git a/mediapipe/tasks/c/components/processors/classifier_options.cc b/mediapipe/tasks/c/components/processors/classifier_options_converter.cc similarity index 99% rename from mediapipe/tasks/c/components/processors/classifier_options.cc rename to mediapipe/tasks/c/components/processors/classifier_options_converter.cc index 7c84e7a033..e421a78322 100644 --- a/mediapipe/tasks/c/components/processors/classifier_options.cc +++ b/mediapipe/tasks/c/components/processors/classifier_options_converter.cc @@ -13,11 +13,10 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#include "mediapipe/tasks/c/components/processors/classifier_options.h" - #include #include +#include "mediapipe/tasks/c/components/processors/classifier_options.h" #include "mediapipe/tasks/cc/components/processors/classifier_options.h" namespace mediapie::c::components::processors { diff --git a/mediapipe/tasks/c/components/processors/classifier_options_converter.h b/mediapipe/tasks/c/components/processors/classifier_options_converter.h new file mode 100644 index 0000000000..7f8019f041 --- /dev/null +++ b/mediapipe/tasks/c/components/processors/classifier_options_converter.h @@ -0,0 +1,30 @@ +/* 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_PROCESSORS_CLASSIFIER_OPTIONS_CONVERTER_H_ +#define MEDIAPIPE_TASKS_C_COMPONENTS_PROCESSORS_CLASSIFIER_OPTIONS_CONVERTER_H_ + +#include "mediapipe/tasks/c/components/processors/classifier_options.h" +#include "mediapipe/tasks/cc/components/processors/classifier_options.h" + +namespace mediapipe::tasks::c::components::processors { + +void CppConvertToClassifierOptions( + ClassifierOptions in, + mediapipe::tasks::components::processors::ClassifierOptions* out); + +} // namespace mediapipe::tasks::c::components::processors + +#endif // MEDIAPIPE_TASKS_C_COMPONENTS_PROCESSORS_CLASSIFIER_OPTIONS_CONVERTER_H_ diff --git a/mediapipe/tasks/c/core/BUILD b/mediapipe/tasks/c/core/BUILD index adf6c81afd..9a360404eb 100644 --- a/mediapipe/tasks/c/core/BUILD +++ b/mediapipe/tasks/c/core/BUILD @@ -18,7 +18,15 @@ licenses(["notice"]) cc_library( name = "base_options", - srcs = ["base_options.cc"], hdrs = ["base_options.h"], - deps = ["//mediapipe/tasks/cc/core:base_options"], +) + +cc_library( + name = "base_options_converter", + srcs = ["base_options_converter.cc"], + hdrs = ["base_options_converter.h"], + deps = [ + ":base_options", + "//mediapipe/tasks/cc/core:base_options", + ], ) diff --git a/mediapipe/tasks/c/core/base_options.h b/mediapipe/tasks/c/core/base_options.h index 1707c9fad5..d23b6884c5 100644 --- a/mediapipe/tasks/c/core/base_options.h +++ b/mediapipe/tasks/c/core/base_options.h @@ -16,9 +16,9 @@ limitations under the License. #ifndef MEDIAPIPE_TASKS_C_CORE_BASE_OPTIONS_H_ #define MEDIAPIPE_TASKS_C_CORE_BASE_OPTIONS_H_ -#include "mediapipe/tasks/cc/core/base_options.h" - +#ifdef __cplusplus extern "C" { +#endif // Base options for MediaPipe C Tasks. struct BaseOptions { @@ -29,13 +29,8 @@ struct BaseOptions { char* model_asset_path; }; +#ifdef __cplusplus } // extern C - -namespace mediapipe::tasks::c::components::containers { - -void CppConvertToBaseOptions(BaseOptions in, - mediapipe::tasks::core::BaseOptions* out); - -} // namespace mediapipe::tasks::c::components::containers +#endif #endif // MEDIAPIPE_TASKS_C_CORE_BASE_OPTIONS_H_ diff --git a/mediapipe/tasks/c/core/base_options.cc b/mediapipe/tasks/c/core/base_options_converter.cc similarity index 94% rename from mediapipe/tasks/c/core/base_options.cc rename to mediapipe/tasks/c/core/base_options_converter.cc index d8fcfdb9e5..c0bdf15397 100644 --- a/mediapipe/tasks/c/core/base_options.cc +++ b/mediapipe/tasks/c/core/base_options_converter.cc @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#include "mediapipe/tasks/c/core/base_options.h" +#include "mediapipe/tasks/c/core/base_options_converter.h" #include "mediapipe/tasks/cc/core/base_options.h" diff --git a/mediapipe/tasks/c/core/base_options_converter.h b/mediapipe/tasks/c/core/base_options_converter.h new file mode 100644 index 0000000000..0890857fc2 --- /dev/null +++ b/mediapipe/tasks/c/core/base_options_converter.h @@ -0,0 +1,29 @@ +/* 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_CORE_BASE_OPTIONS_CONVERTER_H_ +#define MEDIAPIPE_TASKS_C_CORE_BASE_OPTIONS_CONVERTER_H_ + +#include "mediapipe/tasks/c/core/base_options.h" +#include "mediapipe/tasks/cc/core/base_options.h" + +namespace mediapipe::tasks::c::components::containers { + +void CppConvertToBaseOptions(BaseOptions in, + mediapipe::tasks::core::BaseOptions* out); + +} // namespace mediapipe::tasks::c::components::containers + +#endif // MEDIAPIPE_TASKS_C_CORE_BASE_OPTIONS_H_ diff --git a/mediapipe/tasks/c/text/text_classifier/BUILD b/mediapipe/tasks/c/text/text_classifier/BUILD index 3fdd06cc05..e095e2680f 100644 --- a/mediapipe/tasks/c/text/text_classifier/BUILD +++ b/mediapipe/tasks/c/text/text_classifier/BUILD @@ -23,8 +23,11 @@ cc_library( visibility = ["//visibility:public"], deps = [ "//mediapipe/tasks/c/components/containers:classification_result", + "//mediapipe/tasks/c/components/containers:classification_result_converter", "//mediapipe/tasks/c/components/processors:classifier_options", + "//mediapipe/tasks/c/components/processors:classifier_options_converter", "//mediapipe/tasks/c/core:base_options", + "//mediapipe/tasks/c/core:base_options_converter", "//mediapipe/tasks/cc/text/text_classifier", "@com_google_absl//absl/log:absl_log", ], diff --git a/mediapipe/tasks/c/text/text_classifier/text_classifier.cc b/mediapipe/tasks/c/text/text_classifier/text_classifier.cc index 9faca2a1b1..388d03b944 100644 --- a/mediapipe/tasks/c/text/text_classifier/text_classifier.cc +++ b/mediapipe/tasks/c/text/text_classifier/text_classifier.cc @@ -19,8 +19,11 @@ limitations under the License. #include "absl/log/absl_log.h" #include "mediapipe/tasks/c/components/containers/classification_result.h" +#include "mediapipe/tasks/c/components/containers/classification_result_converter.h" #include "mediapipe/tasks/c/components/processors/classifier_options.h" +#include "mediapipe/tasks/c/components/processors/classifier_options_converter.h" #include "mediapipe/tasks/c/core/base_options.h" +#include "mediapipe/tasks/c/core/base_options_converter.h" #include "mediapipe/tasks/cc/text/text_classifier/text_classifier.h" namespace mediapipe::tasks::c::text::text_classifier { diff --git a/mediapipe/tasks/c/text/text_classifier/text_classifier.h b/mediapipe/tasks/c/text/text_classifier/text_classifier.h index 9ec9682dc8..2c084bbed6 100644 --- a/mediapipe/tasks/c/text/text_classifier/text_classifier.h +++ b/mediapipe/tasks/c/text/text_classifier/text_classifier.h @@ -20,7 +20,10 @@ limitations under the License. #include "mediapipe/tasks/c/components/processors/classifier_options.h" #include "mediapipe/tasks/c/core/base_options.h" +#ifdef __cplusplus extern "C" { +#endif + typedef ClassificationResult TextClassifierResult; // The options for configuring a MediaPipe text classifier task. @@ -44,6 +47,8 @@ bool text_classifier_classify(void* classifier, char* utf8_str, // Shuts down the TextClassifier when all the work is done. Frees all memory. void text_classifier_close(void* classifier); +#ifdef __cplusplus } // extern C +#endif #endif // MEDIAPIPE_TASKS_C_TEXT_TEXT_CLASSIFIER_TEXT_CLASSIFIER_H_