Skip to content

Commit

Permalink
Internal update
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 696527818
  • Loading branch information
MediaPipe Team authored and copybara-github committed Nov 14, 2024
1 parent 87c1af4 commit f53af03
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ class SingleFaceLandmarksDetectorGraph : public core::ModelTaskGraph {
auto& landmark_projection = graph.AddNode("LandmarkProjectionCalculator");
landmarks_letterbox_removed >> landmark_projection.In(kNormLandmarksTag);
face_rect >> landmark_projection.In(kNormRectTag);
image_size >> landmark_projection.In("IMAGE_DIMENSIONS");
Stream<NormalizedLandmarkList> projected_landmarks = AllowIf(
landmark_projection[Output<NormalizedLandmarkList>(kNormLandmarksTag)],
presence, graph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ constexpr char kImageInStream[] = "image_in";
constexpr char kPoseLandmarksInStream[] = "pose_landmarks_in";
constexpr char kFaceLandmarksOutStream[] = "face_landmarks_out";
constexpr char kRenderedImageOutStream[] = "rendered_image_out";
constexpr char kFaceDetectorTFLiteName[] = "face_detector.tflite";
constexpr char kFaceLandmarksDetectorTFLiteName[] =
"face_landmarks_detector.tflite";
constexpr char kFaceDetectionModelFile[] = "face_detection_short_range.tflite";
constexpr char kFaceLandmarksModelFile[] =
"facemesh2_lite_iris_faceflag_2023_02_14.tflite";

std::string GetFilePath(absl::string_view filename) {
return file::JoinPath("./", kTestDataDirectory, filename);
Expand All @@ -108,6 +108,16 @@ mediapipe::LandmarksToRenderDataCalculatorOptions GetFaceRendererOptions() {
return render_options;
}

mediapipe::RectToRenderDataCalculatorOptions GetRectRendererOptions() {
mediapipe::RectToRenderDataCalculatorOptions render_options;
render_options.set_filled(false);
render_options.mutable_color()->set_r(255);
render_options.mutable_color()->set_g(0);
render_options.mutable_color()->set_b(0);
render_options.set_thickness(2);
return render_options;
}

absl::StatusOr<std::unique_ptr<ModelAssetBundleResources>>
CreateModelAssetBundleResources(const std::string& model_asset_filename) {
auto external_model_bundle = std::make_unique<ExternalFile>();
Expand All @@ -127,38 +137,19 @@ absl::StatusOr<std::unique_ptr<tasks::core::TaskRunner>> CreateTaskRunner() {
Stream<NormalizedLandmarkList> face_landmarks_from_pose =
SplitToRanges(pose_landmarks, {{0, 11}}, graph)[0];
// Create face landmarker model bundle.
MP_ASSIGN_OR_RETURN(
auto model_bundle,
CreateModelAssetBundleResources(GetFilePath("face_landmarker_v2.task")));
face_detector::proto::FaceDetectorGraphOptions detector_options;
face_landmarker::proto::FaceLandmarksDetectorGraphOptions
landmarks_detector_options;

// Set face detection model.
MP_ASSIGN_OR_RETURN(auto face_detector_model_file,
model_bundle->GetFile(kFaceDetectorTFLiteName));
core::proto::FilePointerMeta face_detection_file_pointer;
face_detection_file_pointer.set_pointer(
reinterpret_cast<uint64_t>(face_detector_model_file.data()));
face_detection_file_pointer.set_length(face_detector_model_file.size());
detector_options.mutable_base_options()
->mutable_model_asset()
->mutable_file_pointer_meta()
->Swap(&face_detection_file_pointer);
detector_options.set_num_faces(1);
detector_options.mutable_base_options()->mutable_model_asset()->set_file_name(
GetFilePath(kFaceDetectionModelFile));

// Set face landmarks model.
MP_ASSIGN_OR_RETURN(auto face_landmarks_model_file,
model_bundle->GetFile(kFaceLandmarksDetectorTFLiteName));
core::proto::FilePointerMeta face_landmarks_detector_file_pointer;
face_landmarks_detector_file_pointer.set_pointer(
reinterpret_cast<uint64_t>(face_landmarks_model_file.data()));
face_landmarks_detector_file_pointer.set_length(
face_landmarks_model_file.size());
landmarks_detector_options.mutable_base_options()
->mutable_model_asset()
->mutable_file_pointer_meta()
->Swap(&face_landmarks_detector_file_pointer);
->set_file_name(GetFilePath(kFaceLandmarksModelFile));

// Track holistic face.
HolisticFaceTrackingRequest request;
Expand All @@ -173,10 +164,11 @@ absl::StatusOr<std::unique_ptr<tasks::core::TaskRunner>> CreateTaskRunner() {
auto render_scale = utils::GetRenderScale(
image_size, result.debug_output.roi_from_pose, 0.0001, graph);

auto face_landmarks_render_data = utils::RenderLandmarks(
face_landmarks, render_scale, GetFaceRendererOptions(), graph);
std::vector<Stream<mediapipe::RenderData>> render_list = {
face_landmarks_render_data};
utils::RenderLandmarks(face_landmarks, render_scale,
GetFaceRendererOptions(), graph),
utils::RenderRect(result.debug_output.roi_from_pose,
GetRectRendererOptions(), graph)};

auto rendered_image =
utils::Render(
Expand Down Expand Up @@ -209,6 +201,7 @@ TEST_F(HolisticFaceTrackingTest, SmokeTest) {
holistic_result.pose_landmarks())}}));
ASSERT_TRUE(output_packets.find(kFaceLandmarksOutStream) !=
output_packets.end());
ASSERT_FALSE(output_packets.find(kFaceLandmarksOutStream)->second.IsEmpty());
auto face_landmarks = output_packets.find(kFaceLandmarksOutStream)
->second.Get<NormalizedLandmarkList>();
EXPECT_THAT(
Expand Down

0 comments on commit f53af03

Please sign in to comment.