|
| 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 | + |
| 17 | +void CpuInfer(const std::string& model_file, const std::string& image_file) { |
| 18 | + auto model = fastdeploy::vision::detection::NanoDetPlus(model_file); |
| 19 | + if (!model.Initialized()) { |
| 20 | + std::cerr << "Failed to initialize." << std::endl; |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + auto im = cv::imread(image_file); |
| 25 | + auto im_bak = im.clone(); |
| 26 | + |
| 27 | + fastdeploy::vision::DetectionResult res; |
| 28 | + if (!model.Predict(&im, &res)) { |
| 29 | + std::cerr << "Failed to predict." << std::endl; |
| 30 | + return; |
| 31 | + } |
| 32 | + std::cout << res.Str() << std::endl; |
| 33 | + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); |
| 34 | + cv::imwrite("vis_result.jpg", vis_im); |
| 35 | + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; |
| 36 | +} |
| 37 | + |
| 38 | +void GpuInfer(const std::string& model_file, const std::string& image_file) { |
| 39 | + auto option = fastdeploy::RuntimeOption(); |
| 40 | + option.UseGpu(); |
| 41 | + auto model = |
| 42 | + fastdeploy::vision::detection::NanoDetPlus(model_file, "", option); |
| 43 | + if (!model.Initialized()) { |
| 44 | + std::cerr << "Failed to initialize." << std::endl; |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + auto im = cv::imread(image_file); |
| 49 | + auto im_bak = im.clone(); |
| 50 | + |
| 51 | + fastdeploy::vision::DetectionResult res; |
| 52 | + if (!model.Predict(&im, &res)) { |
| 53 | + std::cerr << "Failed to predict." << std::endl; |
| 54 | + return; |
| 55 | + } |
| 56 | + std::cout << res.Str() << std::endl; |
| 57 | + |
| 58 | + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); |
| 59 | + cv::imwrite("vis_result.jpg", vis_im); |
| 60 | + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; |
| 61 | +} |
| 62 | + |
| 63 | +void TrtInfer(const std::string& model_file, const std::string& image_file) { |
| 64 | + auto option = fastdeploy::RuntimeOption(); |
| 65 | + option.UseGpu(); |
| 66 | + option.UseTrtBackend(); |
| 67 | + option.SetTrtInputShape("images", {1, 3, 320, 320}); |
| 68 | + auto model = |
| 69 | + fastdeploy::vision::detection::NanoDetPlus(model_file, "", option); |
| 70 | + if (!model.Initialized()) { |
| 71 | + std::cerr << "Failed to initialize." << std::endl; |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + auto im = cv::imread(image_file); |
| 76 | + auto im_bak = im.clone(); |
| 77 | + |
| 78 | + fastdeploy::vision::DetectionResult res; |
| 79 | + if (!model.Predict(&im, &res)) { |
| 80 | + std::cerr << "Failed to predict." << std::endl; |
| 81 | + return; |
| 82 | + } |
| 83 | + std::cout << res.Str() << std::endl; |
| 84 | + |
| 85 | + auto vis_im = fastdeploy::vision::Visualize::VisDetection(im_bak, res); |
| 86 | + cv::imwrite("vis_result.jpg", vis_im); |
| 87 | + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; |
| 88 | +} |
| 89 | + |
| 90 | +int main(int argc, char* argv[]) { |
| 91 | + if (argc < 4) { |
| 92 | + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " |
| 93 | + "e.g ./infer_model ./nanodet-plus-m_320.onnx ./test.jpeg 0" |
| 94 | + << std::endl; |
| 95 | + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " |
| 96 | + "with gpu; 2: run with gpu and use tensorrt backend." |
| 97 | + << std::endl; |
| 98 | + return -1; |
| 99 | + } |
| 100 | + |
| 101 | + if (std::atoi(argv[3]) == 0) { |
| 102 | + CpuInfer(argv[1], argv[2]); |
| 103 | + } else if (std::atoi(argv[3]) == 1) { |
| 104 | + GpuInfer(argv[1], argv[2]); |
| 105 | + } else if (std::atoi(argv[3]) == 2) { |
| 106 | + TrtInfer(argv[1], argv[2]); |
| 107 | + } |
| 108 | + return 0; |
| 109 | +} |
0 commit comments