Project import generated by Copybara.
GitOrigin-RevId: 7e1d382a1788ebd8412c5626581b4c4cf2fe75ea
This commit is contained in:
@@ -321,3 +321,27 @@ cc_test(
|
||||
"//mediapipe/framework/port:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "rectangle_util",
|
||||
srcs = ["rectangle_util.cc"],
|
||||
hdrs = ["rectangle_util.h"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//mediapipe/framework/formats:rect_cc_proto",
|
||||
"//mediapipe/framework/port:rectangle",
|
||||
"//mediapipe/framework/port:ret_check",
|
||||
"//mediapipe/framework/port:statusor",
|
||||
"@com_google_absl//absl/status:statusor",
|
||||
"@com_google_absl//absl/types:span",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "rectangle_util_test",
|
||||
srcs = ["rectangle_util_test.cc"],
|
||||
deps = [
|
||||
":rectangle_util",
|
||||
"//mediapipe/framework/port:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
// Copyright 2019 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/util/rectangle_util.h"
|
||||
|
||||
#include "mediapipe/framework/formats/rect.pb.h"
|
||||
#include "mediapipe/framework/port/rectangle.h"
|
||||
#include "mediapipe/framework/port/ret_check.h"
|
||||
#include "mediapipe/framework/port/statusor.h"
|
||||
|
||||
namespace mediapipe {
|
||||
|
||||
// Converts a NormalizedRect into a Rectangle_f.
|
||||
absl::StatusOr<Rectangle_f> ToRectangle(
|
||||
const mediapipe::NormalizedRect& input) {
|
||||
if (!input.has_x_center() || !input.has_y_center() || !input.has_width() ||
|
||||
!input.has_height()) {
|
||||
return absl::InvalidArgumentError("Missing dimensions in NormalizedRect.");
|
||||
}
|
||||
if (input.width() < 0.0f || input.height() < 0.0f) {
|
||||
return absl::InvalidArgumentError("Negative rectangle width or height.");
|
||||
}
|
||||
|
||||
const float xmin = input.x_center() - input.width() / 2.0;
|
||||
const float ymin = input.y_center() - input.height() / 2.0;
|
||||
|
||||
// TODO: Support rotation for rectangle.
|
||||
return Rectangle_f(xmin, ymin, input.width(), input.height());
|
||||
}
|
||||
|
||||
// If the new_rect overlaps with any of the rectangles in
|
||||
// existing_rects, then return true. Otherwise, return false.
|
||||
absl::StatusOr<bool> DoesRectOverlap(
|
||||
const mediapipe::NormalizedRect& new_rect,
|
||||
absl::Span<const mediapipe::NormalizedRect> existing_rects,
|
||||
float min_similarity_threshold) {
|
||||
ASSIGN_OR_RETURN(Rectangle_f new_rectangle, ToRectangle(new_rect));
|
||||
|
||||
for (const mediapipe::NormalizedRect& existing_rect : existing_rects) {
|
||||
ASSIGN_OR_RETURN(Rectangle_f existing_rectangle,
|
||||
ToRectangle(existing_rect));
|
||||
if (CalculateIou(existing_rectangle, new_rectangle) >
|
||||
min_similarity_threshold) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Computes the overlap similarity based on Intersection over Union (IoU) of
|
||||
// two rectangles. Result is bounded between [0.0, 1.0], where 0.0 means no
|
||||
// intersection at all, and 1.0 means the two rectangles are identical.
|
||||
float CalculateIou(const Rectangle_f& rect1, const Rectangle_f& rect2) {
|
||||
if (!rect1.Intersects(rect2)) return 0.0f;
|
||||
|
||||
// Compute IoU similarity score.
|
||||
const float intersection_area = Rectangle_f(rect1).Intersect(rect2).Area();
|
||||
const float normalization = rect1.Area() + rect2.Area() - intersection_area;
|
||||
return normalization > 0.0f ? intersection_area / normalization : 0.0f;
|
||||
}
|
||||
|
||||
} // namespace mediapipe
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright 2021 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_RECTANGLE_UTIL_H_
|
||||
#define MEDIAPIPE_RECTANGLE_UTIL_H_
|
||||
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/types/span.h"
|
||||
#include "mediapipe/framework/formats/rect.pb.h"
|
||||
#include "mediapipe/framework/port/rectangle.h"
|
||||
|
||||
namespace mediapipe {
|
||||
|
||||
// Converts a NormalizedRect into a Rectangle_f.
|
||||
absl::StatusOr<Rectangle_f> ToRectangle(const mediapipe::NormalizedRect& input);
|
||||
|
||||
// If the new_rect overlaps with any of the rectangles in
|
||||
// existing_rects, then return true. Otherwise, return false.
|
||||
absl::StatusOr<bool> DoesRectOverlap(
|
||||
const mediapipe::NormalizedRect& new_rect,
|
||||
absl::Span<const mediapipe::NormalizedRect> existing_rects,
|
||||
float min_similarity_threshold);
|
||||
|
||||
// Computes the Intersection over Union (IoU) between two rectangles.
|
||||
float CalculateIou(const Rectangle_f& rect1, const Rectangle_f& rect2);
|
||||
|
||||
} // namespace mediapipe
|
||||
|
||||
#endif // MEDIAPIPE_RECTANGLE_UTIL_H_
|
||||
@@ -0,0 +1,180 @@
|
||||
// Copyright 2019 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/util/rectangle_util.h"
|
||||
|
||||
#include "mediapipe/framework/port/gtest.h"
|
||||
#include "mediapipe/framework/port/status_matchers.h"
|
||||
|
||||
namespace mediapipe {
|
||||
namespace {
|
||||
|
||||
using ::testing::FloatNear;
|
||||
|
||||
class RectangleUtilTest : public testing::Test {
|
||||
protected:
|
||||
RectangleUtilTest() {
|
||||
// 0.4 ================
|
||||
// | | | |
|
||||
// 0.3 ===================== | NR2 | |
|
||||
// | | | NR1 | | | NR4 |
|
||||
// 0.2 | NR0 | =========== ================
|
||||
// | | | | | |
|
||||
// 0.1 =====|=============== |
|
||||
// | NR3 | | |
|
||||
// 0.0 ================ |
|
||||
// | NR5 |
|
||||
// -0.1 ===========
|
||||
// 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2
|
||||
|
||||
// NormalizedRect nr_0.
|
||||
nr_0.set_x_center(0.2);
|
||||
nr_0.set_y_center(0.2);
|
||||
nr_0.set_width(0.2);
|
||||
nr_0.set_height(0.2);
|
||||
|
||||
// NormalizedRect nr_1.
|
||||
nr_1.set_x_center(0.4);
|
||||
nr_1.set_y_center(0.2);
|
||||
nr_1.set_width(0.2);
|
||||
nr_1.set_height(0.2);
|
||||
|
||||
// NormalizedRect nr_2.
|
||||
nr_2.set_x_center(1.0);
|
||||
nr_2.set_y_center(0.3);
|
||||
nr_2.set_width(0.2);
|
||||
nr_2.set_height(0.2);
|
||||
|
||||
// NormalizedRect nr_3.
|
||||
nr_3.set_x_center(0.35);
|
||||
nr_3.set_y_center(0.15);
|
||||
nr_3.set_width(0.3);
|
||||
nr_3.set_height(0.3);
|
||||
|
||||
// NormalizedRect nr_4.
|
||||
nr_4.set_x_center(1.1);
|
||||
nr_4.set_y_center(0.3);
|
||||
nr_4.set_width(0.2);
|
||||
nr_4.set_height(0.2);
|
||||
|
||||
// NormalizedRect nr_5.
|
||||
nr_5.set_x_center(0.5);
|
||||
nr_5.set_y_center(0.05);
|
||||
nr_5.set_width(0.2);
|
||||
nr_5.set_height(0.3);
|
||||
}
|
||||
mediapipe::NormalizedRect nr_0, nr_1, nr_2, nr_3, nr_4, nr_5;
|
||||
};
|
||||
|
||||
TEST_F(RectangleUtilTest, OverlappingWithListLargeThreshold) {
|
||||
constexpr float kMinSimilarityThreshold = 0.15;
|
||||
|
||||
std::vector<NormalizedRect> existing_rects;
|
||||
existing_rects.push_back(nr_0);
|
||||
existing_rects.push_back(nr_5);
|
||||
existing_rects.push_back(nr_2);
|
||||
|
||||
EXPECT_THAT(DoesRectOverlap(nr_3, existing_rects, kMinSimilarityThreshold),
|
||||
IsOkAndHolds(true));
|
||||
EXPECT_THAT(DoesRectOverlap(nr_4, existing_rects, kMinSimilarityThreshold),
|
||||
IsOkAndHolds(true));
|
||||
EXPECT_THAT(DoesRectOverlap(nr_1, existing_rects, kMinSimilarityThreshold),
|
||||
IsOkAndHolds(false));
|
||||
}
|
||||
|
||||
TEST_F(RectangleUtilTest, OverlappingWithListSmallThreshold) {
|
||||
constexpr float kMinSimilarityThreshold = 0.1;
|
||||
|
||||
std::vector<NormalizedRect> existing_rects;
|
||||
existing_rects.push_back(nr_0);
|
||||
existing_rects.push_back(nr_5);
|
||||
existing_rects.push_back(nr_2);
|
||||
|
||||
EXPECT_THAT(DoesRectOverlap(nr_3, existing_rects, kMinSimilarityThreshold),
|
||||
IsOkAndHolds(true));
|
||||
EXPECT_THAT(DoesRectOverlap(nr_4, existing_rects, kMinSimilarityThreshold),
|
||||
IsOkAndHolds(true));
|
||||
EXPECT_THAT(DoesRectOverlap(nr_1, existing_rects, kMinSimilarityThreshold),
|
||||
IsOkAndHolds(true));
|
||||
}
|
||||
|
||||
TEST_F(RectangleUtilTest, NonOverlappingWithList) {
|
||||
constexpr float kMinSimilarityThreshold = 0.1;
|
||||
|
||||
std::vector<NormalizedRect> existing_rects;
|
||||
existing_rects.push_back(nr_0);
|
||||
existing_rects.push_back(nr_3);
|
||||
existing_rects.push_back(nr_5);
|
||||
|
||||
EXPECT_THAT(DoesRectOverlap(nr_2, existing_rects, kMinSimilarityThreshold),
|
||||
IsOkAndHolds(false));
|
||||
EXPECT_THAT(DoesRectOverlap(nr_4, existing_rects, kMinSimilarityThreshold),
|
||||
IsOkAndHolds(false));
|
||||
}
|
||||
|
||||
TEST_F(RectangleUtilTest, OverlappingWithEmptyList) {
|
||||
constexpr float kMinSimilarityThreshold = 0.1;
|
||||
std::vector<NormalizedRect> existing_rects;
|
||||
|
||||
EXPECT_THAT(DoesRectOverlap(nr_2, existing_rects, kMinSimilarityThreshold),
|
||||
IsOkAndHolds(false));
|
||||
EXPECT_THAT(DoesRectOverlap(nr_4, existing_rects, kMinSimilarityThreshold),
|
||||
IsOkAndHolds(false));
|
||||
}
|
||||
|
||||
TEST_F(RectangleUtilTest, OverlapSimilarityOverlapping) {
|
||||
constexpr float kMaxAbsoluteError = 1e-4;
|
||||
constexpr float kExpectedIou = 4.0 / 9.0;
|
||||
auto rect_1 = ToRectangle(nr_1);
|
||||
auto rect_3 = ToRectangle(nr_3);
|
||||
MP_ASSERT_OK(rect_1);
|
||||
MP_ASSERT_OK(rect_3);
|
||||
EXPECT_THAT(CalculateIou(*rect_1, *rect_3),
|
||||
FloatNear(kExpectedIou, kMaxAbsoluteError));
|
||||
}
|
||||
|
||||
TEST_F(RectangleUtilTest, OverlapSimilarityNotOverlapping) {
|
||||
constexpr float kMaxAbsoluteError = 1e-4;
|
||||
constexpr float kExpectedIou = 0.0;
|
||||
auto rect_1 = ToRectangle(nr_1);
|
||||
auto rect_2 = ToRectangle(nr_2);
|
||||
MP_ASSERT_OK(rect_1);
|
||||
MP_ASSERT_OK(rect_2);
|
||||
EXPECT_THAT(CalculateIou(*rect_1, *rect_2),
|
||||
FloatNear(kExpectedIou, kMaxAbsoluteError));
|
||||
}
|
||||
|
||||
TEST_F(RectangleUtilTest, NormRectToRectangleSuccess) {
|
||||
const Rectangle_f kExpectedRect(/*xmin=*/0.1, /*ymin=*/0.1,
|
||||
/*width=*/0.2, /*height=*/0.2);
|
||||
EXPECT_THAT(ToRectangle(nr_0), IsOkAndHolds(kExpectedRect));
|
||||
}
|
||||
|
||||
TEST_F(RectangleUtilTest, NormRectToRectangleFail) {
|
||||
mediapipe::NormalizedRect invalid_nr;
|
||||
invalid_nr.set_x_center(0.2);
|
||||
EXPECT_THAT(ToRectangle(invalid_nr), testing::Not(IsOk()));
|
||||
|
||||
invalid_nr.set_y_center(0.2);
|
||||
invalid_nr.set_width(-0.2);
|
||||
invalid_nr.set_height(0.2);
|
||||
EXPECT_THAT(ToRectangle(invalid_nr), testing::Not(IsOk()));
|
||||
|
||||
invalid_nr.set_width(0.2);
|
||||
invalid_nr.set_height(-0.2);
|
||||
EXPECT_THAT(ToRectangle(invalid_nr), testing::Not(IsOk()));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace mediapipe
|
||||
Reference in New Issue
Block a user