// 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. #pragma once #include "paddle_api.h" // NOLINT #include "json/json.h" #include #include #include #include #include #include #include #include #include #include using namespace paddle::lite_api; // NOLINT using namespace std; namespace PPShiTu { struct RESULT { std::string class_name; int class_id; float score; }; class FeatureExtract { public: explicit FeatureExtract(const Json::Value &config_file) { MobileConfig config; if (config_file["Global"]["rec_model_path"].as().empty()) { std::cout << "Please set [rec_model_path] in config file" << std::endl; exit(-1); } config.set_model_from_file( config_file["Global"]["rec_model_path"].as()); this->predictor = CreatePaddlePredictor(config); SetPreProcessParam(config_file["RecPreProcess"]["transform_ops"]); printf("feature extract model create!\n"); } void SetPreProcessParam(const Json::Value &config_file) { for (const auto &item : config_file) { auto op_name = item["type"].as(); if (op_name == "ResizeImage") { this->size = item["size"].as(); } else if (op_name == "NormalizeImage") { this->mean.clear(); this->std.clear(); for (auto tmp : item["mean"]) { this->mean.emplace_back(tmp.as()); } for (auto tmp : item["std"]) { this->std.emplace_back(tmp.as()); } this->scale = item["scale"].as(); } } } void RunRecModel(const cv::Mat &img, double &cost_time, std::vector &feature); // void PostProcess(std::vector &feature); void FeatureNorm(std::vector &featuer); private: std::shared_ptr predictor; // std::vector label_list; std::vector mean = {0.485f, 0.456f, 0.406f}; std::vector std = {0.229f, 0.224f, 0.225f}; double scale = 0.00392157; int size = 224; // pre-process Resize resize_op_; NormalizeImage normalize_op_; Permute permute_op_; }; } // namespace PPShiTu