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