// Copyright (c) 2022 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 // // http://www.apache.org/licenses/LICENSE-2.0 // // 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. #include "fastdeploy/vision/common/processors/transform.h" #include "fastdeploy/vision/ppdet/ppyoloe.h" #include "yaml-cpp/yaml.h" namespace fastdeploy { namespace vision { bool BuildPreprocessPipelineFromConfig( std::vector>* processors, const std::string& config_file) { processors->clear(); YAML::Node cfg; try { cfg = YAML::LoadFile(config_file); } catch (YAML::BadFile& e) { FDERROR << "Failed to load yaml file " << config_file << ", maybe you should check this file." << std::endl; return false; } processors->push_back(std::make_shared()); for (const auto& op : cfg["Preprocess"]) { std::string op_name = op["type"].as(); if (op_name == "NormalizeImage") { auto mean = op["mean"].as>(); auto std = op["std"].as>(); bool is_scale = op["is_scale"].as(); processors->push_back(std::make_shared(mean, std, is_scale)); } else if (op_name == "Resize") { bool keep_ratio = op["keep_ratio"].as(); auto target_size = op["target_size"].as>(); int interp = op["interp"].as(); FDASSERT(target_size.size(), "Require size of target_size be 2, but now it's " + std::to_string(target_size.size()) + "."); if (!keep_ratio) { int width = target_size[1]; int height = target_size[0]; processors->push_back( std::make_shared(width, height, -1.0, -1.0, interp, false)); } else { int min_target_size = std::min(target_size[0], target_size[1]); int max_target_size = std::max(target_size[0], target_size[1]); processors->push_back(std::make_shared( min_target_size, interp, true, max_target_size)); } } else if (op_name == "Permute") { // Do nothing, do permute as the last operation continue; } else if (op_name == "Pad") { auto size = op["size"].as>(); auto value = op["fill_value"].as>(); processors->push_back(std::make_shared("float")); processors->push_back( std::make_shared(size[1], size[0], value)); } else if (op_name == "PadStride") { auto stride = op["stride"].as(); processors->push_back( std::make_shared(stride, std::vector(3, 0))); } else { FDERROR << "Unexcepted preprocess operator: " << op_name << "." << std::endl; return false; } } processors->push_back(std::make_shared()); return true; } } // namespace vision } // namespace fastdeploy