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