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.0 kB

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