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.

cvop_common.cc 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright 2019 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 <memory>
  17. #include <string>
  18. #include <vector>
  19. #include "cvop_common.h"
  20. #include "minddata/dataset/core/constants.h"
  21. #include "utils/ms_utils.h"
  22. #include "minddata/dataset/core/cv_tensor.h"
  23. #include "utils/log_adapter.h"
  24. #include <fstream>
  25. #include <opencv2/opencv.hpp>
  26. namespace common = mindspore::common;
  27. using namespace mindspore::dataset;
  28. using mindspore::LogStream;
  29. using mindspore::ExceptionType::NoExceptionType;
  30. using mindspore::MsLogLevel::INFO;
  31. using UT::CVOP::CVOpCommon;
  32. CVOpCommon::CVOpCommon() {}
  33. CVOpCommon::~CVOpCommon() {}
  34. void CVOpCommon::SetUp() {
  35. MS_LOG(INFO) << "starting test.";
  36. filename_ = GetFilename();
  37. GetInputImage(filename_);
  38. }
  39. std::string CVOpCommon::GetFilename() {
  40. std::string de_home = "data/dataset";
  41. std::string filename = de_home + "/apple.jpg";
  42. MS_LOG(INFO) << "Reading " << common::SafeCStr(filename) << ".";
  43. return filename;
  44. }
  45. void CVOpCommon::GetInputImage(std::string filename) {
  46. try {
  47. Tensor::CreateFromFile(filename, &raw_input_tensor_);
  48. raw_cv_image_ = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  49. std::shared_ptr<CVTensor> input_cv_tensor;
  50. CVTensor::CreateFromMat(raw_cv_image_, &input_cv_tensor);
  51. input_tensor_ = std::dynamic_pointer_cast<Tensor>(input_cv_tensor);
  52. SwapRedAndBlue(input_tensor_, &input_tensor_);
  53. if (raw_cv_image_.data) {
  54. MS_LOG(INFO) << "Reading was successful. Height:" << raw_cv_image_.rows << " Width: " << raw_cv_image_.cols
  55. << " Channels:" << raw_cv_image_.channels() << ".";
  56. }
  57. } catch (...) {
  58. MS_LOG(INFO) << "Error in GetInputImage.";
  59. }
  60. }
  61. void CVOpCommon::Save(const std::shared_ptr<Tensor> &tensor, std::string filename) {
  62. std::shared_ptr<Tensor> output;
  63. SwapRedAndBlue(tensor, &output);
  64. cv::Mat output_image_CV = std::dynamic_pointer_cast<CVTensor>(output)->mat();
  65. cv::imwrite(filename, output_image_CV);
  66. }
  67. std::string CVOpCommon::GetJPGStr(const cv::Mat &image) {
  68. std::vector<unsigned char> buff_jpg;
  69. cv::imencode(".jpg", image, buff_jpg);
  70. return std::string(buff_jpg.begin(), buff_jpg.end());
  71. }
  72. bool CVOpCommon::CompareCVMat(const cv::Mat &actual, const cv::Mat &expect, OperatorType type) {
  73. if (actual.size() != expect.size() || actual.channels() != expect.channels() || actual.type() != expect.type()) {
  74. return false;
  75. }
  76. std::string strJPGData_actual = GetJPGStr(actual);
  77. std::string strJPGData_expect = GetJPGStr(expect);
  78. bool success = strJPGData_actual.compare(strJPGData_expect) == 0;
  79. if (type == kFlipHorizontal || type == kFlipVertical) {
  80. std::string raw_filename = filename_;
  81. raw_filename.replace(raw_filename.end() - 9, raw_filename.end(), "imagefolder/apple_expect_not_flip.jpg");
  82. cv::Mat raw;
  83. raw = cv::imread(raw_filename, cv::ImreadModes::IMREAD_COLOR);
  84. std::string strJPGData_raw = GetJPGStr(raw);
  85. success = success || strJPGData_actual.compare(strJPGData_raw) == 0;
  86. } else if (type == kCrop) {
  87. success = expect.rows == actual.rows && expect.cols == actual.cols && expect.channels(), actual.channels();
  88. }
  89. return success;
  90. }
  91. void CVOpCommon::CheckImageShapeAndData(const std::shared_ptr<Tensor> &output_tensor, OperatorType type) {
  92. std::string dir_path = filename_.substr(0, filename_.length() - 9);
  93. std::string expect_image_path, actual_image_path;
  94. switch (type) {
  95. case kRescale:
  96. expect_image_path = dir_path + "imagefolder/apple_expect_rescaled.jpg";
  97. actual_image_path = dir_path + "imagefolder/apple_actual_rescaled.jpg";
  98. break;
  99. case kResizeBilinear:
  100. expect_image_path = dir_path + "imagefolder/apple_expect_resize_bilinear.jpg";
  101. actual_image_path = dir_path + "imagefolder/apple_actual_resize_bilinear.jpg";
  102. break;
  103. case kFlipHorizontal:
  104. expect_image_path = dir_path + "imagefolder/apple_expect_flipped_horizontal.jpg";
  105. actual_image_path = dir_path + "imagefolder/apple_actual_flipped_horizontal.jpg";
  106. break;
  107. case kFlipVertical:
  108. expect_image_path = dir_path + "imagefolder/apple_expect_flipped_vertical.jpg";
  109. actual_image_path = dir_path + "imagefolder/apple_actual_flipped_vertical.jpg";
  110. break;
  111. case kDecode:
  112. expect_image_path = dir_path + "imagefolder/apple_expect_decoded.jpg";
  113. actual_image_path = dir_path + "imagefolder/apple_actual_decoded.jpg";
  114. break;
  115. case kChangeMode:
  116. expect_image_path = dir_path + "imagefolder/apple_expect_changemode.jpg";
  117. actual_image_path = dir_path + "imagefolder/apple_actual_changemode.jpg";
  118. break;
  119. case kRandomAffine:
  120. expect_image_path = dir_path + "imagefolder/apple_expect_randomaffine.jpg";
  121. actual_image_path = dir_path + "imagefolder/apple_actual_randomaffine.jpg";
  122. break;
  123. case kAutoContrast:
  124. expect_image_path = dir_path + "imagefolder/apple_expect_autocontrast.jpg";
  125. actual_image_path = dir_path + "imagefolder/apple_actual_autocontrast.jpg";
  126. break;
  127. case kEqualize:
  128. expect_image_path = dir_path + "imagefolder/apple_expect_equalize.jpg";
  129. actual_image_path = dir_path + "imagefolder/apple_actual_equalize.jpg";
  130. break;
  131. default:
  132. MS_LOG(INFO) << "Not pass verification! Operation type does not exists.";
  133. EXPECT_EQ(0, 1);
  134. }
  135. cv::Mat expect_img = cv::imread(expect_image_path, cv::IMREAD_COLOR);
  136. cv::Mat actual_img;
  137. // Saving
  138. MS_LOG(INFO) << "output_tensor.shape is " << output_tensor->shape();
  139. Save(output_tensor, actual_image_path);
  140. actual_img = cv::imread(actual_image_path, cv::IMREAD_COLOR);
  141. if (actual_img.data) {
  142. EXPECT_EQ(CompareCVMat(actual_img, expect_img, type), true);
  143. } else {
  144. MS_LOG(INFO) << "Not pass verification! Image data is null.";
  145. EXPECT_EQ(0, 1);
  146. }
  147. }