test_osd.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <memory>
  22. #include <string>
  23. #include <utility>
  24. #include "opencv2/highgui/highgui.hpp"
  25. #include "opencv2/imgproc/imgproc.hpp"
  26. #if (CV_MAJOR_VERSION >= 3)
  27. #include "opencv2/imgcodecs/imgcodecs.hpp"
  28. #endif
  29. #include "cnstream_frame_va.hpp"
  30. #include "cnstream_module.hpp"
  31. #include "osd.hpp"
  32. #include "test_base.hpp"
  33. namespace cnstream {
  34. static constexpr const char *gname = "osd";
  35. static constexpr const char *glabel_path = "../../modules/unitest/osd/test_label.txt";
  36. TEST(Osd, Construct) {
  37. std::shared_ptr<Module> osd = std::make_shared<Osd>(gname);
  38. EXPECT_STREQ(osd->GetName().c_str(), gname);
  39. }
  40. TEST(Osd, OpenClose) {
  41. std::shared_ptr<Module> osd = std::make_shared<Osd>(gname);
  42. ModuleParamSet param;
  43. EXPECT_TRUE(osd->Open(param));
  44. param["label_path"] = "test-osd";
  45. EXPECT_TRUE(osd->Open(param)) << "if can not read labels, function should not return false";
  46. std::string label_path = GetExePath() + glabel_path;
  47. param["label_path"] = label_path;
  48. EXPECT_TRUE(osd->Open(param));
  49. param.clear();
  50. param["label_size"] = "large";
  51. EXPECT_TRUE(osd->Open(param));
  52. param["label_size"] = "normal";
  53. EXPECT_TRUE(osd->Open(param));
  54. param["label_size"] = "large";
  55. EXPECT_TRUE(osd->Open(param));
  56. param["label_size"] = "larger";
  57. EXPECT_TRUE(osd->Open(param));
  58. param["label_size"] = "small";
  59. EXPECT_TRUE(osd->Open(param));
  60. param["label_size"] = "smaller";
  61. EXPECT_TRUE(osd->Open(param));
  62. param["label_size"] = "0.9";
  63. EXPECT_TRUE(osd->Open(param));
  64. param.clear();
  65. param["text_scale"] = "1.2";
  66. param["text_thickness"] = "1.5";
  67. param["box_thickness"] = "2";
  68. EXPECT_TRUE(osd->Open(param));
  69. param["secondary_label_path"] = label_path;
  70. param["attr_keys"] = "test_key";
  71. EXPECT_TRUE(osd->Open(param));
  72. param["logo"] = "Cambricon-test";
  73. EXPECT_TRUE(osd->Open(param));
  74. osd->Close();
  75. }
  76. TEST(Osd, Process) {
  77. // create osd
  78. std::shared_ptr<Module> osd = std::make_shared<Osd>(gname);
  79. ModuleParamSet param;
  80. std::string label_path = GetExePath() + glabel_path;
  81. param["label_path"] = label_path;
  82. param["logo"] = "Cambricon-test";
  83. ASSERT_TRUE(osd->Open(param));
  84. // prepare data
  85. int width = 1920;
  86. int height = 1080;
  87. cv::Mat img(height, width, CV_8UC3, cv::Scalar(0, 0, 0));
  88. auto data = cnstream::CNFrameInfo::Create(std::to_string(0));
  89. std::shared_ptr<CNDataFrame> frame(new (std::nothrow) CNDataFrame());
  90. data->SetStreamIndex(0);
  91. frame->frame_id = 1;
  92. data->timestamp = 1000;
  93. frame->width = width;
  94. frame->height = height;
  95. void* ptr_cpu[1] = {img.data};
  96. frame->stride[0] = width;
  97. frame->ctx.dev_type = DevContext::DevType::CPU;
  98. frame->fmt = CNDataFormat::CN_PIXEL_FORMAT_BGR24;
  99. frame->CopyToSyncMem(ptr_cpu, false);
  100. data->collection.Add(kCNDataFrameTag, frame);
  101. std::shared_ptr<CNInferObjs> objs_holder = std::make_shared<CNInferObjs>();
  102. auto obj = std::make_shared<CNInferObject>();
  103. obj->id = std::to_string(11);
  104. CNInferBoundingBox bbox = {0.6, 0.4, 0.6, 0.3};
  105. obj->bbox = bbox;
  106. objs_holder->objs_.push_back(obj);
  107. auto obj2 = std::make_shared<CNInferObject>();
  108. obj2->id = std::to_string(12);
  109. bbox = {0.1, -0.2, 0.3, 0.4};
  110. obj2->bbox = bbox;
  111. objs_holder->objs_.push_back(obj2);
  112. for (int i = 0; i < 5; ++i) {
  113. auto obj = std::make_shared<CNInferObject>();
  114. obj->id = std::to_string(i);
  115. float val = i * 0.1;
  116. CNInferBoundingBox bbox = {val, val, val, val};
  117. obj->bbox = bbox;
  118. objs_holder->objs_.push_back(obj);
  119. }
  120. data->collection.Add(kCNInferObjsTag, objs_holder);
  121. EXPECT_EQ(osd->Process(data), 0);
  122. EXPECT_EQ(osd->Process(data), 0);
  123. frame->width = -1;
  124. EXPECT_EQ(osd->Process(data), -1);
  125. }
  126. TEST(Osd, ProcessSecondary) {
  127. // create osd
  128. std::shared_ptr<Module> osd = std::make_shared<Osd>(gname);
  129. ModuleParamSet param;
  130. std::string label_path = GetExePath() + glabel_path;
  131. param["label_path"] = label_path;
  132. param["secondary_label_path"] = label_path;
  133. param["attr_keys"] = "classification";
  134. ASSERT_TRUE(osd->Open(param));
  135. // prepare data
  136. int width = 1920;
  137. int height = 1080;
  138. cv::Mat img(height, width, CV_8UC3, cv::Scalar(0, 0, 0));
  139. auto data = cnstream::CNFrameInfo::Create(std::to_string(0));
  140. std::shared_ptr<CNDataFrame> frame(new (std::nothrow) CNDataFrame());
  141. data->SetStreamIndex(0);
  142. frame->frame_id = 1;
  143. data->timestamp = 1000;
  144. frame->width = width;
  145. frame->height = height;
  146. void* ptr_cpu[1] = {img.data};
  147. frame->stride[0] = width;
  148. frame->ctx.dev_type = DevContext::DevType::CPU;
  149. frame->fmt = CNDataFormat::CN_PIXEL_FORMAT_BGR24;
  150. frame->CopyToSyncMem(ptr_cpu, false);
  151. data->collection.Add(kCNDataFrameTag, frame);
  152. std::shared_ptr<CNInferObjs> objs_holder = std::make_shared<CNInferObjs>();
  153. auto obj = std::make_shared<CNInferObject>();
  154. obj->id = std::to_string(11);
  155. CNInferBoundingBox bbox = {0.6, 0.4, 0.6, 0.3};
  156. obj->bbox = bbox;
  157. cnstream::CNInferAttr attr;
  158. attr.id = 0;
  159. attr.value = -1;
  160. attr.score = -1;
  161. obj->AddAttribute("classification", attr);
  162. objs_holder->objs_.push_back(obj);
  163. auto obj2 = std::make_shared<CNInferObject>();
  164. obj2->id = std::to_string(12);
  165. bbox = {0.1, -0.2, 0.3, 0.4};
  166. obj2->bbox = bbox;
  167. cnstream::CNInferAttr attr2;
  168. attr2.id = 0;
  169. attr2.value = 2;
  170. attr2.score = 0.6;
  171. obj2->AddAttribute("classification", attr2);
  172. objs_holder->objs_.push_back(obj2);
  173. for (int i = 0; i < 5; ++i) {
  174. auto obj = std::make_shared<CNInferObject>();
  175. obj->id = std::to_string(i);
  176. float val = i * 0.1;
  177. CNInferBoundingBox bbox = {val, val, val, val};
  178. obj->bbox = bbox;
  179. objs_holder->objs_.push_back(obj);
  180. }
  181. data->collection.Add(kCNInferObjsTag, objs_holder);
  182. EXPECT_EQ(osd->Process(data), 0);
  183. }
  184. TEST(Osd, CheckParamSet) {
  185. std::shared_ptr<Module> osd = std::make_shared<Osd>(gname);
  186. ModuleParamSet param;
  187. param.clear();
  188. EXPECT_TRUE(osd->CheckParamSet(param));
  189. std::string label_path = GetExePath() + glabel_path;
  190. param["label_path"] = label_path;
  191. EXPECT_TRUE(osd->CheckParamSet(param));
  192. param["label_path"] = "wrong_path";
  193. EXPECT_FALSE(osd->CheckParamSet(param));
  194. param.clear();
  195. param["secondary_label_path"] = label_path;
  196. EXPECT_TRUE(osd->CheckParamSet(param));
  197. param["secondary_label_path"] = "wrong_path";
  198. EXPECT_FALSE(osd->CheckParamSet(param));
  199. param.clear();
  200. param["label_size"] = "normal";
  201. EXPECT_TRUE(osd->CheckParamSet(param));
  202. param["label_size"] = "large";
  203. EXPECT_TRUE(osd->CheckParamSet(param));
  204. param["label_size"] = "larger";
  205. EXPECT_TRUE(osd->CheckParamSet(param));
  206. param["label_size"] = "small";
  207. EXPECT_TRUE(osd->CheckParamSet(param));
  208. param["label_size"] = "smaller";
  209. EXPECT_TRUE(osd->CheckParamSet(param));
  210. param["label_size"] = "0.9";
  211. EXPECT_TRUE(osd->CheckParamSet(param));
  212. param["label_size"] = "wrong_size";
  213. EXPECT_FALSE(osd->CheckParamSet(param));
  214. param.clear();
  215. param["text_scale"] = "1.2";
  216. param["text_thickness"] = "1.5";
  217. param["box_thickness"] = "1.5";
  218. EXPECT_TRUE(osd->CheckParamSet(param));
  219. param["text_scale"] = "wrong_num";
  220. param["text_thickness"] = "wrong_num";
  221. param["box_thickness"] = "wrong_num";
  222. EXPECT_FALSE(osd->CheckParamSet(param));
  223. param.clear();
  224. param["test_param"] = "test";
  225. EXPECT_TRUE(osd->CheckParamSet(param));
  226. }
  227. } // namespace cnstream