1 /**
2 * Copyright 2021-2022 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 #ifdef MSLITE_DEPS_OPENCV
17 #include "tools/converter/preprocess/opencv_utils.h"
18 #include <vector>
19 #include "src/common/log_adapter.h"
20 #include "include/errorcode.h"
21
22 namespace mindspore {
23 namespace lite {
24 namespace preprocess {
ConvertColorConversionCodes(const std::string & format)25 cv::ColorConversionCodes ConvertColorConversionCodes(const std::string &format) {
26 if (format == "RGB") {
27 return cv::COLOR_BGR2RGB;
28 } else if (format == "GRAY") {
29 return cv::COLOR_BGR2GRAY;
30 } else {
31 MS_LOG(ERROR) << "Unsupported format:" << format;
32 return cv::COLOR_COLORCVT_MAX;
33 }
34 }
35
ConvertColorConversionCodes(preprocess::ImageToFormat format)36 cv::ColorConversionCodes ConvertColorConversionCodes(preprocess::ImageToFormat format) {
37 if (format == RGB) {
38 return cv::COLOR_BGR2RGB;
39 } else if (format == GRAY) {
40 return cv::COLOR_BGR2GRAY;
41 } else {
42 MS_LOG(ERROR) << "Unsupported format:" << format;
43 return cv::COLOR_COLORCVT_MAX;
44 }
45 }
46
ConvertResizeMethod(const std::string & method)47 cv::InterpolationFlags ConvertResizeMethod(const std::string &method) {
48 if (method == "NEAREST") {
49 return cv::INTER_NEAREST;
50 } else if (method == "LINEAR") {
51 return cv::INTER_LINEAR;
52 } else if (method == "CUBIC") {
53 return cv::INTER_CUBIC;
54 } else {
55 MS_LOG(ERROR) << "INPUT ILLEGAL: resize_method must be NEAREST|LINEAR|CUBIC.";
56 return cv::INTER_MAX;
57 }
58 }
59
GetMatData(const cv::Mat & mat,void ** data,size_t * size)60 int GetMatData(const cv::Mat &mat, void **data, size_t *size) {
61 if (data == nullptr || size == nullptr) {
62 MS_LOG(ERROR) << "data or size is nullptr.";
63 return RET_NULL_PTR;
64 }
65 cv::Mat mat_local = mat;
66 // if the input Mat's memory is not continuous, copy it to one block of memory
67 if (!mat.isContinuous()) {
68 mat_local = mat.clone();
69 }
70 (*size) = 0;
71 for (int i = 0; i < mat.rows; ++i) {
72 (*size) += static_cast<size_t>(mat.cols) * mat.elemSize();
73 }
74
75 (*data) = new (std::nothrow) char[*size];
76 if (*data == nullptr) {
77 MS_LOG(ERROR) << "new data failed.";
78 return RET_ERROR;
79 }
80 if (memcpy_s(*data, *size, mat_local.data,
81 static_cast<size_t>(mat.rows * mat.cols * mat.channels()) * sizeof(float)) != EOK) {
82 MS_LOG(ERROR) << "memcpy failed.";
83 return RET_ERROR;
84 }
85 return RET_OK;
86 }
87 } // namespace preprocess
88 } // namespace lite
89 } // namespace mindspore
90 #endif
91