blob: 08975274be78707aad6ee4b869cd1f4573dfbf29 [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
23// STL
24#include <condition_variable>
25#include <cstdint>
agsantosac1940d2020-09-17 10:18:40 -040026#include <map>
agsantos5aa39652020-08-11 18:18:04 -040027#include <memory>
28#include <mutex>
29#include <thread>
30#include <vector>
agsantos5aa39652020-08-11 18:18:04 -040031// AvFrame
32extern "C" {
33#include <libavutil/frame.h>
34}
35// Plugin
36#include <plugin/jamiplugin.h>
37#include <plugin/mediahandler.h>
agsantos5aa39652020-08-11 18:18:04 -040038
agsantos796b5af2020-12-22 19:38:27 -050039// 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
agsantos5aa39652020-08-11 18:18:04 -040048namespace jami {
49
agsantosac1940d2020-09-17 10:18:40 -040050class PluginProcessor
51{
agsantos5aa39652020-08-11 18:18:04 -040052public:
agsantos796b5af2020-12-22 19:38:27 -050053 PluginProcessor(const std::string& dataPath, const std::string& model, const std::string& backgroundImage, bool acc);
54 ~PluginProcessor();
agsantos5aa39652020-08-11 18:18:04 -040055
agsantos796b5af2020-12-22 19:38:27 -050056 void initModel(const std::string& modelPath);
agsantosac1940d2020-09-17 10:18:40 -040057 /**
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);
agsantos5aa39652020-08-11 18:18:04 -040063
agsantosac1940d2020-09-17 10:18:40 -040064 /**
65 * @brief computePredictions
66 * Uses the model to compute the predictions and store them in
67 * computedPredictions
68 */
69 void computePredictions();
agsantos5aa39652020-08-11 18:18:04 -040070
agsantosac1940d2020-09-17 10:18:40 -040071 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);
agsantos9dcf4302020-09-01 18:21:48 -040081 bool hasBackground() const;
agsantos31d1a0b2020-10-23 14:05:53 -040082 void resetInitValues(const cv::Size& modelInputSize);
agsantos796b5af2020-12-22 19:38:27 -050083 bool isAllocated();
agsantos5aa39652020-08-11 18:18:04 -040084
agsantosac1940d2020-09-17 10:18:40 -040085 // Output predictions
86 std::vector<float> computedMask;
agsantos5aa39652020-08-11 18:18:04 -040087
agsantosac1940d2020-09-17 10:18:40 -040088 cv::Mat previousMasks[2];
89 cv::Mat backgroundImage;
agsantos5aa39652020-08-11 18:18:04 -040090
agsantosac1940d2020-09-17 10:18:40 -040091 cv::Size kSize;
agsantos5aa39652020-08-11 18:18:04 -040092
agsantosac1940d2020-09-17 10:18:40 -040093 std::string backgroundPath;
agsantos5aa39652020-08-11 18:18:04 -040094
95private:
agsantos796b5af2020-12-22 19:38:27 -050096 int count{0};
agsantosac1940d2020-09-17 10:18:40 -040097 cv::Mat frame;
agsantos796b5af2020-12-22 19:38:27 -050098 int backgroundRotation{0};
99 bool hasBackground_{false};
agsantos31d1a0b2020-10-23 14:05:53 -0400100 cv::Mat bgdModel, fgdModel;
agsantos796b5af2020-12-22 19:38:27 -0500101 int grabCutMode{1}; // cv::GC_INIT_WITH_MASK = 1;
102 int grabCutIterations{5};
103 int grabcutClass{3};
104 int frameCount{3};
agsantosb74f4cb2020-10-01 14:30:43 -0400105 float smoothFactors[3] = {0.6f, 0.3f, 0.1f};
agsantos796b5af2020-12-22 19:38:27 -0500106 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};
agsantos5aa39652020-08-11 18:18:04 -0400126};
127} // namespace jami