|
40 | 40 |
|
41 | 41 | #include <array> |
42 | 42 | #include <exception> |
| 43 | +#include <optional> |
43 | 44 | #include <string> |
44 | 45 | #include <typeinfo> |
45 | 46 | #if !defined(ENABLE_VAAPI) || defined(_WIN32) |
@@ -146,6 +147,8 @@ struct Impl { |
146 | 147 | const GVA::Rect<double> &rectangle, std::vector<render::Prim> &prims) const; |
147 | 148 | void find_gvafpscounter_element(); |
148 | 149 | void parse_displ_config(); |
| 150 | + inline bool is_ROI_filtered_out(const std::string &label) const; |
| 151 | + |
149 | 152 | std::unique_ptr<Renderer> createRenderer(std::shared_ptr<ColorConverter> converter); |
150 | 153 |
|
151 | 154 | std::unique_ptr<Renderer> createGPURenderer(dlstreamer::ImageFormat format, |
@@ -192,6 +195,8 @@ struct Impl { |
192 | 195 | uint thickness = DEFAULT_THICKNESS; |
193 | 196 | int font_type = cv::FONT_HERSHEY_TRIPLEX; |
194 | 197 | double font_scale = DEFAULT_TEXT_SCALE; |
| 198 | + std::optional<std::unordered_set<std::string>> include_labels_filter; |
| 199 | + std::optional<std::unordered_set<std::string>> exclude_labels_filter; |
195 | 200 | } _displCfg; |
196 | 201 | }; |
197 | 202 |
|
@@ -899,54 +904,62 @@ bool Impl::render_va(cv::Mat *buffer) { |
899 | 904 | return true; |
900 | 905 | } |
901 | 906 |
|
902 | | -void Impl::preparePrimsForRoi(GVA::RegionOfInterest &roi, std::vector<render::Prim> &prims) const { |
903 | | - size_t color_index = roi.label_id(); |
| 907 | +inline bool Impl::is_ROI_filtered_out(const std::string &label) const { |
| 908 | + return _displCfg.include_labels_filter.has_value() |
| 909 | + ? (_displCfg.include_labels_filter->count(label) == 0) |
| 910 | + : (_displCfg.exclude_labels_filter.has_value() && _displCfg.exclude_labels_filter->count(label) != 0); |
| 911 | +} |
904 | 912 |
|
905 | | - auto rect_u32 = roi.rect(); |
906 | | - GVA::Rect<double> rect = {safe_convert<double>(rect_u32.x), safe_convert<double>(rect_u32.y), |
907 | | - safe_convert<double>(rect_u32.w), safe_convert<double>(rect_u32.h)}; |
| 913 | +void Impl::preparePrimsForRoi(GVA::RegionOfInterest &roi, std::vector<render::Prim> &prims) const { |
| 914 | + if (!is_ROI_filtered_out(roi.label())) { |
| 915 | + size_t color_index = roi.label_id(); |
908 | 916 |
|
909 | | - clip_rect(rect.x, rect.y, rect.w, rect.h, _vinfo); |
| 917 | + auto rect_u32 = roi.rect(); |
| 918 | + GVA::Rect<double> rect = {safe_convert<double>(rect_u32.x), safe_convert<double>(rect_u32.y), |
| 919 | + safe_convert<double>(rect_u32.w), safe_convert<double>(rect_u32.h)}; |
910 | 920 |
|
911 | | - std::ostringstream text; |
912 | | - const int object_id = roi.object_id(); |
913 | | - if (object_id > 0) { |
914 | | - text << object_id << ": "; |
915 | | - color_index = object_id; |
916 | | - } |
| 921 | + clip_rect(rect.x, rect.y, rect.w, rect.h, _vinfo); |
917 | 922 |
|
918 | | - if (roi.label().size() > 1) { |
919 | | - appendStr(text, roi.label()); |
920 | | - text << int(roi.confidence() * 100) << "%"; |
921 | | - } |
| 923 | + std::ostringstream text; |
| 924 | + const int object_id = roi.object_id(); |
| 925 | + if (object_id > 0) { |
| 926 | + text << object_id << ": "; |
| 927 | + color_index = object_id; |
| 928 | + } |
922 | 929 |
|
923 | | - // Prepare primitives for tensors |
924 | | - for (auto &tensor : roi.tensors()) { |
925 | | - preparePrimsForTensor(tensor, rect, prims, color_index); |
926 | | - if (!tensor.is_detection()) { |
927 | | - appendStr(text, tensor.label()); |
| 930 | + if (roi.label().size() > 1) { |
| 931 | + appendStr(text, roi.label()); |
| 932 | + text << int(roi.confidence() * 100) << "%"; |
928 | 933 | } |
929 | | - } |
930 | 934 |
|
931 | | - // set color |
932 | | - Color color = indexToColor(color_index); |
933 | | - if (_displCfg.color_idx != DEFAULT_COLOR_IDX) |
934 | | - color = _default_color; |
935 | | - |
936 | | - // put rectangle |
937 | | - cv::Rect bbox_rect(rect.x, rect.y, rect.w, rect.h); |
938 | | - if (!_obb) |
939 | | - prims.emplace_back(render::Rect(bbox_rect, color, _displCfg.thickness, roi.rotation())); |
940 | | - |
941 | | - // put text |
942 | | - if (_displCfg.show_labels) |
943 | | - if (text.str().size() != 0) { |
944 | | - cv::Point2f pos(rect.x, rect.y - 5.f); |
945 | | - if (pos.y < 0) |
946 | | - pos.y = rect.y + 30.f; |
947 | | - prims.emplace_back(render::Text(text.str(), pos, _displCfg.font_type, _displCfg.font_scale, color)); |
| 935 | + // Prepare primitives for tensors |
| 936 | + for (auto &tensor : roi.tensors()) { |
| 937 | + preparePrimsForTensor(tensor, rect, prims, color_index); |
| 938 | + if (!tensor.is_detection()) { |
| 939 | + appendStr(text, tensor.label()); |
| 940 | + } |
948 | 941 | } |
949 | 942 |
|
| 943 | + // set color |
| 944 | + Color color = indexToColor(color_index); |
| 945 | + if (_displCfg.color_idx != DEFAULT_COLOR_IDX) |
| 946 | + color = _default_color; |
| 947 | + |
| 948 | + // put rectangle |
| 949 | + cv::Rect bbox_rect(rect.x, rect.y, rect.w, rect.h); |
| 950 | + if (!_obb) |
| 951 | + prims.emplace_back(render::Rect(bbox_rect, color, _displCfg.thickness, roi.rotation())); |
| 952 | + |
| 953 | + // put text |
| 954 | + if (_displCfg.show_labels) |
| 955 | + if (text.str().size() != 0) { |
| 956 | + cv::Point2f pos(rect.x, rect.y - 5.f); |
| 957 | + if (pos.y < 0) |
| 958 | + pos.y = rect.y + 30.f; |
| 959 | + prims.emplace_back(render::Text(text.str(), pos, _displCfg.font_type, _displCfg.font_scale, color)); |
| 960 | + } |
| 961 | + } |
| 962 | + |
950 | 963 | // put avg-fps from gvafpscounter element |
951 | 964 | if (_displ_avgfps) { |
952 | 965 | std::ostringstream fpstext; |
@@ -1222,6 +1235,21 @@ void Impl::parse_displ_config() { |
1222 | 1235 | } |
1223 | 1236 | cfg.erase(iter); |
1224 | 1237 | } |
| 1238 | + if (iter = cfg.find("show-labels"); iter != cfg.end()) { |
| 1239 | + _displCfg.include_labels_filter = std::unordered_set<std::string>(); |
| 1240 | + Utils::parseFilterConfig(iter->second, *_displCfg.include_labels_filter); |
| 1241 | + cfg.erase(iter); |
| 1242 | + } |
| 1243 | + if (iter = cfg.find("hide-labels"); iter != cfg.end()) { |
| 1244 | + if (_displCfg.include_labels_filter.has_value() && !(_displCfg.include_labels_filter->empty())) { |
| 1245 | + GST_WARNING("[gvawatermarkimpl] Both 'show-labels' and 'hide-labels' parameters are set, " |
| 1246 | + "'hide-labels' will be ignored."); |
| 1247 | + } else { |
| 1248 | + _displCfg.exclude_labels_filter = std::unordered_set<std::string>(); |
| 1249 | + Utils::parseFilterConfig(iter->second, *_displCfg.exclude_labels_filter); |
| 1250 | + } |
| 1251 | + cfg.erase(iter); |
| 1252 | + } |
1225 | 1253 | } catch (...) { |
1226 | 1254 | if (iter == cfg.end()) |
1227 | 1255 | std::throw_with_nested( |
|
0 commit comments