You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

bboxop_common.cc 9.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  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. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "bboxop_common.h"
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #include <iostream>
  21. #include <stdio.h>
  22. #include "./tinyxml2.h"
  23. #include "opencv2/opencv.hpp"
  24. #include "common/utils.h"
  25. #include "dataset/core/cv_tensor.h"
  26. #include "dataset/util/path.h"
  27. #include "dataset/core/constants.h"
  28. #include "utils/log_adapter.h"
  29. using namespace mindspore::dataset;
  30. using namespace UT::CVOP::BBOXOP;
  31. using tinyxml2::XMLDocument;
  32. using tinyxml2::XMLElement;
  33. using tinyxml2::XMLError;
  34. const char kAnnotationsFolder[] = "/Annotations/";
  35. const char kImagesFolder[] = "/JPEGImages/";
  36. const char kExpectedName[] = "Expected";
  37. const char kActualName[] = "Actual";
  38. const char kAnnotExt[] = ".xml";
  39. const char kImageExt[] = ".jpg";
  40. BBoxOpCommon::BBoxOpCommon() {}
  41. BBoxOpCommon::~BBoxOpCommon() {}
  42. void BBoxOpCommon::SetUp() {
  43. MS_LOG(INFO) << "starting test.";
  44. image_folder_build_ = "data/dataset/imagefolder/";
  45. image_folder_src_ = "../../../../../tests/ut/data/dataset/imagefolder/";
  46. std::string dir_path = "data/dataset/testVOC2012_2";
  47. GetInputImagesAndAnnotations(dir_path);
  48. }
  49. void BBoxOpCommon::GetInputImagesAndAnnotations(const std::string &dir, std::size_t num_of_samples) {
  50. std::string images_path = dir + std::string(kImagesFolder);
  51. std::string annots_path = dir + std::string(kAnnotationsFolder);
  52. Path dir_path(images_path);
  53. std::shared_ptr<Path::DirIterator> image_dir_itr = Path::DirIterator::OpenDirectory(&dir_path);
  54. std::vector<std::string> paths_to_fetch;
  55. if (!dir_path.Exists()) {
  56. MS_LOG(ERROR) << "Images folder was not found : " + images_path;
  57. EXPECT_TRUE(dir_path.Exists());
  58. }
  59. std::size_t files_fetched = 0;
  60. // get image file paths
  61. while (image_dir_itr->hasNext() && files_fetched < num_of_samples) {
  62. Path image_path = image_dir_itr->next();
  63. if (image_path.Extension() == std::string(kImageExt)) {
  64. paths_to_fetch.push_back(image_path.toString());
  65. files_fetched++;
  66. }
  67. }
  68. // sort fetched files
  69. std::sort(paths_to_fetch.begin(), paths_to_fetch.end());
  70. for (const auto &image_file : paths_to_fetch) {
  71. std::string image_ext = std::string(kImageExt);
  72. std::string annot_file = image_file;
  73. std::size_t pos = 0;
  74. // first replace the Image dir with the Annotation dir.
  75. if ((pos = image_file.find(std::string(kImagesFolder), 0)) != std::string::npos) {
  76. annot_file.replace(pos, std::string(kImagesFolder).length(), std::string(kAnnotationsFolder));
  77. }
  78. // then replace the extensions. the image extension to annotation extension
  79. if ((pos = annot_file.find(image_ext, 0)) != std::string::npos) {
  80. annot_file.replace(pos, std::string(kAnnotExt).length(), std::string(kAnnotExt));
  81. }
  82. std::shared_ptr<Tensor> annotation_tensor;
  83. // load annotations and log failure
  84. if (!LoadAnnotationFile(annot_file, &annotation_tensor)) {
  85. MS_LOG(ERROR) << "Loading Annotations failed in GetInputImagesAndAnnotations";
  86. EXPECT_EQ(0, 1);
  87. }
  88. // load image
  89. GetInputImage(image_file);
  90. // add image and annotation to the tensor table
  91. TensorRow row_data({std::move(input_tensor_), std::move(annotation_tensor)});
  92. images_and_annotations_.push_back(row_data);
  93. }
  94. }
  95. void BBoxOpCommon::SaveImagesWithAnnotations(BBoxOpCommon::FileType type, const std::string &op_name,
  96. const TensorTable &table) {
  97. int i = 0;
  98. for (auto &row : table) {
  99. std::shared_ptr<Tensor> row_to_save;
  100. Status swap_status = SwapRedAndBlue(row[0], &row_to_save);
  101. if (!swap_status.IsOk()) {
  102. MS_LOG(ERROR) << "Swaping red and blue channels failed in SaveImagesWithAnnotations.";
  103. EXPECT_TRUE(swap_status.IsOk());
  104. }
  105. cv::Mat image = std::static_pointer_cast<CVTensor>(row_to_save)->mat();
  106. uint32_t num_of_boxes = row[1]->shape()[0];
  107. bool passing_data_fetch = true;
  108. // For each bounding box draw on the image.
  109. for (uint32_t i = 0; i < num_of_boxes; i++) {
  110. float x = 0.0, y = 0.0, w = 0.0, h = 0.0;
  111. passing_data_fetch &= row[1]->GetItemAt<float>(&x, {i, 0}).IsOk();
  112. passing_data_fetch &= row[1]->GetItemAt<float>(&y, {i, 1}).IsOk();
  113. passing_data_fetch &= row[1]->GetItemAt<float>(&w, {i, 2}).IsOk();
  114. passing_data_fetch &= row[1]->GetItemAt<float>(&h, {i, 3}).IsOk();
  115. if (!passing_data_fetch) {
  116. MS_LOG(ERROR) << "Fetching bbox coordinates failed in SaveImagesWithAnnotations.";
  117. EXPECT_TRUE(passing_data_fetch);
  118. }
  119. cv::Rect bbox(x, y, w, h);
  120. cv::rectangle(image, bbox, cv::Scalar(255, 0, 0), 10, 8, 0);
  121. }
  122. bool im_write_success = false;
  123. // if user wants to save an expected image, use the path to the source folder.
  124. if (type == FileType::kExpected) {
  125. im_write_success = cv::imwrite(
  126. image_folder_src_ + std::string(kExpectedName) + op_name + std::to_string(i) + std::string(kImageExt), image);
  127. } else {
  128. // otherwise if we are saving actual images only for comparison, save in current working dir in build folders.
  129. im_write_success =
  130. cv::imwrite(std::string(kActualName) + op_name + std::to_string(i) + std::string(kImageExt), image);
  131. }
  132. if (!im_write_success) {
  133. MS_LOG(ERROR) << "Image write failed in SaveImagesWithAnnotations.";
  134. EXPECT_TRUE(im_write_success);
  135. }
  136. i += 1;
  137. }
  138. }
  139. void BBoxOpCommon::CompareActualAndExpected(const std::string &op_name) {
  140. size_t num_of_images = images_and_annotations_.size();
  141. for (size_t i = 0; i < num_of_images; i++) {
  142. // load actual and expected images.
  143. std::string actual_path = std::string(kActualName) + op_name + std::to_string(i) + std::string(kImageExt);
  144. std::string expected_path =
  145. image_folder_build_ + std::string(kExpectedName) + op_name + std::to_string(i) + std::string(kImageExt);
  146. cv::Mat expect_img = cv::imread(expected_path, cv::IMREAD_COLOR);
  147. cv::Mat actual_img = cv::imread(actual_path, cv::IMREAD_COLOR);
  148. // after comparison is done remove temporary file
  149. EXPECT_TRUE(remove(actual_path.c_str()) == 0);
  150. // compare using ==operator by Tensor
  151. if (actual_img.data) {
  152. EXPECT_EQ(CVTensor(expect_img) == CVTensor(actual_img), true);
  153. } else {
  154. MS_LOG(ERROR) << "Not pass verification! Image data is null.";
  155. EXPECT_EQ(0, 1);
  156. }
  157. }
  158. }
  159. bool BBoxOpCommon::LoadAnnotationFile(const std::string &path, std::shared_ptr<Tensor> *target_BBox) {
  160. if (!Path(path).Exists()) {
  161. MS_LOG(ERROR) << "File is not found : " + path;
  162. return false;
  163. }
  164. XMLDocument doc;
  165. XMLError e = doc.LoadFile(mindspore::common::SafeCStr(path));
  166. if (e != XMLError::XML_SUCCESS) {
  167. MS_LOG(ERROR) << "Xml load failed";
  168. return false;
  169. }
  170. XMLElement *root = doc.RootElement();
  171. if (root == nullptr) {
  172. MS_LOG(ERROR) << "Xml load root element error";
  173. return false;
  174. }
  175. XMLElement *object = root->FirstChildElement("object");
  176. if (object == nullptr) {
  177. MS_LOG(ERROR) << "No object find in " + path;
  178. return false;
  179. }
  180. std::vector<float> return_value_list;
  181. dsize_t bbox_count = 0; // keep track of number of bboxes in file
  182. dsize_t bbox_val_count = 4; // creating bboxes of size 4 to test function
  183. // FILE OK TO READ
  184. while (object != nullptr) {
  185. bbox_count += 1;
  186. std::string label_name;
  187. float xmin = 0.0, ymin = 0.0, xmax = 0.0, ymax = 0.0;
  188. XMLElement *bbox_node = object->FirstChildElement("bndbox");
  189. if (bbox_node != nullptr) {
  190. XMLElement *xmin_node = bbox_node->FirstChildElement("xmin");
  191. if (xmin_node != nullptr) xmin = xmin_node->FloatText();
  192. XMLElement *ymin_node = bbox_node->FirstChildElement("ymin");
  193. if (ymin_node != nullptr) ymin = ymin_node->FloatText();
  194. XMLElement *xmax_node = bbox_node->FirstChildElement("xmax");
  195. if (xmax_node != nullptr) xmax = xmax_node->FloatText();
  196. XMLElement *ymax_node = bbox_node->FirstChildElement("ymax");
  197. if (ymax_node != nullptr) ymax = ymax_node->FloatText();
  198. } else {
  199. MS_LOG(ERROR) << "bndbox dismatch in " + path;
  200. return false;
  201. }
  202. if (xmin > 0 && ymin > 0 && xmax > xmin && ymax > ymin) {
  203. for (auto item : {xmin, ymin, xmax - xmin, ymax - ymin}) {
  204. return_value_list.push_back(item);
  205. }
  206. }
  207. object = object->NextSiblingElement("object"); // Read next BBox if exists
  208. }
  209. std::shared_ptr<Tensor> ret_value;
  210. Status s = Tensor::CreateTensor(&ret_value, return_value_list, TensorShape({bbox_count, bbox_val_count}));
  211. EXPECT_TRUE(s.IsOk());
  212. (*target_BBox) = ret_value; // load bbox from file into return
  213. return true;
  214. }