From 2fe40000543bf2f82fb5ee1b513c6e4be0049420 Mon Sep 17 00:00:00 2001 From: scepter914 Date: Tue, 18 Oct 2022 11:51:54 +0900 Subject: [PATCH 1/7] feat(perception_utils): add classification util function with string Signed-off-by: scepter914 --- .../object_classification.hpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/common/perception_utils/include/perception_utils/object_classification.hpp b/common/perception_utils/include/perception_utils/object_classification.hpp index dba9236f2c3c0..25153bfbc6561 100644 --- a/common/perception_utils/include/perception_utils/object_classification.hpp +++ b/common/perception_utils/include/perception_utils/object_classification.hpp @@ -92,6 +92,52 @@ inline bool isLargeVehicle(const std::vector & object_clas auto highest_prob_label = getHighestProbLabel(object_classifications); return isLargeVehicle(highest_prob_label); } + +inline uint8_t toObjectClassification(const std::string & class_name) +{ + if (class_name == "UNKNOWN") { + return ObjectClassification::UNKNOWN; + } else if (class_name == "CAR") { + return ObjectClassification::CAR; + } else if (class_name == "TRUCK") { + return ObjectClassification::TRUCK; + } else if (class_name == "BUS") { + return ObjectClassification::BUS; + } else if (class_name == "TRAILER") { + return ObjectClassification::TRAILER; + } else if (class_name == "MOTORCYCLE") { + return ObjectClassification::MOTORCYCLE; + } else if (class_name == "BICYCLE") { + return ObjectClassification::BICYCLE; + } else if (class_name == "PEDESTRIAN") { + return ObjectClassification::PEDESTRIAN; + } else { + return ObjectClassification::UNKNOWN; + } +} + +inline std::string toString(const uint8_t label) +{ + if (label == ObjectClassification::UNKNOWN) { + return "UNKNOWN"; + } else if (label == ObjectClassification::CAR) { + return "CAR"; + } else if (label == ObjectClassification::TRUCK) { + return "TRUCK"; + } else if (label == ObjectClassification::BUS) { + return "BUS"; + } else if (label == ObjectClassification::TRAILER) { + return "TRAILER"; + } else if (label == ObjectClassification::MOTORCYCLE) { + return "MOTORCYCLE"; + } else if (label == ObjectClassification::BICYCLE) { + return "BICYCLE"; + } else if (label == ObjectClassification::PEDESTRIAN) { + return "PEDESTRIAN"; + } else { + return "UNKNOWN"; + } +} } // namespace perception_utils #endif // PERCEPTION_UTILS__OBJECT_CLASSIFICATION_HPP_ From 90684739fc6317446b8f7e7c81d7555baeb8d4c6 Mon Sep 17 00:00:00 2001 From: scepter914 Date: Tue, 18 Oct 2022 11:52:03 +0900 Subject: [PATCH 2/7] add unit test Signed-off-by: scepter914 --- .../test/src/test_object_classification.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/common/perception_utils/test/src/test_object_classification.cpp b/common/perception_utils/test/src/test_object_classification.cpp index 9758266642255..bbb9393b4b9f5 100644 --- a/common/perception_utils/test/src/test_object_classification.cpp +++ b/common/perception_utils/test/src/test_object_classification.cpp @@ -98,3 +98,34 @@ TEST(object_classification, test_getHighestProbClassification) EXPECT_NEAR(classification.probability, 0.8, epsilon); } } + +TEST(object_classification, test_toObjectClassification) +{ + using autoware_auto_perception_msgs::msg::ObjectClassification; + using perception_utils::toObjectClassification; + + EXPECT_EQ(toObjectClassification("UNKNOWN"), ObjectClassification::UNKNOWN); + EXPECT_EQ(toObjectClassification("CAR"), ObjectClassification::CAR); + EXPECT_EQ(toObjectClassification("TRUCK"), ObjectClassification::TRUCK); + EXPECT_EQ(toObjectClassification("BUS"), ObjectClassification::BUS); + EXPECT_EQ(toObjectClassification("TRAILER"), ObjectClassification::TRAILER); + EXPECT_EQ(toObjectClassification("MOTORCYCLE"), ObjectClassification::MOTORCYCLE); + EXPECT_EQ(toObjectClassification("BICYCLE"), ObjectClassification::BICYCLE); + EXPECT_EQ(toObjectClassification("PEDESTRIAN"), ObjectClassification::PEDESTRIAN); + EXPECT_EQ(toObjectClassification(""), ObjectClassification::UNKNOWN); +} + +TEST(object_classification, test_toString) +{ + using autoware_auto_perception_msgs::msg::ObjectClassification; + using perception_utils::toString; + + EXPECT_EQ(toString(ObjectClassification::UNKNOWN), "UNKNOWN"); + EXPECT_EQ(toString(ObjectClassification::CAR), "CAR"); + EXPECT_EQ(toString(ObjectClassification::TRUCK), "TRUCK"); + EXPECT_EQ(toString(ObjectClassification::BUS), "BUS"); + EXPECT_EQ(toString(ObjectClassification::TRAILER), "TRAILER"); + EXPECT_EQ(toString(ObjectClassification::MOTORCYCLE), "MOTORCYCLE"); + EXPECT_EQ(toString(ObjectClassification::BICYCLE), "BICYCLE"); + EXPECT_EQ(toString(ObjectClassification::PEDESTRIAN), "PEDESTRIAN"); +} From b3a460b13443c52a9afcccc1d90be53cdeb17a4e Mon Sep 17 00:00:00 2001 From: scepter914 Date: Tue, 18 Oct 2022 11:54:51 +0900 Subject: [PATCH 3/7] add include string Signed-off-by: scepter914 --- .../include/perception_utils/object_classification.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/common/perception_utils/include/perception_utils/object_classification.hpp b/common/perception_utils/include/perception_utils/object_classification.hpp index 25153bfbc6561..da95a0d58b2e2 100644 --- a/common/perception_utils/include/perception_utils/object_classification.hpp +++ b/common/perception_utils/include/perception_utils/object_classification.hpp @@ -17,6 +17,7 @@ #include "autoware_auto_perception_msgs/msg/object_classification.hpp" +#include #include namespace perception_utils From 75d4987b0a869bf681c6d041e88d4a924ed9091d Mon Sep 17 00:00:00 2001 From: scepter914 Date: Tue, 18 Oct 2022 12:07:27 +0900 Subject: [PATCH 4/7] fix function interface Signed-off-by: scepter914 --- .../object_classification.hpp | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/common/perception_utils/include/perception_utils/object_classification.hpp b/common/perception_utils/include/perception_utils/object_classification.hpp index da95a0d58b2e2..b7b707f98a725 100644 --- a/common/perception_utils/include/perception_utils/object_classification.hpp +++ b/common/perception_utils/include/perception_utils/object_classification.hpp @@ -94,7 +94,7 @@ inline bool isLargeVehicle(const std::vector & object_clas return isLargeVehicle(highest_prob_label); } -inline uint8_t toObjectClassification(const std::string & class_name) +inline uint8_t toLabel(const std::string & class_name) { if (class_name == "UNKNOWN") { return ObjectClassification::UNKNOWN; @@ -117,6 +117,23 @@ inline uint8_t toObjectClassification(const std::string & class_name) } } +inline ObjectClassification toObjectClassification( + const std::string & class_name, float probability) +{ + ObjectClassification classification; + classification.label = toLabel(class_name); + classification.probability = probability; + return classification; +} + +inline std::vector toObjectClassifications( + const std::string & class_name, float probability) +{ + std::vector classifications; + classifications.push_back(toObjectClassification(class_name, probability)); + return classifications; +} + inline std::string toString(const uint8_t label) { if (label == ObjectClassification::UNKNOWN) { @@ -139,6 +156,18 @@ inline std::string toString(const uint8_t label) return "UNKNOWN"; } } + +inline std::string toString(const ObjectClassification object_classification) +{ + return toString(object_classification.label); +} + +inline std::string toString(const std::vector object_classifications) +{ + auto highest_prob_label = getHighestProbLabel(object_classifications); + return toString(highest_prob_label); +} + } // namespace perception_utils #endif // PERCEPTION_UTILS__OBJECT_CLASSIFICATION_HPP_ From 72a4998fc948cb107ae3c6fb1a5ef31d82fae998 Mon Sep 17 00:00:00 2001 From: scepter914 Date: Tue, 18 Oct 2022 12:21:27 +0900 Subject: [PATCH 5/7] fix unit test Signed-off-by: scepter914 --- .../test/src/test_object_classification.cpp | 74 ++++++++++++++----- 1 file changed, 56 insertions(+), 18 deletions(-) diff --git a/common/perception_utils/test/src/test_object_classification.cpp b/common/perception_utils/test/src/test_object_classification.cpp index bbb9393b4b9f5..f9001231511fa 100644 --- a/common/perception_utils/test/src/test_object_classification.cpp +++ b/common/perception_utils/test/src/test_object_classification.cpp @@ -99,20 +99,38 @@ TEST(object_classification, test_getHighestProbClassification) } } -TEST(object_classification, test_toObjectClassification) +TEST(object_classification, test_fromString) { using autoware_auto_perception_msgs::msg::ObjectClassification; + using perception_utils::toLabel; using perception_utils::toObjectClassification; + using perception_utils::toObjectClassifications; + + // toLabel + { + EXPECT_EQ(toLabel("UNKNOWN"), ObjectClassification::UNKNOWN); + EXPECT_EQ(toLabel("CAR"), ObjectClassification::CAR); + EXPECT_EQ(toLabel("TRUCK"), ObjectClassification::TRUCK); + EXPECT_EQ(toLabel("BUS"), ObjectClassification::BUS); + EXPECT_EQ(toLabel("TRAILER"), ObjectClassification::TRAILER); + EXPECT_EQ(toLabel("MOTORCYCLE"), ObjectClassification::MOTORCYCLE); + EXPECT_EQ(toLabel("BICYCLE"), ObjectClassification::BICYCLE); + EXPECT_EQ(toLabel("PEDESTRIAN"), ObjectClassification::PEDESTRIAN); + EXPECT_EQ(toLabel(""), ObjectClassification::UNKNOWN); + } - EXPECT_EQ(toObjectClassification("UNKNOWN"), ObjectClassification::UNKNOWN); - EXPECT_EQ(toObjectClassification("CAR"), ObjectClassification::CAR); - EXPECT_EQ(toObjectClassification("TRUCK"), ObjectClassification::TRUCK); - EXPECT_EQ(toObjectClassification("BUS"), ObjectClassification::BUS); - EXPECT_EQ(toObjectClassification("TRAILER"), ObjectClassification::TRAILER); - EXPECT_EQ(toObjectClassification("MOTORCYCLE"), ObjectClassification::MOTORCYCLE); - EXPECT_EQ(toObjectClassification("BICYCLE"), ObjectClassification::BICYCLE); - EXPECT_EQ(toObjectClassification("PEDESTRIAN"), ObjectClassification::PEDESTRIAN); - EXPECT_EQ(toObjectClassification(""), ObjectClassification::UNKNOWN); + // Classification + { + auto classification = toObjectClassification("CAR", 0.7); + EXPECT_EQ(classification.label, ObjectClassification::CAR); + EXPECT_NEAR(classification.probability, 0.7, epsilon); + } + // Classifications + { + auto classifications = toObjectClassifications("CAR", 0.7); + EXPECT_EQ(classifications.at(0).label, ObjectClassification::CAR); + EXPECT_NEAR(classifications.at(0).probability, 0.7, epsilon); + } } TEST(object_classification, test_toString) @@ -120,12 +138,32 @@ TEST(object_classification, test_toString) using autoware_auto_perception_msgs::msg::ObjectClassification; using perception_utils::toString; - EXPECT_EQ(toString(ObjectClassification::UNKNOWN), "UNKNOWN"); - EXPECT_EQ(toString(ObjectClassification::CAR), "CAR"); - EXPECT_EQ(toString(ObjectClassification::TRUCK), "TRUCK"); - EXPECT_EQ(toString(ObjectClassification::BUS), "BUS"); - EXPECT_EQ(toString(ObjectClassification::TRAILER), "TRAILER"); - EXPECT_EQ(toString(ObjectClassification::MOTORCYCLE), "MOTORCYCLE"); - EXPECT_EQ(toString(ObjectClassification::BICYCLE), "BICYCLE"); - EXPECT_EQ(toString(ObjectClassification::PEDESTRIAN), "PEDESTRIAN"); + // from label + { + EXPECT_EQ(toString(ObjectClassification::UNKNOWN), "UNKNOWN"); + EXPECT_EQ(toString(ObjectClassification::CAR), "CAR"); + EXPECT_EQ(toString(ObjectClassification::TRUCK), "TRUCK"); + EXPECT_EQ(toString(ObjectClassification::BUS), "BUS"); + EXPECT_EQ(toString(ObjectClassification::TRAILER), "TRAILER"); + EXPECT_EQ(toString(ObjectClassification::MOTORCYCLE), "MOTORCYCLE"); + EXPECT_EQ(toString(ObjectClassification::BICYCLE), "BICYCLE"); + EXPECT_EQ(toString(ObjectClassification::PEDESTRIAN), "PEDESTRIAN"); + } + + // from ObjectClassification + { + auto classification = createObjectClassification(ObjectClassification::CAR, 0.8); + + EXPECT_EQ(toString(classification), "CAR"); + } + + // from ObjectClassifications + { + std::vector classifications; + classifications.push_back(createObjectClassification(ObjectClassification::CAR, 0.5)); + classifications.push_back(createObjectClassification(ObjectClassification::TRUCK, 0.8)); + classifications.push_back(createObjectClassification(ObjectClassification::BUS, 0.7)); + + EXPECT_EQ(toString(classifications), "TRUCK"); + } } From 802b254e4f8dae67a3c84009127d4073c6e04088 Mon Sep 17 00:00:00 2001 From: scepter914 Date: Fri, 21 Oct 2022 15:13:28 +0900 Subject: [PATCH 6/7] change function name Signed-off-by: scepter914 --- .../object_classification.hpp | 11 +++++---- .../test/src/test_object_classification.cpp | 24 +++++++++---------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/common/perception_utils/include/perception_utils/object_classification.hpp b/common/perception_utils/include/perception_utils/object_classification.hpp index b7b707f98a725..2f6b008ca4e96 100644 --- a/common/perception_utils/include/perception_utils/object_classification.hpp +++ b/common/perception_utils/include/perception_utils/object_classification.hpp @@ -134,7 +134,7 @@ inline std::vector toObjectClassifications( return classifications; } -inline std::string toString(const uint8_t label) +inline std::string convertLabelToString(const uint8_t label) { if (label == ObjectClassification::UNKNOWN) { return "UNKNOWN"; @@ -157,15 +157,16 @@ inline std::string toString(const uint8_t label) } } -inline std::string toString(const ObjectClassification object_classification) +inline std::string convertLabelToString(const ObjectClassification object_classification) { - return toString(object_classification.label); + return convertLabelToString(object_classification.label); } -inline std::string toString(const std::vector object_classifications) +inline std::string convertLabelToString( + const std::vector object_classifications) { auto highest_prob_label = getHighestProbLabel(object_classifications); - return toString(highest_prob_label); + return convertLabelToString(highest_prob_label); } } // namespace perception_utils diff --git a/common/perception_utils/test/src/test_object_classification.cpp b/common/perception_utils/test/src/test_object_classification.cpp index f9001231511fa..7590ec07d549b 100644 --- a/common/perception_utils/test/src/test_object_classification.cpp +++ b/common/perception_utils/test/src/test_object_classification.cpp @@ -133,28 +133,28 @@ TEST(object_classification, test_fromString) } } -TEST(object_classification, test_toString) +TEST(object_classification, test_convertLabelToString) { using autoware_auto_perception_msgs::msg::ObjectClassification; - using perception_utils::toString; + using perception_utils::convertLabelToString; // from label { - EXPECT_EQ(toString(ObjectClassification::UNKNOWN), "UNKNOWN"); - EXPECT_EQ(toString(ObjectClassification::CAR), "CAR"); - EXPECT_EQ(toString(ObjectClassification::TRUCK), "TRUCK"); - EXPECT_EQ(toString(ObjectClassification::BUS), "BUS"); - EXPECT_EQ(toString(ObjectClassification::TRAILER), "TRAILER"); - EXPECT_EQ(toString(ObjectClassification::MOTORCYCLE), "MOTORCYCLE"); - EXPECT_EQ(toString(ObjectClassification::BICYCLE), "BICYCLE"); - EXPECT_EQ(toString(ObjectClassification::PEDESTRIAN), "PEDESTRIAN"); + EXPECT_EQ(convertLabelToString(ObjectClassification::UNKNOWN), "UNKNOWN"); + EXPECT_EQ(convertLabelToString(ObjectClassification::CAR), "CAR"); + EXPECT_EQ(convertLabelToString(ObjectClassification::TRUCK), "TRUCK"); + EXPECT_EQ(convertLabelToString(ObjectClassification::BUS), "BUS"); + EXPECT_EQ(convertLabelToString(ObjectClassification::TRAILER), "TRAILER"); + EXPECT_EQ(convertLabelToString(ObjectClassification::MOTORCYCLE), "MOTORCYCLE"); + EXPECT_EQ(convertLabelToString(ObjectClassification::BICYCLE), "BICYCLE"); + EXPECT_EQ(convertLabelToString(ObjectClassification::PEDESTRIAN), "PEDESTRIAN"); } // from ObjectClassification { auto classification = createObjectClassification(ObjectClassification::CAR, 0.8); - EXPECT_EQ(toString(classification), "CAR"); + EXPECT_EQ(convertLabelToString(classification), "CAR"); } // from ObjectClassifications @@ -164,6 +164,6 @@ TEST(object_classification, test_toString) classifications.push_back(createObjectClassification(ObjectClassification::TRUCK, 0.8)); classifications.push_back(createObjectClassification(ObjectClassification::BUS, 0.7)); - EXPECT_EQ(toString(classifications), "TRUCK"); + EXPECT_EQ(convertLabelToString(classifications), "TRUCK"); } } From 15d8d0afa3242d3a7120326757d49cc428a73bf3 Mon Sep 17 00:00:00 2001 From: scepter914 Date: Mon, 24 Oct 2022 15:23:53 +0900 Subject: [PATCH 7/7] change to except throw Signed-off-by: scepter914 --- .../include/perception_utils/object_classification.hpp | 2 +- common/perception_utils/test/src/test_object_classification.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/perception_utils/include/perception_utils/object_classification.hpp b/common/perception_utils/include/perception_utils/object_classification.hpp index 2f6b008ca4e96..3c38c1f16b21b 100644 --- a/common/perception_utils/include/perception_utils/object_classification.hpp +++ b/common/perception_utils/include/perception_utils/object_classification.hpp @@ -113,7 +113,7 @@ inline uint8_t toLabel(const std::string & class_name) } else if (class_name == "PEDESTRIAN") { return ObjectClassification::PEDESTRIAN; } else { - return ObjectClassification::UNKNOWN; + throw std::runtime_error("Invalid Classification label."); } } diff --git a/common/perception_utils/test/src/test_object_classification.cpp b/common/perception_utils/test/src/test_object_classification.cpp index 7590ec07d549b..be389634ef5a2 100644 --- a/common/perception_utils/test/src/test_object_classification.cpp +++ b/common/perception_utils/test/src/test_object_classification.cpp @@ -116,7 +116,7 @@ TEST(object_classification, test_fromString) EXPECT_EQ(toLabel("MOTORCYCLE"), ObjectClassification::MOTORCYCLE); EXPECT_EQ(toLabel("BICYCLE"), ObjectClassification::BICYCLE); EXPECT_EQ(toLabel("PEDESTRIAN"), ObjectClassification::PEDESTRIAN); - EXPECT_EQ(toLabel(""), ObjectClassification::UNKNOWN); + EXPECT_THROW(toLabel(""), std::runtime_error); } // Classification