1 /**
2 * Copyright 2021 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 <sys/stat.h>
17 #include <unistd.h>
18 #include <fstream>
19 #include <iostream>
20 #include <map>
21 #include <memory>
22 #include <set>
23 #include <string>
24 #include <unordered_map>
25 #include <unordered_set>
26 #include <utility>
27 #include <vector>
28
29 #include "include/api/types.h"
30 #include "include/dataset/lite_cv/lite_mat.h"
31 #include "include/dataset/lite_cv/image_process.h"
32 #include "include/dataset/vision_lite.h"
33 #include "include/dataset/execute.h"
34
35 using mindspore::dataset::Execute;
36 using mindspore::dataset::LDataType;
37 using mindspore::dataset::LiteMat;
38 using mindspore::dataset::PaddBorderType;
39 using mindspore::dataset::vision::Decode;
40
main(int argc,char ** argv)41 int main(int argc, char **argv) {
42 std::ifstream ifs("../../../../tests/ut/data/dataset/apple.jpg");
43
44 if (!ifs.is_open() || !ifs.good()) {
45 std::cout << "fail to load image, check image path" << std::endl;
46 return -1;
47 }
48
49 ifs.seekg(0, std::ios::end);
50 size_t size = ifs.tellg();
51 mindspore::MSTensor image("file", mindspore::DataType::kNumberTypeUInt8, {static_cast<int64_t>(size)}, nullptr, size);
52
53 ifs.seekg(0, std::ios::beg);
54 ifs.read(reinterpret_cast<char *>(image.MutableData()), size);
55 ifs.close();
56
57 auto decode = Decode();
58 auto executor = Execute(decode);
59 executor(image, &image);
60
61 constexpr int32_t image_h = 0;
62 constexpr int32_t image_w = 1;
63 constexpr int32_t image_c = 2;
64 LiteMat lite_mat_rgb(image.Shape()[image_w], image.Shape()[image_h], image.Shape()[image_c],
65 const_cast<void *>(image.Data().get()), LDataType::UINT8);
66 std::cout << "lite_mat_rgb: height=" << lite_mat_rgb.height_ << ", width=" << lite_mat_rgb.width_ << std::endl;
67
68 LiteMat lite_mat_resize;
69 constexpr int32_t target_size = 256;
70 ResizeBilinear(lite_mat_rgb, lite_mat_resize, target_size, target_size);
71 std::cout << "lite_mat_resize: height=" << lite_mat_resize.height_ << ", width=" << lite_mat_resize.width_
72 << std::endl;
73
74 LiteMat lite_mat_pad;
75 constexpr int32_t pad_top = 30;
76 constexpr int32_t pad_bottom = 30;
77 constexpr int32_t pad_left = 10;
78 constexpr int32_t pad_right = 10;
79 constexpr int32_t pad_color = 255;
80 Pad(lite_mat_resize, lite_mat_pad, pad_top, pad_bottom, pad_left, pad_right, PaddBorderType::PADD_BORDER_CONSTANT,
81 pad_color, pad_color, pad_color);
82 std::cout << "lite_mat_pad: height=" << lite_mat_pad.height_ << ", width=" << lite_mat_pad.width_ << std::endl;
83 }
84