agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 1 | /** |
Sébastien Blin | cb783e3 | 2021-02-12 11:34:10 -0500 | [diff] [blame] | 2 | * Copyright (C) 2020-2021 Savoir-faire Linux Inc. |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 3 | * |
| 4 | * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 | * USA. |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #pragma once |
| 23 | // STL |
| 24 | #include <condition_variable> |
| 25 | #include <cstdint> |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 26 | #include <map> |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <mutex> |
| 29 | #include <thread> |
| 30 | #include <vector> |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 31 | // AvFrame |
| 32 | extern "C" { |
| 33 | #include <libavutil/frame.h> |
| 34 | } |
| 35 | // Plugin |
| 36 | #include <plugin/jamiplugin.h> |
| 37 | #include <plugin/mediahandler.h> |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 38 | |
agsantos | 796b5af | 2020-12-22 19:38:27 -0500 | [diff] [blame] | 39 | // Opencv processing |
| 40 | #include <opencv2/core.hpp> |
| 41 | #include <onnxruntime_cxx_api.h> |
| 42 | #ifdef NVIDIA |
| 43 | #include <cuda_provider_factory.h> |
| 44 | #endif |
| 45 | #ifdef ANDROID |
| 46 | #include <nnapi_provider_factory.h> |
| 47 | #endif |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 48 | namespace jami { |
| 49 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 50 | class PluginProcessor |
| 51 | { |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 52 | public: |
agsantos | 796b5af | 2020-12-22 19:38:27 -0500 | [diff] [blame] | 53 | PluginProcessor(const std::string& dataPath, const std::string& model, const std::string& backgroundImage, bool acc); |
| 54 | ~PluginProcessor(); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 55 | |
agsantos | 796b5af | 2020-12-22 19:38:27 -0500 | [diff] [blame] | 56 | void initModel(const std::string& modelPath); |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 57 | /** |
| 58 | * @brief feedInput |
| 59 | * Takes a frame and feeds it to the model storage for predictions |
| 60 | * @param frame |
| 61 | */ |
| 62 | void feedInput(const cv::Mat& frame); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 63 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 64 | /** |
| 65 | * @brief computePredictions |
| 66 | * Uses the model to compute the predictions and store them in |
| 67 | * computedPredictions |
| 68 | */ |
| 69 | void computePredictions(); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 70 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 71 | void printMask(); |
| 72 | void drawMaskOnFrame(cv::Mat& frame, |
| 73 | cv::Mat& frameReduced, |
| 74 | std::vector<float> computedMask, |
| 75 | int lineSize, |
| 76 | int angle); |
| 77 | int getBackgroundRotation(); |
| 78 | void setBackgroundRotation(int angle); |
| 79 | void setBackgroundImage(const std::string& backgroundPath); |
| 80 | void rotateFrame(int angle, cv::Mat& mat); |
agsantos | 9dcf430 | 2020-09-01 18:21:48 -0400 | [diff] [blame] | 81 | bool hasBackground() const; |
agsantos | 31d1a0b | 2020-10-23 14:05:53 -0400 | [diff] [blame] | 82 | void resetInitValues(const cv::Size& modelInputSize); |
agsantos | 796b5af | 2020-12-22 19:38:27 -0500 | [diff] [blame] | 83 | bool isAllocated(); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 84 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 85 | // Output predictions |
| 86 | std::vector<float> computedMask; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 87 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 88 | cv::Mat previousMasks[2]; |
| 89 | cv::Mat backgroundImage; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 90 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 91 | cv::Size kSize; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 92 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 93 | std::string backgroundPath; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 94 | |
| 95 | private: |
agsantos | 796b5af | 2020-12-22 19:38:27 -0500 | [diff] [blame] | 96 | int count{0}; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 97 | cv::Mat frame; |
agsantos | 796b5af | 2020-12-22 19:38:27 -0500 | [diff] [blame] | 98 | int backgroundRotation{0}; |
| 99 | bool hasBackground_{false}; |
agsantos | 31d1a0b | 2020-10-23 14:05:53 -0400 | [diff] [blame] | 100 | cv::Mat bgdModel, fgdModel; |
agsantos | 796b5af | 2020-12-22 19:38:27 -0500 | [diff] [blame] | 101 | int grabCutMode{1}; // cv::GC_INIT_WITH_MASK = 1; |
| 102 | int grabCutIterations{5}; |
| 103 | int grabcutClass{3}; |
| 104 | int frameCount{3}; |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 105 | float smoothFactors[3] = {0.6f, 0.3f, 0.1f}; |
agsantos | 796b5af | 2020-12-22 19:38:27 -0500 | [diff] [blame] | 106 | float kernelSize{0.05f}; |
| 107 | |
| 108 | bool isAllocated_{false}; |
| 109 | Ort::Env env{ORT_LOGGING_LEVEL_WARNING, "test"}; |
| 110 | Ort::Value input_tensor_{nullptr}; |
| 111 | std::array<int64_t, 3> input_shape_{257, 257, 3}; |
| 112 | |
| 113 | Ort::Value output_tensor_{nullptr}; |
| 114 | std::array<int64_t, 4> output_shape_{1, 17, 17, 1}; |
| 115 | |
| 116 | |
| 117 | std::array<float, 257 * 257 * 3> input_image_{}; |
| 118 | |
| 119 | std::array<float, 17 * 17> results_{}; |
| 120 | Ort::Session* session_{}; |
| 121 | const char* input_names[8] = {"image:0"}; |
| 122 | const char* output_names[11] = {"Identity:0"}; |
| 123 | Ort::SessionOptions sessOpt_; |
| 124 | |
| 125 | bool activateAcc_{false}; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 126 | }; |
| 127 | } // namespace jami |