Skip to content
Snippets Groups Projects
Commit 9dcf4309 authored by Aline Gondim Santos's avatar Aline Gondim Santos
Browse files

preferences: adjusts for Path preference

Change-Id: Icf1d0bccec77320ad4b66f90d2999f7210627629
parent 364bbae3
No related branches found
No related tags found
No related merge requests found
/*
* 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();
}
}
......@@ -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
......
......@@ -148,6 +148,6 @@ protected:
*/
size_t nbLabels;
bool allocated = false;
bool allocated_ = false;
};
}
......@@ -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;
......
......@@ -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);
}
}
......
......@@ -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
......@@ -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
......@@ -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"
}
]
......@@ -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"
}
]
......@@ -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);
}
}
......@@ -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:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment