blob: aec78eb5d1483c4aae4dfd60a98809605f29db66 [file] [log] [blame]
agsantos5aa39652020-08-11 18:18:04 -04001/**
Sébastien Blincb783e32021-02-12 11:34:10 -05002 * Copyright (C) 2020-2021 Savoir-faire Linux Inc.
agsantos5aa39652020-08-11 18:18:04 -04003 *
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
agsantosac1940d2020-09-17 10:18:40 -040018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA.
agsantos5aa39652020-08-11 18:18:04 -040020 */
21
22#pragma once
agsantosdd6a62a2021-03-29 17:13:27 -040023
agsantosac1940d2020-09-17 10:18:40 -040024#include <map>
agsantos5aa39652020-08-11 18:18:04 -040025#include <vector>
agsantos5aa39652020-08-11 18:18:04 -040026extern "C" {
27#include <libavutil/frame.h>
28}
agsantosdd6a62a2021-03-29 17:13:27 -040029
agsantos5aa39652020-08-11 18:18:04 -040030#include <plugin/jamiplugin.h>
31#include <plugin/mediahandler.h>
agsantos5aa39652020-08-11 18:18:04 -040032
agsantos796b5af2020-12-22 19:38:27 -050033#include <opencv2/core.hpp>
34#include <onnxruntime_cxx_api.h>
35#ifdef NVIDIA
36#include <cuda_provider_factory.h>
37#endif
agsantosdd6a62a2021-03-29 17:13:27 -040038#ifdef __ANDROID__
agsantos796b5af2020-12-22 19:38:27 -050039#include <nnapi_provider_factory.h>
40#endif
agsantosdd6a62a2021-03-29 17:13:27 -040041
42#include <frameFilter.h>
43#include <frameUtils.h>
44#include <mediaStream.h>
45#include <functional>
46
agsantos5aa39652020-08-11 18:18:04 -040047namespace jami {
48
agsantosdd6a62a2021-03-29 17:13:27 -040049static const char* modelInputNames[8] = {"image:0"};
50static const char* modelOutputNames[11] = {"Identity:0"};
51
agsantosac1940d2020-09-17 10:18:40 -040052class PluginProcessor
53{
agsantos5aa39652020-08-11 18:18:04 -040054public:
agsantosdd6a62a2021-03-29 17:13:27 -040055 PluginProcessor(const std::string& model, bool acc);
agsantos796b5af2020-12-22 19:38:27 -050056 ~PluginProcessor();
agsantos5aa39652020-08-11 18:18:04 -040057
agsantosdd6a62a2021-03-29 17:13:27 -040058 void initModel(const std::string& modelPath, bool activateAcc);
agsantosac1940d2020-09-17 10:18:40 -040059 /**
60 * @brief feedInput
61 * Takes a frame and feeds it to the model storage for predictions
62 * @param frame
63 */
agsantosdd6a62a2021-03-29 17:13:27 -040064 void feedInput(AVFrame* input);
agsantos5aa39652020-08-11 18:18:04 -040065
agsantosac1940d2020-09-17 10:18:40 -040066 /**
67 * @brief computePredictions
68 * Uses the model to compute the predictions and store them in
69 * computedPredictions
70 */
71 void computePredictions();
agsantos5aa39652020-08-11 18:18:04 -040072
agsantosac1940d2020-09-17 10:18:40 -040073 void printMask();
agsantosdd6a62a2021-03-29 17:13:27 -040074 void drawMaskOnFrame(AVFrame* frame, AVFrame* frameReduced, int angle);
75 bool isAllocated() { return isAllocated_; }
76 void setBlur(bool isBlur) { isBlur_ = isBlur; }
77 void setBlurLevel(const std::string& blurLevel) { blurLevel_ = blurLevel; }
78 void setBackgroundImage(const std::string& backgroundPath) { backgroundPath_ = backgroundPath; }
79 void initFilters(const std::pair<int, int>& inputSize, int format, int angle);
agsantos5aa39652020-08-11 18:18:04 -040080
agsantosdd6a62a2021-03-29 17:13:27 -040081 std::pair<int, int> modelInputDimensions {257, 257};
82 std::map<int, std::string> rotation = {{90, "-PI/2"},
83 {-90, "PI/2"},
84 {-180, "-PI"},
85 {180, "PI"},
86 {0, "0"}};
agsantos5aa39652020-08-11 18:18:04 -040087
88private:
agsantosdd6a62a2021-03-29 17:13:27 -040089 void resetInitValues();
90 void loadBackground();
91 MediaStream getbgAVFrameInfos();
agsantos796b5af2020-12-22 19:38:27 -050092
agsantosdd6a62a2021-03-29 17:13:27 -040093 bool isBlur_ {false};
94 std::string blurLevel_;
95 std::string backgroundPath_;
96 cv::Mat previousMasks_[2];
97 std::vector<float> computedMask_;
98 cv::Mat cvFrame_;
agsantos796b5af2020-12-22 19:38:27 -050099
agsantosdd6a62a2021-03-29 17:13:27 -0400100 // process variables
101 cv::Size kSize_;
102 int count_ {0};
103 cv::Mat bgdModel_, fgdModel_;
104 int grabCutMode_ {1}; // cv::GC_INIT_WITH_MASK = 1;
105 int grabCutIterations_ {4};
106 int grabcutClass_ {3};
107 int frameCount_ {5};
108 float smoothFactors_[3] = {0.6f, 0.3f, 0.1f};
109 float kernelSize_ {0.05f};
agsantos796b5af2020-12-22 19:38:27 -0500110
agsantosdd6a62a2021-03-29 17:13:27 -0400111 // filters
112 std::string mainFilterDescription_;
113 FrameFilter mainFilter_;
114 std::unique_ptr<AVFormatContext, std::function<void(AVFormatContext*)>> pFormatCtx_
115 = {avformat_alloc_context(), [](AVFormatContext* ptr) {
116 avformat_close_input(&ptr);
117 avformat_free_context(ptr);
118 }};
119 int videoStream_ {-1};
120 MediaStream ims_, ims2_, maskms_;
agsantos796b5af2020-12-22 19:38:27 -0500121
agsantosdd6a62a2021-03-29 17:13:27 -0400122 // onnx related
123 bool isAllocated_ {false};
124 Ort::Env env_ {ORT_LOGGING_LEVEL_WARNING, "test"};
125 Ort::Session* session_ {};
agsantos796b5af2020-12-22 19:38:27 -0500126 Ort::SessionOptions sessOpt_;
127
agsantosdd6a62a2021-03-29 17:13:27 -0400128 Ort::Value input_tensor_ {nullptr};
129 std::array<int64_t, 3> input_shape_ {257, 257, 3};
130 std::array<float, 257 * 257 * 3> input_image_ {};
131
132 Ort::Value output_tensor_ {nullptr};
133 std::array<int64_t, 4> output_shape_ {1, 17, 17, 1};
134 std::array<float, 17 * 17> results_ {};
agsantos5aa39652020-08-11 18:18:04 -0400135};
136} // namespace jami