|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of NVIDIA CORPORATION nor the names of its |
| 13 | + * contributors may be used to endorse or promote products derived |
| 14 | + * from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 17 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 19 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 20 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <nvidia/aiaa/client.h> |
| 30 | +#include <nvidia/aiaa/utils.h> |
| 31 | + |
| 32 | +#include "../commonutils.h" |
| 33 | +#include <chrono> |
| 34 | + |
| 35 | +int main(int argc, char **argv) { |
| 36 | + if (argc < 2 || cmdOptionExists(argv, argv + argc, "-h")) { |
| 37 | + std::cout << "Usage:: <COMMAND> <OPTIONS>\n" |
| 38 | + " |-h (Help) Print this information |\n" |
| 39 | + " |-server Server URI {default: http://0.0.0.0:5000} |\n" |
| 40 | + " *|-model Model Name [either -label or -model is required] |\n" |
| 41 | + " |-params Input Params (JSON) |\n" |
| 42 | + " *|-image Input Image File |\n" |
| 43 | + " *|-session Session ID |\n" |
| 44 | + " |-output Output Image File |\n" |
| 45 | + " |-timeout Timeout In Seconds {default: 60} |\n" |
| 46 | + " |-ts Print API Latency |\n"; |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | + std::string serverUri = getCmdOption(argv, argv + argc, "-server", "http://0.0.0.0:5000"); |
| 51 | + std::string model = getCmdOption(argv, argv + argc, "-model"); |
| 52 | + |
| 53 | + std::string params = getCmdOption(argv, argv + argc, "-params"); |
| 54 | + std::string inputImageFile = getCmdOption(argv, argv + argc, "-image"); |
| 55 | + std::string sessionId = getCmdOption(argv, argv + argc, "-session"); |
| 56 | + std::string outputImageFile = getCmdOption(argv, argv + argc, "-output"); |
| 57 | + |
| 58 | + int timeout = nvidia::aiaa::Utils::lexical_cast<int>(getCmdOption(argv, argv + argc, "-timeout", "60")); |
| 59 | + bool printTs = cmdOptionExists(argv, argv + argc, "-ts") ? true : false; |
| 60 | + |
| 61 | + if (model.empty()) { |
| 62 | + std::cerr << "Model is required\n"; |
| 63 | + return -1; |
| 64 | + } |
| 65 | + if (inputImageFile.empty() && sessionId.empty()) { |
| 66 | + std::cerr << "Input Image file is missing (Either session-id or input image should be provided)\n"; |
| 67 | + return -1; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + nvidia::aiaa::Client client(serverUri, timeout); |
| 72 | + |
| 73 | + nvidia::aiaa::Model m; |
| 74 | + m = client.model(model); |
| 75 | + |
| 76 | + if (m.name.empty()) { |
| 77 | + std::cerr << "Couldn't find a model for name: " << model << "\n"; |
| 78 | + return -1; |
| 79 | + } |
| 80 | + |
| 81 | + auto begin = std::chrono::high_resolution_clock::now(); |
| 82 | + std::string resultJson = client.inference(m, params, inputImageFile, outputImageFile, sessionId); |
| 83 | + std::cout << "Result (JSON): " << resultJson << std::endl; |
| 84 | + |
| 85 | + auto end = std::chrono::high_resolution_clock::now(); |
| 86 | + auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count(); |
| 87 | + |
| 88 | + if (printTs) { |
| 89 | + std::cout << "API Latency (in milli sec): " << ms << std::endl; |
| 90 | + } |
| 91 | + return 0; |
| 92 | + } catch (nvidia::aiaa::exception &e) { |
| 93 | + std::cerr << "nvidia::aiaa::exception => nvidia.aiaa.error." << e.id << "; description: " << e.name() << "; reason: " << e.what() << std::endl; |
| 94 | + } |
| 95 | + return -1; |
| 96 | +} |
0 commit comments