From 8355c3eff1a297999d0e7d906d82204a5ff754d0 Mon Sep 17 00:00:00 2001
From: Max Breitenfeldt <git@mxbr.me>
Date: Sun, 15 Aug 2021 12:54:54 +0200
Subject: [PATCH] Set model path via UI

---
 Src/Model/BioTrackerTrackingAlgorithm.cpp | 13 ++++-
 Src/Model/TrackerParameter.h              |  9 +++
 Src/View/TrackerParameterView.cpp         | 14 +++++
 Src/View/TrackerParameterView.h           |  3 +
 Src/View/TrackerParameterView.ui          | 68 ++++++++++++++++++++++-
 5 files changed, 104 insertions(+), 3 deletions(-)

diff --git a/Src/Model/BioTrackerTrackingAlgorithm.cpp b/Src/Model/BioTrackerTrackingAlgorithm.cpp
index 69cc502..e1bb125 100644
--- a/Src/Model/BioTrackerTrackingAlgorithm.cpp
+++ b/Src/Model/BioTrackerTrackingAlgorithm.cpp
@@ -29,7 +29,6 @@ BioTrackerTrackingAlgorithm::BioTrackerTrackingAlgorithm(IController *parent, IM
 
     _lastImage = nullptr;
     _lastFramenumber = -1;
-    start_python();
 }
 
 BioTrackerTrackingAlgorithm::~BioTrackerTrackingAlgorithm()
@@ -75,11 +74,12 @@ void BioTrackerTrackingAlgorithm::stop_python() {
 
 void BioTrackerTrackingAlgorithm::start_python() {
     stop_python();
+    auto model_path = _TrackingParameter->getModelPath().toStdString();
     _python_process = boost::process::child(
             boost::process::search_path("python3"),
             boost::process::args({
                 "-c", "from biotracker import BiotrackerAdapter;"
-                "BiotrackerAdapter('/home/max/tmp/example.multi_instance'"
+                "BiotrackerAdapter('" + model_path + "'"
                 ",verbose=True).run();"
                 }), _python_process_group);
 
@@ -92,6 +92,10 @@ void BioTrackerTrackingAlgorithm::receiveAreaDescriptorUpdate(IModelAreaDescript
 }
 
 void BioTrackerTrackingAlgorithm::receiveParametersChanged() {
+    if (_TrackingParameter->getModelPath() != NULL) {
+        start_python();
+    }
+
     if (_lastFramenumber >= 0 && _lastImage && !_lastImage->empty()) {
         doTracking(_lastImage, _lastFramenumber);
     }
@@ -107,6 +111,11 @@ void BioTrackerTrackingAlgorithm::doTracking(std::shared_ptr<cv::Mat> p_image, u
 		return;
 	}
 
+    // skip frame if model is not yet loaded
+    if (_python_process.has_value()) {
+        return;
+    }
+
 	if (_imageX != p_image->size().width || _imageY != p_image->size().height) {
 		_imageX = p_image->size().width;
 		_imageY = p_image->size().height;
diff --git a/Src/Model/TrackerParameter.h b/Src/Model/TrackerParameter.h
index 40feddd..aa1b870 100644
--- a/Src/Model/TrackerParameter.h
+++ b/Src/Model/TrackerParameter.h
@@ -4,6 +4,7 @@
 
 #include "Interfaces/IModel/IModel.h"
 #include "../Config.h"
+#include <optional>
 
 class TrackerParameter : public IModel
 {
@@ -16,7 +17,15 @@ public:
 		_newSelection = x;
 	}
 
+    void setModelPath(QString x) {
+        _model_path = x;
+        Q_EMIT notifyView();
+    }
+
+    QString getModelPath() { return _model_path; };
+
 private:
+    QString _model_path;
 
 	std::string _newSelection;
 	Config *_cfg;
diff --git a/Src/View/TrackerParameterView.cpp b/Src/View/TrackerParameterView.cpp
index b9ec3e2..70d0968 100644
--- a/Src/View/TrackerParameterView.cpp
+++ b/Src/View/TrackerParameterView.cpp
@@ -2,12 +2,26 @@
 #include "ui_TrackerParameterView.h"
 
 #include <iostream>
+#include <QFileDialog>
+#include <QString>
 
 TrackerParameterView::TrackerParameterView(QWidget *parent, IController *controller, IModel *model) :
     IViewWidget(parent, controller, model),
     _ui(new Ui::TrackerParameterView)
 {
     _ui->setupUi(this);
+    QObject::connect(_ui->model_path_browse, SIGNAL(clicked()), this, SLOT(on_model_path_browse()));
+}
+
+void TrackerParameterView::on_model_path_browse() {
+    QString dir = QFileDialog::getExistingDirectory(this, tr("Choose Model"),
+                                                "/home",
+                                                QFileDialog::ShowDirsOnly
+                                                | QFileDialog::DontResolveSymlinks);
+    _ui->model_path_line->setText(dir);
+    TrackerParameter *parameter = qobject_cast<TrackerParameter *>(getModel());
+    parameter->setModelPath(dir);
+    Q_EMIT parametersChanged();
 }
 
 TrackerParameterView::~TrackerParameterView()
diff --git a/Src/View/TrackerParameterView.h b/Src/View/TrackerParameterView.h
index edf9c70..d003bb0 100644
--- a/Src/View/TrackerParameterView.h
+++ b/Src/View/TrackerParameterView.h
@@ -21,6 +21,9 @@ public:
     void trackingAreaType(int v);
     void parametersChanged();
 
+private slots:
+    void on_model_path_browse();
+
 private:
     Ui::TrackerParameterView *_ui;
 
diff --git a/Src/View/TrackerParameterView.ui b/Src/View/TrackerParameterView.ui
index 9f810ec..537d68d 100644
--- a/Src/View/TrackerParameterView.ui
+++ b/Src/View/TrackerParameterView.ui
@@ -65,6 +65,52 @@
        <property name="topMargin">
         <number>0</number>
        </property>
+       <item>
+        <widget class="QFrame" name="frame">
+         <property name="frameShape">
+          <enum>QFrame::StyledPanel</enum>
+         </property>
+         <property name="frameShadow">
+          <enum>QFrame::Raised</enum>
+         </property>
+         <widget class="QLineEdit" name="model_path_line">
+          <property name="geometry">
+           <rect>
+            <x>60</x>
+            <y>10</y>
+            <width>151</width>
+            <height>23</height>
+           </rect>
+          </property>
+         </widget>
+         <widget class="QLabel" name="model_path_label">
+          <property name="geometry">
+           <rect>
+            <x>10</x>
+            <y>10</y>
+            <width>41</width>
+            <height>20</height>
+           </rect>
+          </property>
+          <property name="text">
+           <string>Model</string>
+          </property>
+         </widget>
+         <widget class="QPushButton" name="model_path_browse">
+          <property name="geometry">
+           <rect>
+            <x>220</x>
+            <y>10</y>
+            <width>61</width>
+            <height>23</height>
+           </rect>
+          </property>
+          <property name="text">
+           <string>Browse</string>
+          </property>
+         </widget>
+        </widget>
+       </item>
       </layout>
      </widget>
     </widget>
@@ -72,5 +118,25 @@
   </layout>
  </widget>
  <resources/>
- <connections/>
+ <connections>
+  <connection>
+   <sender>model_path_browse</sender>
+   <signal>clicked()</signal>
+   <receiver>TrackerParameterView</receiver>
+   <slot>browseSlot()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>268</x>
+     <y>29</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>363</x>
+     <y>31</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+ <slots>
+  <slot>browseSlot()</slot>
+ </slots>
 </ui>
-- 
GitLab