|
| 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 | +#include "gflags/gflags.h" |
| 17 | + |
| 18 | +DEFINE_string(model, "", "Directory of the inference model."); |
| 19 | +DEFINE_string(image, "", "Path of the image file."); |
| 20 | +DEFINE_string(device, "cpu", |
| 21 | + "Type of inference device, support 'cpu' or 'gpu'."); |
| 22 | +DEFINE_string(backend, "default", |
| 23 | + "The inference runtime backend, support: ['default', 'ort', " |
| 24 | + "'paddle', 'ov', 'trt', 'paddle_trt']"); |
| 25 | +DEFINE_bool(use_fp16, false, "Whether to use FP16 mode, only support 'trt' and 'paddle_trt' backend"); |
| 26 | + |
| 27 | +void PrintUsage() { |
| 28 | + std::cout << "Usage: infer_demo --model model_path --image img_path --device [cpu|gpu] --backend " |
| 29 | + "[default|ort|paddle|ov|trt|paddle_trt] " |
| 30 | + "--use_fp16 false" |
| 31 | + << std::endl; |
| 32 | + std::cout << "Default value of device: cpu" << std::endl; |
| 33 | + std::cout << "Default value of backend: default" << std::endl; |
| 34 | + std::cout << "Default value of use_fp16: false" << std::endl; |
| 35 | +} |
| 36 | + |
| 37 | +bool CreateRuntimeOption(fastdeploy::RuntimeOption* option) { |
| 38 | + if (FLAGS_device == "gpu") { |
| 39 | + option->UseGpu(); |
| 40 | + if (FLAGS_backend == "ort") { |
| 41 | + option->UseOrtBackend(); |
| 42 | + } else if (FLAGS_backend == "paddle") { |
| 43 | + option->UsePaddleBackend(); |
| 44 | + } else if (FLAGS_backend == "trt" || |
| 45 | + FLAGS_backend == "paddle_trt") { |
| 46 | + option->UseTrtBackend(); |
| 47 | + option->SetTrtInputShape("input", {1, 3, 128, 128}); |
| 48 | + if (FLAGS_backend == "paddle_trt") { |
| 49 | + option->EnablePaddleToTrt(); |
| 50 | + } |
| 51 | + if (FLAGS_use_fp16) { |
| 52 | + option->EnableTrtFP16(); |
| 53 | + } |
| 54 | + } else if (FLAGS_backend == "default") { |
| 55 | + return true; |
| 56 | + } else { |
| 57 | + std::cout << "While inference with GPU, only support default/ort/paddle/trt/paddle_trt now, " << FLAGS_backend << " is not supported." << std::endl; |
| 58 | + return false; |
| 59 | + } |
| 60 | + } else if (FLAGS_device == "cpu") { |
| 61 | + if (FLAGS_backend == "ort") { |
| 62 | + option->UseOrtBackend(); |
| 63 | + } else if (FLAGS_backend == "ov") { |
| 64 | + option->UseOpenVINOBackend(); |
| 65 | + } else if (FLAGS_backend == "paddle") { |
| 66 | + option->UsePaddleBackend(); |
| 67 | + } else if (FLAGS_backend == "default") { |
| 68 | + return true; |
| 69 | + } else { |
| 70 | + std::cout << "While inference with CPU, only support default/ort/ov/paddle now, " << FLAGS_backend << " is not supported." << std::endl; |
| 71 | + return false; |
| 72 | + } |
| 73 | + } else { |
| 74 | + std::cerr << "Only support device CPU/GPU now, " << FLAGS_device << " is not supported." << std::endl; |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +int main(int argc, char* argv[]) { |
| 82 | + google::ParseCommandLineFlags(&argc, &argv, true); |
| 83 | + auto option = fastdeploy::RuntimeOption(); |
| 84 | + if (!CreateRuntimeOption(&option)) { |
| 85 | + PrintUsage(); |
| 86 | + return -1; |
| 87 | + } |
| 88 | + |
| 89 | + auto model = fastdeploy::vision::facealign::FaceLandmark1000(FLAGS_model, "", option); |
| 90 | + if (!model.Initialized()) { |
| 91 | + std::cerr << "Failed to initialize." << std::endl; |
| 92 | + return -1; |
| 93 | + } |
| 94 | + |
| 95 | + auto im = cv::imread(FLAGS_image); |
| 96 | + auto im_bak = im.clone(); |
| 97 | + |
| 98 | + fastdeploy::vision::FaceAlignmentResult res; |
| 99 | + if (!model.Predict(&im, &res)) { |
| 100 | + std::cerr << "Failed to predict." << std::endl; |
| 101 | + return -1; |
| 102 | + } |
| 103 | + std::cout << res.Str() << std::endl; |
| 104 | + |
| 105 | + auto vis_im = fastdeploy::vision::VisFaceAlignment(im_bak, res); |
| 106 | + cv::imwrite("vis_result.jpg", vis_im); |
| 107 | + std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
0 commit comments