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.

testlitecv.cpp 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include "include/api/types.h"
  29. #include "include/dataset/lite_cv/lite_mat.h"
  30. #include "include/dataset/lite_cv/image_process.h"
  31. #include "include/dataset/vision_lite.h"
  32. #include "include/dataset/execute.h"
  33. using mindspore::dataset::Execute;
  34. using mindspore::dataset::LDataType;
  35. using mindspore::dataset::LiteMat;
  36. using mindspore::dataset::PaddBorderType;
  37. using mindspore::dataset::vision::Decode;
  38. int main(int argc, char **argv) {
  39. std::ifstream ifs("test_image.jpg");
  40. if (!ifs.is_open() || !ifs.good()) {
  41. std::cout << "fail to load image, check image path" << std::endl;
  42. return -1;
  43. }
  44. ifs.seekg(0, std::ios::end);
  45. size_t size = ifs.tellg();
  46. mindspore::MSTensor image("file", mindspore::DataType::kNumberTypeUInt8, {static_cast<int64_t>(size)}, nullptr, size);
  47. ifs.seekg(0, std::ios::beg);
  48. ifs.read(reinterpret_cast<char *>(image.MutableData()), size);
  49. ifs.close();
  50. auto decode = Decode();
  51. auto executor = Execute(decode);
  52. executor(image, &image);
  53. constexpr int32_t image_h = 0;
  54. constexpr int32_t image_w = 1;
  55. constexpr int32_t image_c = 2;
  56. LiteMat lite_mat_rgb(image.Shape()[image_w], image.Shape()[image_h], image.Shape()[image_c],
  57. const_cast<void *>(image.Data().get()), LDataType::UINT8);
  58. std::cout << "lite_mat_rgb: height=" << lite_mat_rgb.height_ << ", width=" << lite_mat_rgb.width_ << std::endl;
  59. LiteMat lite_mat_resize;
  60. constexpr target_size = 256;
  61. ResizeBilinear(lite_mat_rgb, lite_mat_resize, target_size, target_size);
  62. std::cout << "lite_mat_resize: height=" << lite_mat_resize.height_ << ", width=" << lite_mat_resize.width_
  63. << std::endl;
  64. LiteMat lite_mat_pad;
  65. constexpr int32_t pad_top = 30;
  66. constexpr int32_t pad_bottom = 30;
  67. constexpr int32_t pad_left = 10;
  68. constexpr int32_t pad_right = 10;
  69. constexpr int32_t pad_color = 255;
  70. Pad(lite_mat_resize, lite_mat_pad, pad_top, pad_bottom, pad_left, pad_right, PaddBorderType::PADD_BORDER_CONSTANT,
  71. pad_color, pad_color, pad_color);
  72. std::cout << "lite_mat_pad: height=" << lite_mat_pad.height_ << ", width=" << lite_mat_pad.width_ << std::endl;
  73. }