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 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "common/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::CreateTensor(&raw_input_tensor_, filename);
  48. raw_cv_image_ = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
  49. input_tensor_ = std::dynamic_pointer_cast<Tensor>(std::make_shared<CVTensor>(raw_cv_image_));
  50. SwapRedAndBlue(input_tensor_, &input_tensor_);
  51. if (raw_cv_image_.data) {
  52. MS_LOG(INFO) << "Reading was successful. Height:" << raw_cv_image_.rows << " Width: " << raw_cv_image_.cols
  53. << " Channels:" << raw_cv_image_.channels() << ".";
  54. }
  55. } catch (...) {
  56. MS_LOG(INFO) << "Error in GetInputImage.";
  57. }
  58. }
  59. void CVOpCommon::Save(const std::shared_ptr<Tensor> &tensor, std::string filename) {
  60. std::shared_ptr<Tensor> output;
  61. SwapRedAndBlue(tensor, &output);
  62. cv::Mat output_image_CV = std::dynamic_pointer_cast<CVTensor>(output)->mat();
  63. cv::imwrite(filename, output_image_CV);
  64. }
  65. std::string CVOpCommon::GetJPGStr(const cv::Mat &image) {
  66. std::vector<unsigned char> buff_jpg;
  67. cv::imencode(".jpg", image, buff_jpg);
  68. return std::string(buff_jpg.begin(), buff_jpg.end());
  69. }
  70. bool CVOpCommon::CompareCVMat(const cv::Mat &actual, const cv::Mat &expect, OperatorType type) {
  71. if (actual.size() != expect.size() || actual.channels() != expect.channels() || actual.type() != expect.type()) {
  72. return false;
  73. }
  74. std::string strJPGData_actual = GetJPGStr(actual);
  75. std::string strJPGData_expect = GetJPGStr(expect);
  76. bool success = strJPGData_actual.compare(strJPGData_expect) == 0;
  77. if (type == kFlipHorizontal || type == kFlipVertical) {
  78. std::string raw_filename = filename_;
  79. raw_filename.replace(raw_filename.end() - 9, raw_filename.end(), "imagefolder/apple_expect_not_flip.jpg");
  80. cv::Mat raw;
  81. raw = cv::imread(raw_filename, cv::ImreadModes::IMREAD_COLOR);
  82. std::string strJPGData_raw = GetJPGStr(raw);
  83. success = success || strJPGData_actual.compare(strJPGData_raw) == 0;
  84. } else if (type == kCrop) {
  85. success = expect.rows == actual.rows && expect.cols == actual.cols && expect.channels(), actual.channels();
  86. }
  87. return success;
  88. }
  89. void CVOpCommon::CheckImageShapeAndData(const std::shared_ptr<Tensor> &output_tensor, OperatorType type) {
  90. std::string dir_path = filename_.substr(0, filename_.length() - 9);
  91. std::string expect_image_path, actual_image_path;
  92. switch (type) {
  93. case kRescale:
  94. expect_image_path = dir_path + "imagefolder/apple_expect_rescaled.jpg";
  95. actual_image_path = dir_path + "imagefolder/apple_actual_rescaled.jpg";
  96. break;
  97. case kResizeBilinear:
  98. expect_image_path = dir_path + "imagefolder/apple_expect_resize_bilinear.jpg";
  99. actual_image_path = dir_path + "imagefolder/apple_actual_resize_bilinear.jpg";
  100. break;
  101. case kFlipHorizontal:
  102. expect_image_path = dir_path + "imagefolder/apple_expect_flipped_horizontal.jpg";
  103. actual_image_path = dir_path + "imagefolder/apple_actual_flipped_horizontal.jpg";
  104. break;
  105. case kFlipVertical:
  106. expect_image_path = dir_path + "imagefolder/apple_expect_flipped_vertical.jpg";
  107. actual_image_path = dir_path + "imagefolder/apple_actual_flipped_vertical.jpg";
  108. break;
  109. case kDecode:
  110. expect_image_path = dir_path + "imagefolder/apple_expect_decoded.jpg";
  111. actual_image_path = dir_path + "imagefolder/apple_actual_decoded.jpg";
  112. break;
  113. case kChangeMode:
  114. expect_image_path = dir_path + "imagefolder/apple_expect_changemode.jpg";
  115. actual_image_path = dir_path + "imagefolder/apple_actual_changemode.jpg";
  116. break;
  117. default:
  118. MS_LOG(INFO) << "Not pass verification! Operation type does not exists.";
  119. EXPECT_EQ(0, 1);
  120. }
  121. cv::Mat expect_img = cv::imread(expect_image_path, cv::IMREAD_COLOR);
  122. cv::Mat actual_img;
  123. // Saving
  124. MS_LOG(INFO) << "output_tensor.shape is " << output_tensor->shape();
  125. Save(output_tensor, actual_image_path);
  126. actual_img = cv::imread(actual_image_path, cv::IMREAD_COLOR);
  127. if (actual_img.data) {
  128. EXPECT_EQ(CompareCVMat(actual_img, expect_img, type), true);
  129. } else {
  130. MS_LOG(INFO) << "Not pass verification! Image data is null.";
  131. EXPECT_EQ(0, 1);
  132. }
  133. }