preprocessor_detection.cpp 5.1 KB
Newer Older
1 2 3 4 5 6
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8 9 10 11 12 13 14
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
#include <glog/logging.h>
16 17 18 19 20 21 22
#include <thread>
#include <mutex>

#include "preprocessor_detection.h"
#include "utils/utils.h"

namespace PaddleSolution {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
bool DetectionPreProcessor::single_process(const std::string& fname,
                                           std::vector<float> &vec_data,
                                           int* ori_w, int* ori_h,
                                           int* resize_w, int* resize_h,
                                           float* scale_ratio) {
    cv::Mat im1 = cv::imread(fname, -1);
    cv::Mat im;
    if (_config->_feeds_size == 3) {  // faster rcnn
        im1.convertTo(im, CV_32FC3, 1/255.0);
    } else if (_config->_feeds_size == 2) {  // yolo v3
        im = im1;
    }
    if (im.data == nullptr || im.empty()) {
    #ifdef _WIN32
        std::cerr << "Failed to open image: " << fname << std::endl;
    #else
        LOG(ERROR) << "Failed to open image: " << fname;
    #endif
        return false;
    }
    int channels = im.channels();
    if (channels == 1) {
        cv::cvtColor(im, im, cv::COLOR_GRAY2BGR);
    }
    channels = im.channels();
    if (channels != 3 && channels != 4) {
    #ifdef _WIN32
        std::cerr << "Only support rgb(gray) and rgba image." << std::endl;
    #else
        LOG(ERROR) << "Only support rgb(gray) and rgba image.";
    #endif 
        return false;
    }
    *ori_w = im.cols;
    *ori_h = im.rows;
    cv::cvtColor(im, im, cv::COLOR_BGR2RGB);
    // channels = im.channels();
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    // resize
    int rw = im.cols;
    int rh = im.rows;
    float im_scale_ratio;
    utils::scaling(_config->_resize_type, rw, rh, _config->_resize[0],
                   _config->_resize[1], _config->_target_short_size,
                   _config->_resize_max_size, im_scale_ratio);
    cv::Size resize_size(rw, rh);
    *resize_w = rw;
    *resize_h = rh;
    *scale_ratio = im_scale_ratio;
    if (*ori_h != rh || *ori_w != rw) {
        cv::Mat im_temp;
        if (_config->_resize_type == utils::SCALE_TYPE::UNPADDING) {
            cv::resize(im, im_temp, resize_size, 0, 0, cv::INTER_LINEAR);
        } else if (_config->_resize_type == utils::SCALE_TYPE::RANGE_SCALING) {
                cv::resize(im, im_temp, cv::Size(), im_scale_ratio,
                           im_scale_ratio, cv::INTER_LINEAR);
79
        }
80 81
        im = im_temp;
    }
82

83 84
    vec_data.resize(channels * rw * rh);
    float *data = vec_data.data();
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99
    float* pmean = _config->_mean.data();
    float* pscale = _config->_std.data();
    for (int h = 0; h < rh; ++h) {
        const uchar* uptr = im.ptr<uchar>(h);
        const float* fptr = im.ptr<float>(h);
        int im_index = 0;
        for (int w = 0; w < rw; ++w) {
            for (int c = 0; c < channels; ++c) {
                int top_index = (c * rh + h) * rw + w;
                float pixel;
                if (_config->_feeds_size == 2) {  // yolo v3
                    pixel = static_cast<float>(uptr[im_index++]) / 255.0;
                } else if (_config->_feeds_size == 3) {
                    pixel = fptr[im_index++];
100
                }
101 102
                pixel = (pixel - pmean[c]) / pscale[c];
                data[top_index] = pixel;
103 104 105
            }
        }
    }
106 107
    return true;
}
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
bool DetectionPreProcessor::batch_process(const std::vector<std::string>& imgs,
                                          std::vector<std::vector<float>> &data,
                                          int* ori_w, int* ori_h, int* resize_w,
                                          int* resize_h, float* scale_ratio) {
    auto ic = _config->_channels;
    auto iw = _config->_resize[0];
    auto ih = _config->_resize[1];
    std::vector<std::thread> threads;
    for (int i = 0; i < imgs.size(); ++i) {
        std::string path = imgs[i];
        int* width = &ori_w[i];
        int* height = &ori_h[i];
        int* resize_width = &resize_w[i];
        int* resize_height = &resize_h[i];
        float* sr = &scale_ratio[i];
        threads.emplace_back([this, &data, i, path, width, height,
                              resize_width, resize_height, sr] {
            std::vector<float> buffer;
            single_process(path, buffer, width, height, resize_width,
                           resize_height, sr);
            data[i] = buffer;
        });
131
    }
132 133 134 135
    for (auto& t : threads) {
        if (t.joinable()) {
            t.join();
        }
136
    }
137 138
    return true;
}
139

140 141 142
bool DetectionPreProcessor::init(std::shared_ptr<PaddleSolution::PaddleModelConfigPaser> config) {
    _config = config;
    return true;
143
}
144
}  //  namespace PaddleSolution