2 // Copyright (c) 2018 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "image_decoder.hpp"
20 #include "details/ie_exception.hpp"
25 #include <opencv2/core/core.hpp>
26 #include <opencv2/highgui/highgui.hpp>
27 #include <opencv2/imgproc/imgproc.hpp>
31 int getLoadModeForChannels(int channels, int base) {
34 return base | CV_LOAD_IMAGE_GRAYSCALE;
36 return base | CV_LOAD_IMAGE_COLOR;
38 return base | CV_LOAD_IMAGE_UNCHANGED;
42 cv::Size addToBlob(std::string name, int batch_pos, Blob& blob, PreprocessingOptions preprocessingOptions) {
43 SizeVector blobSize = blob.dims();
44 int width = static_cast<int>(blobSize[0]);
45 int height = static_cast<int>(blobSize[1]);
46 int channels = static_cast<int>(blobSize[2]);
47 T* blob_data = static_cast<T*>(blob.buffer());
48 Mat orig_image, result_image;
49 int loadMode = getLoadModeForChannels(channels, 0);
51 std::string tryName = name;
53 // TODO This is a dirty hack to support VOC2007 (where no file extension is put into annotation).
55 if (name.find('.') == -1) tryName = name + ".JPEG";
57 orig_image = imread(tryName, loadMode);
59 if (orig_image.empty()) {
60 THROW_IE_EXCEPTION << "Cannot open image file: " << tryName;
63 // Preprocessing the image
64 Size res = orig_image.size();
66 if (preprocessingOptions.resizeCropPolicy == ResizeCropPolicy::Resize) {
67 cv::resize(orig_image, result_image, Size(width, height));
68 } else if (preprocessingOptions.resizeCropPolicy == ResizeCropPolicy::ResizeThenCrop) {
71 cv::resize(orig_image, resized_image, Size(preprocessingOptions.resizeBeforeCropX, preprocessingOptions.resizeBeforeCropY));
73 size_t cx = preprocessingOptions.resizeBeforeCropX / 2;
74 size_t cy = preprocessingOptions.resizeBeforeCropY / 2;
76 cv::Rect cropRect(cx - width / 2, cy - height / 2, width, height);
77 result_image = resized_image(cropRect);
78 } else if (preprocessingOptions.resizeCropPolicy == ResizeCropPolicy::DoNothing) {
79 // No image preprocessing to be done here
80 result_image = orig_image;
82 THROW_IE_EXCEPTION << "Unsupported ResizeCropPolicy value";
85 float scaleFactor = preprocessingOptions.scaleValuesTo01 ? 255.0 : 1.0;
87 for (int c = 0; c < channels; c++) {
88 for (int h = 0; h < height; h++) {
89 for (int w = 0; w < width; w++) {
90 blob_data[batch_pos * channels * width * height + c * width * height + h * width + w] =
91 static_cast<T>(result_image.at<cv::Vec3b>(h, w)[c] / scaleFactor);
99 std::map<std::string, cv::Size> convertToBlob(std::vector<std::string> names, int batch_pos, Blob& blob, PreprocessingOptions preprocessingOptions) {
100 if (blob.buffer() == nullptr) {
101 THROW_IE_EXCEPTION << "Blob was not allocated";
104 std::function<cv::Size(std::string, int, Blob&, PreprocessingOptions)> add_func;
106 switch (blob.precision()) {
107 case Precision::FP32:
108 add_func = &addToBlob<float>;
110 case Precision::FP16:
114 add_func = &addToBlob<short>;
117 add_func = &addToBlob<uint8_t>;
120 std::map<std::string, Size> res;
121 for (int b = 0; b < names.size(); b++) {
122 std::string name = names[b];
123 Size orig_size = add_func(name, batch_pos + b, blob, preprocessingOptions);
124 res.insert(std::pair<std::string, Size>(name, orig_size));
130 Size ImageDecoder::loadToBlob(std::string name, Blob& blob, PreprocessingOptions preprocessingOptions) {
131 std::vector<std::string> names = { name };
132 return loadToBlob(names, blob, preprocessingOptions).at(name);
135 std::map<std::string, cv::Size> ImageDecoder::loadToBlob(std::vector<std::string> names, Blob& blob, PreprocessingOptions preprocessingOptions) {
136 return convertToBlob(names, 0, blob, preprocessingOptions);
139 Size ImageDecoder::insertIntoBlob(std::string name, int batch_pos, Blob& blob, PreprocessingOptions preprocessingOptions) {
140 return convertToBlob({ name }, batch_pos, blob, preprocessingOptions).at(name);