|
| 1 | +#include "ort_session_handler.hpp" |
| 2 | + |
| 3 | +namespace { |
| 4 | +constexpr int BISENETV2_CITYSCAPES_IMAGE_HEIGHT = 1024; |
| 5 | +constexpr int BISENETV2_CITYSCAPES_IMAGE_WIDTH = 1024; |
| 6 | + |
| 7 | +static const std::vector<std::vector<uint8_t>> CITYSCAPES_COLORS = { |
| 8 | + {128, 64, 128}, {244, 35, 232}, {70, 70, 70}, {102, 102, 156}, {190, 153, 153}, {153, 153, 153}, {250, 170, 30}, |
| 9 | + {220, 220, 0}, {107, 142, 35}, {152, 251, 152}, {70, 130, 180}, {220, 20, 60}, {255, 0, 0}, {0, 0, 142}, |
| 10 | + {0, 0, 70}, {0, 60, 100}, {0, 80, 100}, {0, 0, 230}, {119, 11, 32}}; |
| 11 | +} // namespace |
| 12 | + |
| 13 | +int main(int argc, char *argv[]) { |
| 14 | + if (argc != 3) { |
| 15 | + std::cerr << "Usage: [app] [/path/to/image] [path/to/onnx/model]" << std::endl; |
| 16 | + return EXIT_FAILURE; |
| 17 | + } |
| 18 | + const std::string image_path = argv[1]; |
| 19 | + cv::Mat image = cv::imread(image_path); |
| 20 | + |
| 21 | + if (image.empty()) { |
| 22 | + std::cerr << "failed to load " << image_path << std::endl; |
| 23 | + return EXIT_FAILURE; |
| 24 | + } |
| 25 | + |
| 26 | + const std::string onnx_model_path = argv[2]; |
| 27 | + |
| 28 | + std::vector<std::vector<int64_t>> input_tensor_shapes{ |
| 29 | + {1, 3, BISENETV2_CITYSCAPES_IMAGE_HEIGHT, BISENETV2_CITYSCAPES_IMAGE_WIDTH}}; |
| 30 | + deploy::OrtSessionHandler ort_session_handler(onnx_model_path, input_tensor_shapes); |
| 31 | + std::vector<float> input_data = |
| 32 | + ort_session_handler.preprocess(image, BISENETV2_CITYSCAPES_IMAGE_HEIGHT, BISENETV2_CITYSCAPES_IMAGE_WIDTH); |
| 33 | + |
| 34 | + // output data's type might change for each different model |
| 35 | + auto output_data = ort_session_handler.run<int64_t>({input_data}); |
| 36 | + |
| 37 | + // postprocess |
| 38 | + // this might change for each different model |
| 39 | + cv::Mat segm(BISENETV2_CITYSCAPES_IMAGE_HEIGHT, BISENETV2_CITYSCAPES_IMAGE_WIDTH, CV_8UC(3)); |
| 40 | + for (int i = 0; i < BISENETV2_CITYSCAPES_IMAGE_HEIGHT; ++i) { |
| 41 | + cv::Vec3b *ptr_segm = segm.ptr<cv::Vec3b>(i); |
| 42 | + for (int j = 0; j < BISENETV2_CITYSCAPES_IMAGE_WIDTH; ++j) { |
| 43 | + const auto &color = CITYSCAPES_COLORS[output_data[0].first[i * BISENETV2_CITYSCAPES_IMAGE_WIDTH + j]]; |
| 44 | + ptr_segm[j] = cv::Vec3b(color[0], color[1], color[2]); |
| 45 | + } |
| 46 | + } |
| 47 | + cv::resize(segm, segm, image.size(), 0, 0, cv::INTER_NEAREST); |
| 48 | + float blended_alpha = 0.4; |
| 49 | + segm = (1 - blended_alpha) * image + blended_alpha * segm; |
| 50 | + cv::imwrite("out_img.jpg", segm); |
| 51 | + |
| 52 | + return EXIT_SUCCESS; |
| 53 | +} |
0 commit comments