|
| 1 | +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "fastdeploy/vision.h" |
| 16 | +#ifdef WIN32 |
| 17 | +const char sep = '\\'; |
| 18 | +#else |
| 19 | +const char sep = '/'; |
| 20 | +#endif |
| 21 | + |
| 22 | +void InitAndInfer(const std::string& model_dir, const std::string& image_file, |
| 23 | + const fastdeploy::RuntimeOption& option) { |
| 24 | + auto model_file = model_dir + sep + "smoke.pdmodel"; |
| 25 | + auto params_file = model_dir + sep + "smoke.pdiparams"; |
| 26 | + auto config_file = model_dir + sep + "infer_cfg.yml"; |
| 27 | + fastdeploy::vision::EnableFlyCV(); |
| 28 | + auto model = fastdeploy::vision::perception::Smoke( |
| 29 | + model_file, params_file, config_file, option, |
| 30 | + fastdeploy::ModelFormat::PADDLE); |
| 31 | + assert(model.Initialized()); |
| 32 | + |
| 33 | + auto im = cv::imread(image_file); |
| 34 | + |
| 35 | + fastdeploy::vision::PerceptionResult res; |
| 36 | + if (!model.Predict(im, &res)) { |
| 37 | + std::cerr << "Failed to predict." << std::endl; |
| 38 | + return; |
| 39 | + } |
| 40 | + std::cout << res.Str() << std::endl; |
| 41 | + |
| 42 | + auto vis_im = fastdeploy::vision::VisPerception(im, res, config_file); |
| 43 | + cv::imwrite("vis_result.jpg", vis_im); |
| 44 | + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; |
| 45 | +} |
| 46 | + |
| 47 | +int main(int argc, char* argv[]) { |
| 48 | + if (argc < 4) { |
| 49 | + std::cout << "Usage: infer_demo path/to/paddle_model" |
| 50 | + "path/to/image " |
| 51 | + "run_option, " |
| 52 | + "e.g ./infer_demo ./smoke ./00000.png 0" |
| 53 | + << std::endl; |
| 54 | + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " |
| 55 | + "with gpu; 2: run with paddle-trt" |
| 56 | + << std::endl; |
| 57 | + return -1; |
| 58 | + } |
| 59 | + |
| 60 | + fastdeploy::RuntimeOption option; |
| 61 | + if (std::atoi(argv[3]) == 0) { |
| 62 | + option.UseCpu(); |
| 63 | + } else if (std::atoi(argv[3]) == 1) { |
| 64 | + option.UseGpu(); |
| 65 | + } else if (std::atoi(argv[3]) == 2) { |
| 66 | + option.UseGpu(); |
| 67 | + option.UseTrtBackend(); |
| 68 | + option.EnablePaddleToTrt(); |
| 69 | + option.SetTrtInputShape("images", {1, 3, 384, 1280}); |
| 70 | + option.SetTrtInputShape("down_ratios", {1, 2}); |
| 71 | + option.SetTrtInputShape("trans_cam_to_img", {1, 3, 3}); |
| 72 | + option.SetTrtInputData("trans_cam_to_img", |
| 73 | + {721.53771973, 0., 609.55932617, 0., 721.53771973, |
| 74 | + 172.85400391, 0, 0, 1}); |
| 75 | + option.EnablePaddleTrtCollectShape(); |
| 76 | + } |
| 77 | + option.UsePaddleBackend(); |
| 78 | + |
| 79 | + std::string model_dir = argv[1]; |
| 80 | + std::string test_image = argv[2]; |
| 81 | + InitAndInfer(model_dir, test_image, option); |
| 82 | + return 0; |
| 83 | +} |
0 commit comments