-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5072 from kinaryml:c-face-detector-api
PiperOrigin-RevId: 598995218
- Loading branch information
Showing
5 changed files
with
791 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Copyright 2024 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 = "face_detector_lib", | ||
srcs = ["face_detector.cc"], | ||
hdrs = ["face_detector.h"], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//mediapipe/framework/formats:image", | ||
"//mediapipe/framework/formats:image_frame", | ||
"//mediapipe/tasks/c/components/containers:detection_result", | ||
"//mediapipe/tasks/c/components/containers:detection_result_converter", | ||
"//mediapipe/tasks/c/core:base_options", | ||
"//mediapipe/tasks/c/core:base_options_converter", | ||
"//mediapipe/tasks/c/vision/core:common", | ||
"//mediapipe/tasks/cc/vision/core:running_mode", | ||
"//mediapipe/tasks/cc/vision/face_detector", | ||
"//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", | ||
], | ||
alwayslink = 1, | ||
) | ||
|
||
cc_test( | ||
name = "face_detector_test", | ||
srcs = ["face_detector_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 = [ | ||
":face_detector_lib", | ||
"//mediapipe/framework/deps:file_path", | ||
"//mediapipe/framework/formats:image", | ||
"//mediapipe/framework/port:gtest", | ||
"//mediapipe/tasks/c/components/containers:detection_result", | ||
"//mediapipe/tasks/c/components/containers:keypoint", | ||
"//mediapipe/tasks/c/components/containers:rect", | ||
"//mediapipe/tasks/c/vision/core:common", | ||
"//mediapipe/tasks/cc/vision/utils:image_utils", | ||
"@com_google_absl//absl/flags:flag", | ||
"@com_google_absl//absl/strings", | ||
"@com_google_googletest//:gtest_main", | ||
], | ||
) | ||
|
||
# bazel build -c opt --linkopt -s --strip always --define MEDIAPIPE_DISABLE_GPU=1 \ | ||
# //mediapipe/tasks/c/vision/face_detector:libface_detector.so | ||
cc_binary( | ||
name = "libface_detector.so", | ||
linkopts = [ | ||
"-Wl,-soname=libface_detector.so", | ||
"-fvisibility=hidden", | ||
], | ||
linkshared = True, | ||
tags = [ | ||
"manual", | ||
"nobuilder", | ||
"notap", | ||
], | ||
deps = [":face_detector_lib"], | ||
) | ||
|
||
# bazel build --config darwin_arm64 -c opt --strip always --define MEDIAPIPE_DISABLE_GPU=1 \ | ||
# //mediapipe/tasks/c/vision/face_detector:libface_detector.dylib | ||
cc_binary( | ||
name = "libface_detector.dylib", | ||
linkopts = [ | ||
"-Wl,-install_name,libface_detector.dylib", | ||
"-fvisibility=hidden", | ||
], | ||
linkshared = True, | ||
tags = [ | ||
"manual", | ||
"nobuilder", | ||
"notap", | ||
], | ||
deps = [":face_detector_lib"], | ||
) |
277 changes: 277 additions & 0 deletions
277
mediapipe/tasks/c/vision/face_detector/face_detector.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
/* Copyright 2024 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/face_detector/face_detector.h" | ||
|
||
#include <cstdint> | ||
#include <cstdlib> | ||
#include <memory> | ||
#include <utility> | ||
|
||
#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/detection_result_converter.h" | ||
#include "mediapipe/tasks/c/core/base_options_converter.h" | ||
#include "mediapipe/tasks/c/vision/core/common.h" | ||
#include "mediapipe/tasks/cc/vision/core/running_mode.h" | ||
#include "mediapipe/tasks/cc/vision/face_detector/face_detector.h" | ||
#include "mediapipe/tasks/cc/vision/utils/image_utils.h" | ||
|
||
namespace mediapipe::tasks::c::vision::face_detector { | ||
|
||
namespace { | ||
|
||
using ::mediapipe::tasks::c::components::containers::CppCloseDetectionResult; | ||
using ::mediapipe::tasks::c::components::containers:: | ||
CppConvertToDetectionResult; | ||
using ::mediapipe::tasks::c::core::CppConvertToBaseOptions; | ||
using ::mediapipe::tasks::vision::CreateImageFromBuffer; | ||
using ::mediapipe::tasks::vision::core::RunningMode; | ||
using ::mediapipe::tasks::vision::face_detector::FaceDetector; | ||
typedef ::mediapipe::tasks::vision::face_detector::FaceDetectorResult | ||
CppFaceDetectorResult; | ||
|
||
int CppProcessError(absl::Status status, char** error_msg) { | ||
if (error_msg) { | ||
*error_msg = strdup(status.ToString().c_str()); | ||
} | ||
return status.raw_code(); | ||
} | ||
|
||
} // namespace | ||
|
||
void CppConvertToFaceDetectorOptions( | ||
const FaceDetectorOptions& in, | ||
mediapipe::tasks::vision::face_detector::FaceDetectorOptions* out) { | ||
out->min_detection_confidence = in.min_detection_confidence; | ||
out->min_suppression_threshold = in.min_suppression_threshold; | ||
} | ||
|
||
FaceDetector* CppFaceDetectorCreate(const FaceDetectorOptions& options, | ||
char** error_msg) { | ||
auto cpp_options = std::make_unique< | ||
::mediapipe::tasks::vision::face_detector::FaceDetectorOptions>(); | ||
|
||
CppConvertToBaseOptions(options.base_options, &cpp_options->base_options); | ||
CppConvertToFaceDetectorOptions(options, cpp_options.get()); | ||
cpp_options->running_mode = static_cast<RunningMode>(options.running_mode); | ||
|
||
// Enable callback for processing live stream data when the running mode is | ||
// set to RunningMode::LIVE_STREAM. | ||
if (cpp_options->running_mode == RunningMode::LIVE_STREAM) { | ||
if (options.result_callback == nullptr) { | ||
const absl::Status status = absl::InvalidArgumentError( | ||
"Provided null pointer to callback function."); | ||
ABSL_LOG(ERROR) << "Failed to create FaceDetector: " << status; | ||
CppProcessError(status, error_msg); | ||
return nullptr; | ||
} | ||
|
||
FaceDetectorOptions::result_callback_fn result_callback = | ||
options.result_callback; | ||
cpp_options->result_callback = | ||
[result_callback](absl::StatusOr<CppFaceDetectorResult> cpp_result, | ||
const Image& image, int64_t timestamp) { | ||
char* error_msg = nullptr; | ||
|
||
if (!cpp_result.ok()) { | ||
ABSL_LOG(ERROR) << "Detection failed: " << cpp_result.status(); | ||
CppProcessError(cpp_result.status(), &error_msg); | ||
result_callback({}, MpImage(), timestamp, error_msg); | ||
free(error_msg); | ||
return; | ||
} | ||
|
||
// Result is valid for the lifetime of the callback function. | ||
FaceDetectorResult result; | ||
CppConvertToDetectionResult(*cpp_result, &result); | ||
|
||
const auto& image_frame = image.GetImageFrameSharedPtr(); | ||
const MpImage mp_image = { | ||
.type = MpImage::IMAGE_FRAME, | ||
.image_frame = { | ||
.format = static_cast<::ImageFormat>(image_frame->Format()), | ||
.image_buffer = image_frame->PixelData(), | ||
.width = image_frame->Width(), | ||
.height = image_frame->Height()}}; | ||
|
||
result_callback(&result, mp_image, timestamp, | ||
/* error_msg= */ nullptr); | ||
|
||
CppCloseDetectionResult(&result); | ||
}; | ||
} | ||
|
||
auto detector = FaceDetector::Create(std::move(cpp_options)); | ||
if (!detector.ok()) { | ||
ABSL_LOG(ERROR) << "Failed to create FaceDetector: " << detector.status(); | ||
CppProcessError(detector.status(), error_msg); | ||
return nullptr; | ||
} | ||
return detector->release(); | ||
} | ||
|
||
int CppFaceDetectorDetect(void* detector, const MpImage& image, | ||
FaceDetectorResult* result, char** error_msg) { | ||
if (image.type == MpImage::GPU_BUFFER) { | ||
const absl::Status status = | ||
absl::InvalidArgumentError("GPU Buffer not supported yet."); | ||
|
||
ABSL_LOG(ERROR) << "Detection failed: " << status.message(); | ||
return CppProcessError(status, error_msg); | ||
} | ||
|
||
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); | ||
|
||
if (!img.ok()) { | ||
ABSL_LOG(ERROR) << "Failed to create Image: " << img.status(); | ||
return CppProcessError(img.status(), error_msg); | ||
} | ||
|
||
auto cpp_detector = static_cast<FaceDetector*>(detector); | ||
auto cpp_result = cpp_detector->Detect(*img); | ||
if (!cpp_result.ok()) { | ||
ABSL_LOG(ERROR) << "Detection failed: " << cpp_result.status(); | ||
return CppProcessError(cpp_result.status(), error_msg); | ||
} | ||
CppConvertToDetectionResult(*cpp_result, result); | ||
return 0; | ||
} | ||
|
||
int CppFaceDetectorDetectForVideo(void* detector, const MpImage& image, | ||
int64_t timestamp_ms, | ||
FaceDetectorResult* result, | ||
char** error_msg) { | ||
if (image.type == MpImage::GPU_BUFFER) { | ||
absl::Status status = | ||
absl::InvalidArgumentError("GPU Buffer not supported yet"); | ||
|
||
ABSL_LOG(ERROR) << "Detection failed: " << status.message(); | ||
return CppProcessError(status, error_msg); | ||
} | ||
|
||
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); | ||
|
||
if (!img.ok()) { | ||
ABSL_LOG(ERROR) << "Failed to create Image: " << img.status(); | ||
return CppProcessError(img.status(), error_msg); | ||
} | ||
|
||
auto cpp_detector = static_cast<FaceDetector*>(detector); | ||
auto cpp_result = cpp_detector->DetectForVideo(*img, timestamp_ms); | ||
if (!cpp_result.ok()) { | ||
ABSL_LOG(ERROR) << "Detection failed: " << cpp_result.status(); | ||
return CppProcessError(cpp_result.status(), error_msg); | ||
} | ||
CppConvertToDetectionResult(*cpp_result, result); | ||
return 0; | ||
} | ||
|
||
int CppFaceDetectorDetectAsync(void* detector, const MpImage& image, | ||
int64_t timestamp_ms, char** error_msg) { | ||
if (image.type == MpImage::GPU_BUFFER) { | ||
absl::Status status = | ||
absl::InvalidArgumentError("GPU Buffer not supported yet"); | ||
|
||
ABSL_LOG(ERROR) << "Detection failed: " << status.message(); | ||
return CppProcessError(status, error_msg); | ||
} | ||
|
||
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); | ||
|
||
if (!img.ok()) { | ||
ABSL_LOG(ERROR) << "Failed to create Image: " << img.status(); | ||
return CppProcessError(img.status(), error_msg); | ||
} | ||
|
||
auto cpp_detector = static_cast<FaceDetector*>(detector); | ||
auto cpp_result = cpp_detector->DetectAsync(*img, timestamp_ms); | ||
if (!cpp_result.ok()) { | ||
ABSL_LOG(ERROR) << "Data preparation for the landmark detection failed: " | ||
<< cpp_result; | ||
return CppProcessError(cpp_result, error_msg); | ||
} | ||
return 0; | ||
} | ||
|
||
void CppFaceDetectorCloseResult(FaceDetectorResult* result) { | ||
CppCloseDetectionResult(result); | ||
} | ||
|
||
int CppFaceDetectorClose(void* detector, char** error_msg) { | ||
auto cpp_detector = static_cast<FaceDetector*>(detector); | ||
auto result = cpp_detector->Close(); | ||
if (!result.ok()) { | ||
ABSL_LOG(ERROR) << "Failed to close FaceDetector: " << result; | ||
return CppProcessError(result, error_msg); | ||
} | ||
delete cpp_detector; | ||
return 0; | ||
} | ||
|
||
} // namespace mediapipe::tasks::c::vision::face_detector | ||
|
||
extern "C" { | ||
|
||
void* face_detector_create(struct FaceDetectorOptions* options, | ||
char** error_msg) { | ||
return mediapipe::tasks::c::vision::face_detector::CppFaceDetectorCreate( | ||
*options, error_msg); | ||
} | ||
|
||
int face_detector_detect_image(void* detector, const MpImage& image, | ||
FaceDetectorResult* result, char** error_msg) { | ||
return mediapipe::tasks::c::vision::face_detector::CppFaceDetectorDetect( | ||
detector, image, result, error_msg); | ||
} | ||
|
||
int face_detector_detect_for_video(void* detector, const MpImage& image, | ||
int64_t timestamp_ms, | ||
FaceDetectorResult* result, | ||
char** error_msg) { | ||
return mediapipe::tasks::c::vision::face_detector:: | ||
CppFaceDetectorDetectForVideo(detector, image, timestamp_ms, result, | ||
error_msg); | ||
} | ||
|
||
int face_detector_detect_async(void* detector, const MpImage& image, | ||
int64_t timestamp_ms, char** error_msg) { | ||
return mediapipe::tasks::c::vision::face_detector::CppFaceDetectorDetectAsync( | ||
detector, image, timestamp_ms, error_msg); | ||
} | ||
|
||
void face_detector_close_result(FaceDetectorResult* result) { | ||
mediapipe::tasks::c::vision::face_detector::CppFaceDetectorCloseResult( | ||
result); | ||
} | ||
|
||
int face_detector_close(void* detector, char** error_ms) { | ||
return mediapipe::tasks::c::vision::face_detector::CppFaceDetectorClose( | ||
detector, error_ms); | ||
} | ||
|
||
} // extern "C" |
Oops, something went wrong.