From 7facc925ba57ead64b2ad9ceee920868d3bea97f Mon Sep 17 00:00:00 2001 From: Jiuqiang Tang Date: Wed, 24 May 2023 11:12:20 -0700 Subject: [PATCH] Allow FaceStylizerGraph to miss base options. Fix the color issue when the graph is running on gpu and "face alignment only" mode. PiperOrigin-RevId: 534912498 --- .../face_stylizer/face_stylizer_graph.cc | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/mediapipe/tasks/cc/vision/face_stylizer/face_stylizer_graph.cc b/mediapipe/tasks/cc/vision/face_stylizer/face_stylizer_graph.cc index d7265a146..d4057467b 100644 --- a/mediapipe/tasks/cc/vision/face_stylizer/face_stylizer_graph.cc +++ b/mediapipe/tasks/cc/vision/face_stylizer/face_stylizer_graph.cc @@ -231,18 +231,26 @@ class FaceStylizerGraph : public core::ModelTaskGraph { SubgraphContext* sc) override { bool output_stylized = HasOutput(sc->OriginalNode(), kStylizedImageTag); bool output_alignment = HasOutput(sc->OriginalNode(), kFaceAlignmentTag); - ASSIGN_OR_RETURN( - const auto* model_asset_bundle_resources, - CreateModelAssetBundleResources(sc)); - // Copies the file content instead of passing the pointer of file in - // memory if the subgraph model resource service is not available. auto face_stylizer_external_file = absl::make_unique(); - MP_RETURN_IF_ERROR(SetSubTaskBaseOptions( - *model_asset_bundle_resources, - sc->MutableOptions(), - output_stylized ? face_stylizer_external_file.get() : nullptr, - !sc->Service(::mediapipe::tasks::core::kModelResourcesCacheService) - .IsAvailable())); + if (sc->Options().has_base_options()) { + ASSIGN_OR_RETURN( + const auto* model_asset_bundle_resources, + CreateModelAssetBundleResources(sc)); + // Copies the file content instead of passing the pointer of file in + // memory if the subgraph model resource service is not available. + MP_RETURN_IF_ERROR(SetSubTaskBaseOptions( + *model_asset_bundle_resources, + sc->MutableOptions(), + output_stylized ? face_stylizer_external_file.get() : nullptr, + !sc->Service(::mediapipe::tasks::core::kModelResourcesCacheService) + .IsAvailable())); + } else if (output_stylized) { + return CreateStatusWithPayload( + absl::StatusCode::kInvalidArgument, + "Face stylizer must specify its base options when the " + "\"STYLIZED_IMAGE\" output stream is connected.", + MediaPipeTasksStatus::kInvalidArgumentError); + } Graph graph; ASSIGN_OR_RETURN( auto face_landmark_lists, @@ -347,7 +355,7 @@ class FaceStylizerGraph : public core::ModelTaskGraph { auto& image_to_tensor = graph.AddNode("ImageToTensorCalculator"); auto& image_to_tensor_options = image_to_tensor.GetOptions(); - image_to_tensor_options.mutable_output_tensor_float_range()->set_min(-1); + image_to_tensor_options.mutable_output_tensor_float_range()->set_min(0); image_to_tensor_options.mutable_output_tensor_float_range()->set_max(1); image_to_tensor_options.set_output_tensor_width(kFaceAlignmentOutputSize); image_to_tensor_options.set_output_tensor_height( @@ -363,7 +371,7 @@ class FaceStylizerGraph : public core::ModelTaskGraph { graph.AddNode("mediapipe.tasks.TensorsToImageCalculator"); auto& tensors_to_image_options = tensors_to_image.GetOptions(); - tensors_to_image_options.mutable_input_tensor_float_range()->set_min(-1); + tensors_to_image_options.mutable_input_tensor_float_range()->set_min(0); tensors_to_image_options.mutable_input_tensor_float_range()->set_max(1); face_alignment_image >> tensors_to_image.In(kTensorsTag); face_alignment = tensors_to_image.Out(kImageTag).Cast();