From 8af4cca4138e5637f1dc7f963c1202ac23a115c0 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Fri, 30 Sep 2022 00:33:19 +0000 Subject: [PATCH] Internal change PiperOrigin-RevId: 477863040 --- .../util/labels_to_render_data_calculator.cc | 11 +++++++++++ .../util/labels_to_render_data_calculator.proto | 7 +++++++ mediapipe/util/annotation_renderer.cc | 10 ++++++++++ mediapipe/util/render_data.proto | 6 ++++++ 4 files changed, 34 insertions(+) diff --git a/mediapipe/calculators/util/labels_to_render_data_calculator.cc b/mediapipe/calculators/util/labels_to_render_data_calculator.cc index 4aab3b676..dcd76d47b 100644 --- a/mediapipe/calculators/util/labels_to_render_data_calculator.cc +++ b/mediapipe/calculators/util/labels_to_render_data_calculator.cc @@ -184,6 +184,17 @@ absl::Status LabelsToRenderDataCalculator::Process(CalculatorContext* cc) { text->set_left(label_left_px_); text->set_baseline(label_baseline_px + i * label_height_px_); text->set_font_face(options_.font_face()); + if (options_.outline_thickness() > 0) { + text->set_outline_thickness(options_.outline_thickness()); + if (options_.outline_color_size() > 0) { + *(text->mutable_outline_color()) = + options_.outline_color(i % options_.outline_color_size()); + } else { + text->mutable_outline_color()->set_r(0); + text->mutable_outline_color()->set_g(0); + text->mutable_outline_color()->set_b(0); + } + } } cc->Outputs() .Tag(kRenderDataTag) diff --git a/mediapipe/calculators/util/labels_to_render_data_calculator.proto b/mediapipe/calculators/util/labels_to_render_data_calculator.proto index cf0ada9c2..7946ff683 100644 --- a/mediapipe/calculators/util/labels_to_render_data_calculator.proto +++ b/mediapipe/calculators/util/labels_to_render_data_calculator.proto @@ -30,6 +30,13 @@ message LabelsToRenderDataCalculatorOptions { // Thickness for drawing the label(s). optional double thickness = 2 [default = 2]; + // Color of outline around each character, if any. One per label, as with + // color attribute. + repeated Color outline_color = 12; + + // Thickness of outline around each character. + optional double outline_thickness = 11; + // The font height in absolute pixels. optional int32 font_height_px = 3 [default = 50]; diff --git a/mediapipe/util/annotation_renderer.cc b/mediapipe/util/annotation_renderer.cc index 19fbbc14d..671f47505 100644 --- a/mediapipe/util/annotation_renderer.cc +++ b/mediapipe/util/annotation_renderer.cc @@ -552,6 +552,16 @@ void AnnotationRenderer::DrawText(const RenderAnnotation& annotation) { origin.y += text_size.height / 2; } + if (text.outline_thickness() > 0.0) { + const int background_thickness = ClampThickness( + round((annotation.thickness() + 2.0 * text.outline_thickness()) * + scale_factor_)); + const cv::Scalar outline_color = + MediapipeColorToOpenCVColor(text.outline_color()); + cv::putText(mat_image_, text.display_text(), origin, font_face, font_scale, + outline_color, background_thickness, /*lineType=*/8, + /*bottomLeftOrigin=*/flip_text_vertically_); + } cv::putText(mat_image_, text.display_text(), origin, font_face, font_scale, color, thickness, /*lineType=*/8, /*bottomLeftOrigin=*/flip_text_vertically_); diff --git a/mediapipe/util/render_data.proto b/mediapipe/util/render_data.proto index 0ff6b3409..62cb750b0 100644 --- a/mediapipe/util/render_data.proto +++ b/mediapipe/util/render_data.proto @@ -168,6 +168,12 @@ message RenderAnnotation { // [left, baseline] represent [center_x, center_y]. optional bool center_horizontally = 7 [default = false]; optional bool center_vertically = 8 [default = false]; + + // Thickness of the text outline. + optional double outline_thickness = 11 [default = 0.0]; + + // Color of the text outline. + optional Color outline_color = 12; } // The RenderAnnotation can be one of the below formats.