preferences: adjusts for Path preference

Change-Id: Icf1d0bccec77320ad4b66f90d2999f7210627629
diff --git a/ForegroundSegmentation/pluginMediaHandler.cpp b/ForegroundSegmentation/pluginMediaHandler.cpp
deleted file mode 100644
index 74916d0..0000000
--- 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 f7053f5..678bc94 100644
--- a/GreenScreen/TFInference.cpp
+++ b/GreenScreen/TFInference.cpp
@@ -56,7 +56,7 @@
 bool
 TensorflowInference::isAllocated() const
 {
-    return allocated;
+    return allocated_;
 }
 
 #ifdef TFLITE
@@ -108,7 +108,7 @@
         std::runtime_error("Failed to allocate tensors!");
     } else {
         Plog::log(Plog::LogPriority::INFO, "TENSOR", "TENSORS ALLOCATED" );
-        allocated = true;
+        allocated_ = true;
     }
 }
 
@@ -226,7 +226,7 @@
     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 @@
     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 9741119..823dab9 100644
--- a/GreenScreen/TFInference.h
+++ b/GreenScreen/TFInference.h
@@ -148,6 +148,6 @@
      */
     size_t nbLabels;
 
-    bool allocated = false;
+    bool allocated_ = false;
 };
 }
diff --git a/GreenScreen/pluginMediaHandler.cpp b/GreenScreen/pluginMediaHandler.cpp
index 8c5d863..c384e63 100644
--- a/GreenScreen/pluginMediaHandler.cpp
+++ b/GreenScreen/pluginMediaHandler.cpp
@@ -78,8 +78,8 @@
 	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 @@
 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 c118ebd..0729cbb 100644
--- a/GreenScreen/pluginParameters.cpp
+++ b/GreenScreen/pluginParameters.cpp
@@ -35,8 +35,8 @@
             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 991bf81..d289b87 100644
--- a/GreenScreen/pluginProcessor.cpp
+++ b/GreenScreen/pluginProcessor.cpp
@@ -46,31 +46,32 @@
 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 @@
 		}
 	}
 }
+
+bool
+PluginProcessor::hasBackground() const
+{
+    return hasBackground_;
+}
 } // namespace jami
diff --git a/GreenScreen/pluginProcessor.h b/GreenScreen/pluginProcessor.h
index ea6ba29..8224d80 100644
--- a/GreenScreen/pluginProcessor.h
+++ b/GreenScreen/pluginProcessor.h
@@ -64,8 +64,9 @@
 	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 @@
 	// 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 f18e45a..8a07ea6 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 505a154..81784db 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 7fd4cb6..4a6fba3 100644
--- a/GreenScreen/videoSubscriber.cpp
+++ b/GreenScreen/videoSubscriber.cpp
@@ -75,7 +75,7 @@
 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 @@
 }
 
 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 4e238d4..c7ffd17 100644
--- a/GreenScreen/videoSubscriber.h
+++ b/GreenScreen/videoSubscriber.h
@@ -64,7 +64,7 @@
 
     void detach();
     void stop();
-    void setBackground(const std::string& dataPath, const std::string& value);
+    void setBackground(const std::string& backgroundPath);
 
 
 private: