1 /**
2 * Copyright 2020-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 <string>
17
18 #include "opencv2/opencv.hpp"
19
20 #include "minddata/dataset/core/cv_tensor.h"
21 #include "minddata/dataset/kernels/image/image_utils.h"
22 #include "minddata/dataset/kernels/image/random_crop_and_resize_op.h"
23 #include "minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.h"
24 #include "minddata/dataset/util/random.h"
25
26 namespace mindspore {
27 namespace dataset {
SoftDvppDecodeRandomCropResizeJpegOp(int32_t target_height,int32_t target_width,float scale_lb,float scale_ub,float aspect_lb,float aspect_ub,int32_t max_attempts)28 SoftDvppDecodeRandomCropResizeJpegOp::SoftDvppDecodeRandomCropResizeJpegOp(int32_t target_height, int32_t target_width,
29 float scale_lb, float scale_ub,
30 float aspect_lb, float aspect_ub,
31 int32_t max_attempts)
32 : target_height_(target_height),
33 target_width_(target_width),
34 scale_lb_(scale_lb),
35 scale_ub_(scale_ub),
36 aspect_lb_(aspect_lb),
37 aspect_ub_(aspect_ub),
38 max_attempts_(max_attempts) {}
39
GetCropInfo(const std::shared_ptr<Tensor> & input,SoftDpCropInfo * crop_info)40 Status SoftDvppDecodeRandomCropResizeJpegOp::GetCropInfo(const std::shared_ptr<Tensor> &input,
41 SoftDpCropInfo *crop_info) {
42 int img_width = 0;
43 int img_height = 0;
44 RETURN_IF_NOT_OK(GetJpegImageInfo(input, &img_width, &img_height));
45 int x = 0;
46 int y = 0;
47 int crop_heigh = 0;
48 int crop_widht = 0;
49 std::unique_ptr<RandomCropAndResizeOp> random_crop_resize(
50 new RandomCropAndResizeOp(target_height_, target_width_, scale_lb_, scale_ub_, aspect_lb_, aspect_ub_,
51 InterpolationMode::kLinear, max_attempts_));
52 RETURN_IF_NOT_OK(random_crop_resize->GetCropBox(img_height, img_width, &x, &y, &crop_heigh, &crop_widht));
53 crop_info->left = x;
54 crop_info->up = y;
55 crop_info->right = crop_info->left + crop_widht - 1;
56 crop_info->down = crop_info->up + crop_heigh - 1;
57 return Status::OK();
58 }
59
Compute(const std::shared_ptr<Tensor> & input,std::shared_ptr<Tensor> * output)60 Status SoftDvppDecodeRandomCropResizeJpegOp::Compute(const std::shared_ptr<Tensor> &input,
61 std::shared_ptr<Tensor> *output) {
62 IO_CHECK(input, output);
63 if (!IsNonEmptyJPEG(input)) {
64 RETURN_STATUS_UNEXPECTED("SoftDvppDecodeRandomCropResizeJpeg: only support processing raw jpeg image.");
65 }
66 SoftDpCropInfo crop_info;
67 RETURN_IF_NOT_OK(GetCropInfo(input, &crop_info));
68 try {
69 auto buffer = const_cast<unsigned char *>(input->GetBuffer());
70 CHECK_FAIL_RETURN_UNEXPECTED(buffer != nullptr,
71 "SoftDvppDecodeRandomCropResizeJpeg: the input image buffer is empty.");
72 SoftDpProcsessInfo info;
73 info.input_buffer = static_cast<uint8_t *>(buffer);
74 info.input_buffer_size = input->SizeInBytes();
75 info.output_width = target_width_;
76 info.output_height = target_height_;
77 cv::Mat out_rgb_img(target_height_, target_width_, CV_8UC3);
78 info.output_buffer = out_rgb_img.data;
79 info.output_buffer_size = target_width_ * target_height_ * 3;
80 info.is_v_before_u = true;
81 int ret = DecodeAndCropAndResizeJpeg(&info, crop_info);
82 std::string error_info("SoftDvppDecodeRandomCropResizeJpeg: failed with return code: ");
83 error_info += std::to_string(ret) + ", please check the log information for more details.";
84 CHECK_FAIL_RETURN_UNEXPECTED(ret == 0, error_info);
85 std::shared_ptr<CVTensor> cv_tensor = nullptr;
86
87 RETURN_IF_NOT_OK(CVTensor::CreateFromMat(out_rgb_img, 3, &cv_tensor));
88 *output = std::static_pointer_cast<Tensor>(cv_tensor);
89 } catch (const cv::Exception &e) {
90 std::string error = "SoftDvppDecodeRandomCropResizeJpeg:" + std::string(e.what());
91 RETURN_STATUS_UNEXPECTED(error);
92 }
93 return Status::OK();
94 }
95 } // namespace dataset
96 } // namespace mindspore
97