diff --git a/ForegroundSegmentation/pluginMediaHandler.cpp b/ForegroundSegmentation/pluginMediaHandler.cpp deleted file mode 100644 index 74916d086ddd434e5ab60db6879309b6cadf620b..0000000000000000000000000000000000000000 --- a/ForegroundSegmentation/pluginMediaHandler.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2020 Savoir-faire Linux Inc. - * - * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "pluginMediaHandler.h" -// Logger -#include "pluglog.h" -const char sep = separator(); -const std::string TAG = "FORESEG"; - -#define NAME "Foreground Segmentation" - -namespace jami -{ - PluginMediaHandler::PluginMediaHandler(std::map<std::string, std::string>&& ppm, std::string &&datapath): - datapath_{datapath}, ppm_{ppm} - { - setGlobalPluginParameters(ppm_); - setId(datapath_); - // setName(NAME); - mVS = std::make_shared<VideoSubscriber>(datapath_); - } - - void PluginMediaHandler::notifyAVFrameSubject(const StreamData &data, jami::avSubjectPtr subject) - { - Plog::log(Plog::LogPriority::INFO, TAG, "IN AVFRAMESUBJECT"); - std::ostringstream oss; - std::string direction = data.direction ? "Receive" : "Preview"; - oss << "NEW SUBJECT: [" << data.id << "," << direction << "]" << std::endl; - - bool preferredStreamDirection = false; - if (!ppm_.empty() && ppm_.find("streamslist") != ppm_.end()) - { - Plog::log(Plog::LogPriority::INFO, TAG, "SET PARAMETERS"); - preferredStreamDirection = ppm_.at("streamslist")=="in"?true:false; - } - oss << "preferredStreamDirection " << preferredStreamDirection << std::endl; - if (data.type == StreamType::video && !data.direction && data.direction == preferredStreamDirection) - { - subject->attach(mVS.get()); // my image - oss << "got my sent image attached" << std::endl; - } else if (data.type == StreamType::video && data.direction && data.direction == preferredStreamDirection) - { - subject->attach(mVS.get()); // the image I receive from the others on the call - } - Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); - } - - std::map<std::string, std::string> PluginMediaHandler::getCallMediaHandlerDetails() - { - std::map<std::string, std::string> mediaHandlerDetails = {}; - mediaHandlerDetails["name"] = NAME; - mediaHandlerDetails["iconPath"] = datapath_ + sep + "icon.png"; - mediaHandlerDetails["pluginId"] = id(); - - - return mediaHandlerDetails; - } - - void PluginMediaHandler::setPreferenceAttribute(const std::string &key, const std::string &value) - { - auto it = ppm_.find(key); - if (it != ppm_.end()) - { - if (ppm_[key] != value) - { - ppm_[key] = value; - if (key == "backgroundlist") - { - mVS->setBackground(dataPath(), value); - } - } - } - } - - bool PluginMediaHandler::preferenceMapHasKey(const std::string &key) - { - if (ppm_.find(key) == ppm_.end()) - { - return false; - } - return true; - } - - void PluginMediaHandler::detach() - { - mVS->detach(); - } - - PluginMediaHandler::~PluginMediaHandler() - { - std::ostringstream oss; - oss << " ~FORESEG Plugin" << std::endl; - Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); - detach(); - } -} diff --git a/GreenScreen/TFInference.cpp b/GreenScreen/TFInference.cpp index f7053f5bde953fefa43b38c61a6d47a34e5739f8..678bc94c0c28057d5db4189591b9d2dc4109b7c5 100644 --- a/GreenScreen/TFInference.cpp +++ b/GreenScreen/TFInference.cpp @@ -56,7 +56,7 @@ TensorflowInference::~TensorflowInference() {} bool TensorflowInference::isAllocated() const { - return allocated; + return allocated_; } #ifdef TFLITE @@ -108,7 +108,7 @@ TensorflowInference::allocateTensors() std::runtime_error("Failed to allocate tensors!"); } else { Plog::log(Plog::LogPriority::INFO, "TENSOR", "TENSORS ALLOCATED" ); - allocated = true; + allocated_ = true; } } @@ -226,7 +226,7 @@ TensorflowInference::LoadGraph() tensorflow::GraphDef graph_def; tensorflow::Status load_graph_status = tensorflow::ReadBinaryProto(tensorflow::Env::Default(), tfModel.modelPath, &graph_def); if (!load_graph_status.ok()) { - allocated = false; + allocated_ = false; Plog::log(Plog::LogPriority::INFO, "LOAD GRAPH", "A problem occured when loading the graph"); return ; } @@ -259,12 +259,12 @@ TensorflowInference::LoadGraph() tensorflow::Status session_create_status = session->Create(graph_def); if (!session_create_status.ok()) { Plog::log(Plog::LogPriority::INFO, "INIT SESSION", "A problem occured when initializating session"); - allocated = true; + allocated_ = true; return ; } Plog::log(Plog::LogPriority::INFO, "INIT SESSION", "session initialized"); - allocated = true; + allocated_ = true; } void diff --git a/GreenScreen/TFInference.h b/GreenScreen/TFInference.h index 9741119b597d377a4138ac83476510df77345689..823dab91ec278e7e602b7ab1aef2f3e4d4b38409 100644 --- a/GreenScreen/TFInference.h +++ b/GreenScreen/TFInference.h @@ -148,6 +148,6 @@ protected: */ size_t nbLabels; - bool allocated = false; + bool allocated_ = false; }; } diff --git a/GreenScreen/pluginMediaHandler.cpp b/GreenScreen/pluginMediaHandler.cpp index 8c5d863fbbaa6f2d296e974545ba099759497b7c..c384e633dde4def14a98ce831259272bb12190ef 100644 --- a/GreenScreen/pluginMediaHandler.cpp +++ b/GreenScreen/pluginMediaHandler.cpp @@ -78,8 +78,8 @@ PluginMediaHandler::setPreferenceAttribute(const std::string& key, const std::st if (it != ppm_.end()) { if (ppm_[key] != value) { ppm_[key] = value; - if (key == "backgroundlist") { - mVS->setBackground(dataPath(), value); + if (key == "background") { + mVS->setBackground(value); } } } @@ -88,7 +88,7 @@ PluginMediaHandler::setPreferenceAttribute(const std::string& key, const std::st bool PluginMediaHandler::preferenceMapHasKey(const std::string& key) { - if (key == "backgroundlist") { + if (key == "background") { return true; } return false; diff --git a/GreenScreen/pluginParameters.cpp b/GreenScreen/pluginParameters.cpp index c118ebd6e6fb8dea19f69da1f8b498bbcd47747c..0729cbbd16af990ec88ecfb5b70c997690c72b47 100644 --- a/GreenScreen/pluginParameters.cpp +++ b/GreenScreen/pluginParameters.cpp @@ -35,8 +35,8 @@ setGlobalPluginParameters(std::map<std::string, std::string> pp) pluginParameters.model = pp.at("modellist"); Plog::log(Plog::LogPriority::INFO, "GLOBAL MODEL ", pluginParameters.model); } - if(pp.find("backgroundlist") != pp.end()) { - pluginParameters.image = pp.at("backgroundlist"); + if(pp.find("background") != pp.end()) { + pluginParameters.image = pp.at("background"); Plog::log(Plog::LogPriority::INFO, "GLOBAL IMAGE ", pluginParameters.image); } } diff --git a/GreenScreen/pluginProcessor.cpp b/GreenScreen/pluginProcessor.cpp index 991bf81b68e8ea658f69796d17b0f780c7765e49..d289b878d79ca59ed1f7488ea2dd8462b0bfe260 100644 --- a/GreenScreen/pluginProcessor.cpp +++ b/GreenScreen/pluginProcessor.cpp @@ -46,31 +46,32 @@ PluginProcessor::PluginProcessor(const std::string& dataPath): pluginInference{TFModel{dataPath + sep + "models" + sep + mPluginParameters->model}} { initModel(); - setBackgroundImage(dataPath, mPluginParameters->image); + setBackgroundImage(mPluginParameters->image); } void -PluginProcessor::setBackgroundImage(const std::string& dataPath, const std::string& value) +PluginProcessor::setBackgroundImage(const std::string& backgroundPath) { - backgroundPath = dataPath + sep + "backgrounds" + sep + value; // cv::Size size = cv::Size{0,0}; if (!backgroundImage.empty()) size = backgroundImage.size(); - backgroundImage = cv::imread(backgroundPath); - if (backgroundImage.cols == 0) { + cv::Mat newBackgroundImage = cv::imread(backgroundPath); + if (newBackgroundImage.cols == 0) { Plog::log(Plog::LogPriority::ERR, TAG, "Background image not Loaded"); } else { Plog::log(Plog::LogPriority::INFO, TAG, "Background image Loaded"); - } - - cv::cvtColor(backgroundImage, backgroundImage, cv::COLOR_BGR2RGB); - backgroundImage.convertTo(backgroundImage, CV_32FC3); - if (size.height) { - cv::resize(backgroundImage, backgroundImage, size); - backgroundRotation = 0; + cv::cvtColor(newBackgroundImage, newBackgroundImage, cv::COLOR_BGR2RGB); + newBackgroundImage.convertTo(newBackgroundImage, CV_32FC3); + if (size.height) { + cv::resize(newBackgroundImage, newBackgroundImage, size); + backgroundRotation = 0; + } + backgroundImage = newBackgroundImage.clone(); + newBackgroundImage.release(); + hasBackground_ = true; } } @@ -300,4 +301,10 @@ PluginProcessor::rotateFrame(int angle, cv::Mat& mat) } } } + +bool +PluginProcessor::hasBackground() const +{ + return hasBackground_; +} } // namespace jami diff --git a/GreenScreen/pluginProcessor.h b/GreenScreen/pluginProcessor.h index ea6ba29cf748f02a7ef0e1ca8d33cc4dcacb4f8d..8224d8016dbdd26107983e8e7500b60a993c020a 100644 --- a/GreenScreen/pluginProcessor.h +++ b/GreenScreen/pluginProcessor.h @@ -64,8 +64,9 @@ public: void drawMaskOnFrame(cv::Mat& frame, cv::Mat& frameReduced, std::vector<float>computedMask, int lineSize, int angle); int getBackgroundRotation(); void setBackgroundRotation(int angle); - void setBackgroundImage(const std::string& dataPath, const std::string& value); + void setBackgroundImage(const std::string& backgroundPath); void rotateFrame(int angle, cv::Mat& mat); + bool hasBackground() const; // Output predictions std::vector<float> computedMask; @@ -84,5 +85,6 @@ private: // Frame cv::Mat frame; int backgroundRotation = 0; + bool hasBackground_ = false; }; } // namespace jami diff --git a/GreenScreen/preferences-tfcc.json b/GreenScreen/preferences-tfcc.json index f18e45af8944ea9bd0a11fa5363490ce5c92550b..8a07ea6e25bb8bf3ca68fcc9e32541d22cbf87a8 100644 --- a/GreenScreen/preferences-tfcc.json +++ b/GreenScreen/preferences-tfcc.json @@ -23,13 +23,12 @@ }, { "category" : "backgrounds", - "type": "UserList", - "key": "backgroundlist", + "type": "Path", + "mimeType": "image/png", + "key": "background", "title": "Background image", "summary": "Select the image background to use", - "defaultValue": "background2.png", - "entries": [ "Add new image", "Painture", "Beach" ], - "entryValues": ["", "background1.png", "background2.png"], + "defaultValue": "data/backgrounds/background2.png", "scope": "plugin,Foreground Segmentation" } ] diff --git a/GreenScreen/preferences-tflite.json b/GreenScreen/preferences-tflite.json index 505a154a99877db8a56ef9be7ae8c6d5550ce182..81784dbcd0e1a2c7fcea66f7ec467456d7e807e6 100644 --- a/GreenScreen/preferences-tflite.json +++ b/GreenScreen/preferences-tflite.json @@ -23,13 +23,12 @@ }, { "category" : "backgrounds", - "type": "UserList", - "key": "backgroundlist", + "type": "Path", + "mimeType": "image/png", + "key": "background", "title": "Background image", "summary": "Select the image background to use", - "defaultValue": "background2.png", - "entries": [ "Add new image", "Painture", "Beach" ], - "entryValues": ["", "background1.png", "background2.png"], + "defaultValue": "data/backgrounds/background2.png", "scope": "plugin,Foreground Segmentation" } ] diff --git a/GreenScreen/videoSubscriber.cpp b/GreenScreen/videoSubscriber.cpp index 7fd4cb68f0d471faddc2033409c7988bfc8a2c38..4a6fba3f9ddb0806959ba3509463dff5783d28aa 100644 --- a/GreenScreen/videoSubscriber.cpp +++ b/GreenScreen/videoSubscriber.cpp @@ -75,7 +75,7 @@ VideoSubscriber::~VideoSubscriber() void VideoSubscriber::update(jami::Observable<AVFrame*> *, AVFrame* const &iFrame) { - if (pluginProcessor.pluginInference.isAllocated()) { + if (pluginProcessor.pluginInference.isAllocated() && pluginProcessor.hasBackground()) { if (!iFrame) return; AVFrame * pluginFrame = const_cast<AVFrame *>(iFrame); @@ -196,8 +196,8 @@ VideoSubscriber::stop() } void -VideoSubscriber::setBackground(const std::string& dataPath, const std::string& value) +VideoSubscriber::setBackground(const std::string& backgroundPath) { - pluginProcessor.setBackgroundImage(dataPath, value); + pluginProcessor.setBackgroundImage(backgroundPath); } } diff --git a/GreenScreen/videoSubscriber.h b/GreenScreen/videoSubscriber.h index 4e238d4317cade0609d2e9c88b702f0689a706e4..c7ffd17d935bbfeb02e86be0acac4d737838c7df 100644 --- a/GreenScreen/videoSubscriber.h +++ b/GreenScreen/videoSubscriber.h @@ -64,7 +64,7 @@ public: void detach(); void stop(); - void setBackground(const std::string& dataPath, const std::string& value); + void setBackground(const std::string& backgroundPath); private: