blob: f30a661b409d27d4a63f439328b2c024be20c147 [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>
agsantosdd6a62a2021-03-29 17:13:27 -040035#ifdef __ANDROID__
agsantos796b5af2020-12-22 19:38:27 -050036#include <nnapi_provider_factory.h>
37#endif
agsantosdd6a62a2021-03-29 17:13:27 -040038
39#include <frameFilter.h>
40#include <frameUtils.h>
41#include <mediaStream.h>
42#include <functional>
43
agsantos5aa39652020-08-11 18:18:04 -040044namespace jami {
45
agsantosdd6a62a2021-03-29 17:13:27 -040046static const char* modelInputNames[8] = {"image:0"};
47static const char* modelOutputNames[11] = {"Identity:0"};
48
agsantosac1940d2020-09-17 10:18:40 -040049class PluginProcessor
50{
agsantos5aa39652020-08-11 18:18:04 -040051public:
agsantosdd6a62a2021-03-29 17:13:27 -040052 PluginProcessor(const std::string& model, bool acc);
agsantos796b5af2020-12-22 19:38:27 -050053 ~PluginProcessor();
agsantos5aa39652020-08-11 18:18:04 -040054
agsantosdd6a62a2021-03-29 17:13:27 -040055 void initModel(const std::string& modelPath, bool activateAcc);
agsantosac1940d2020-09-17 10:18:40 -040056 /**
57 * @brief feedInput
58 * Takes a frame and feeds it to the model storage for predictions
59 * @param frame
60 */
agsantosdd6a62a2021-03-29 17:13:27 -040061 void feedInput(AVFrame* input);
agsantos5aa39652020-08-11 18:18:04 -040062
agsantosac1940d2020-09-17 10:18:40 -040063 /**
64 * @brief computePredictions
65 * Uses the model to compute the predictions and store them in
66 * computedPredictions
67 */
68 void computePredictions();
agsantos5aa39652020-08-11 18:18:04 -040069
agsantosac1940d2020-09-17 10:18:40 -040070 void printMask();
agsantosdd6a62a2021-03-29 17:13:27 -040071 void drawMaskOnFrame(AVFrame* frame, AVFrame* frameReduced, int angle);
72 bool isAllocated() { return isAllocated_; }
73 void setBlur(bool isBlur) { isBlur_ = isBlur; }
74 void setBlurLevel(const std::string& blurLevel) { blurLevel_ = blurLevel; }
75 void setBackgroundImage(const std::string& backgroundPath) { backgroundPath_ = backgroundPath; }
76 void initFilters(const std::pair<int, int>& inputSize, int format, int angle);
agsantos5aa39652020-08-11 18:18:04 -040077
agsantosdd6a62a2021-03-29 17:13:27 -040078 std::pair<int, int> modelInputDimensions {257, 257};
79 std::map<int, std::string> rotation = {{90, "-PI/2"},
80 {-90, "PI/2"},
81 {-180, "-PI"},
82 {180, "PI"},
83 {0, "0"}};
agsantos5aa39652020-08-11 18:18:04 -040084
85private:
agsantosdd6a62a2021-03-29 17:13:27 -040086 void resetInitValues();
87 void loadBackground();
88 MediaStream getbgAVFrameInfos();
agsantos796b5af2020-12-22 19:38:27 -050089
agsantosdd6a62a2021-03-29 17:13:27 -040090 bool isBlur_ {false};
91 std::string blurLevel_;
92 std::string backgroundPath_;
93 cv::Mat previousMasks_[2];
94 std::vector<float> computedMask_;
95 cv::Mat cvFrame_;
agsantos796b5af2020-12-22 19:38:27 -050096
agsantosdd6a62a2021-03-29 17:13:27 -040097 // process variables
98 cv::Size kSize_;
99 int count_ {0};
100 cv::Mat bgdModel_, fgdModel_;
101 int grabCutMode_ {1}; // cv::GC_INIT_WITH_MASK = 1;
102 int grabCutIterations_ {4};
103 int grabcutClass_ {3};
104 int frameCount_ {5};
105 float smoothFactors_[3] = {0.6f, 0.3f, 0.1f};
106 float kernelSize_ {0.05f};
agsantos796b5af2020-12-22 19:38:27 -0500107
agsantosdd6a62a2021-03-29 17:13:27 -0400108 // filters
109 std::string mainFilterDescription_;
110 FrameFilter mainFilter_;
111 std::unique_ptr<AVFormatContext, std::function<void(AVFormatContext*)>> pFormatCtx_
112 = {avformat_alloc_context(), [](AVFormatContext* ptr) {
113 avformat_close_input(&ptr);
114 avformat_free_context(ptr);
115 }};
116 int videoStream_ {-1};
117 MediaStream ims_, ims2_, maskms_;
agsantos796b5af2020-12-22 19:38:27 -0500118
agsantosdd6a62a2021-03-29 17:13:27 -0400119 // onnx related
120 bool isAllocated_ {false};
121 Ort::Env env_ {ORT_LOGGING_LEVEL_WARNING, "test"};
122 Ort::Session* session_ {};
agsantos796b5af2020-12-22 19:38:27 -0500123 Ort::SessionOptions sessOpt_;
124
agsantosdd6a62a2021-03-29 17:13:27 -0400125 Ort::Value input_tensor_ {nullptr};
126 std::array<int64_t, 3> input_shape_ {257, 257, 3};
127 std::array<float, 257 * 257 * 3> input_image_ {};
128
129 Ort::Value output_tensor_ {nullptr};
130 std::array<int64_t, 4> output_shape_ {1, 17, 17, 1};
131 std::array<float, 17 * 17> results_ {};
agsantos5aa39652020-08-11 18:18:04 -0400132};
133} // namespace jami