iVS3D v2.0.9
Loading...
Searching...
No Matches
segplugin.h
1#pragma once
2
3#include <ModelManager.h>
4#include <NeuralNetFactory.h>
5
6#include <QCoreApplication>
7#include <QObject>
8#include <QTranslator>
9#include <memory>
10#include <optional>
11
12#include "ibase.h"
13#include "imask.h"
14#include "ipreview.h"
15
17
19 Q_OBJECT
20 Q_PLUGIN_METADATA(IID "iVS3D.IMask")
21 Q_PLUGIN_METADATA(IID "iVS3D.IPreview")
22 Q_PLUGIN_METADATA(IID "iVS3D.IBase")
24
25 public:
26 using PLUG::IBase::IBase;
27
29
30 // IBase interface
31 QString getName() const override { return tr("Segmentation Masks"); }
33 QMap<QString, QVariant> getSettings() const override;
34 QString getSettingsString() const override;
36 const QMap<QString, QVariant>& settings) override;
37 void onCudaChanged(bool enabled) override;
38 void deactivate() override;
39
40 // IMask interface
41 PLUG::MaskResult generateMask(const PLUG::MaskData& data) override;
42
43 // IPreview interface
44 VIS::VisualizationResult generatePreview(const PLUG::PreviewData& data) override;
45
46 signals:
47 void syncSettingsWidget(QString selectedModelName, float overlayAlpha);
48
49 private slots:
50 void onModelChanged(const QString& modelName, bool isValid);
51 void onClassSelectionChanged(const QVector<uint>& selectedClassIds);
52 void onOverlayAlphaChanged(float value);
53
54 private:
61 std::optional<PLUG::Error> ensureModelReady();
62
63 tl::expected<NN::Tensor, NN::NeuralError> runInference(
64 const cv::Mat& image);
65 tl::expected<cv::Mat, NN::NeuralError> runColorization(
66 const NN::Tensor& inferenceTensor);
67 tl::expected<cv::Mat, NN::NeuralError> runMasking(
68 const NN::Tensor& inferenceTensor);
69
70 MCFG::ModelManager m_modelManager{QCoreApplication::applicationDirPath() +
71 "/plugins/resources/neural_network_models"};
72
73 bool m_useCuda = false;
74 float m_overlayAlpha = 0.5f;
75
76 QString m_currentModelName;
77 NN::NeuralNetPtr m_currentModel;
78 std::unique_ptr<QTranslator> m_segmentationTranslator;
79
80 // cache for the last inference result (if any and valid)
82 uint idx;
83 cv::Mat image;
84 NN::Tensor inferenceTensor;
85 cv::Mat colorizedImage =
86 cv::Mat(); // optional: can be empty if invalid
87 cv::Mat maskImage = cv::Mat(); // optional: can be empty if invalid
88 long inferenceDurationMs = 0; // Time taken for inference
89 };
90 std::optional<SegmentationCache> m_cache;
91};
Discovery, validation, activation, and state tracking for model configs.
Factory class for creating NeuralNet instances.
Manages model configurations in a directory and exposes UI-friendly state.
Definition ModelManager.h:43
A Tensor represents a N-dimensional array containing elements of the same type. Can be used as input ...
Definition Tensor.h:201
The IBase interface provides a base class for all plugin interfaces in iVS3D. It inherits from QObjec...
Definition ibase.h:102
Interface for mask generation plugins in iVS3D.
Definition imask.h:46
The IPreview interface defines the contract for preview plugins in iVS3D. Plugins implementing this i...
Definition ipreview.h:41
Definition segplugin.h:18
PLUG::ApplySettingsResult applySettings(const QMap< QString, QVariant > &settings) override
applySettings applies the provided settings to the plugin. This method is used to restore plugin conf...
Definition segplugin.cpp:124
QString getSettingsString() const override
getSettingsString is a helper method that converts the plugin settings into a human-readable string f...
Definition segplugin.cpp:120
QString getName() const override
getName returns the name of the plugin which will be displayed in the iVS3D interface.
Definition segplugin.h:31
std::optional< PLUG::Error > ensureModelReady()
Ensures the model manager has an active, ready model and loads the neural network....
Definition segplugin.cpp:22
PLUG::MaskResult generateMask(const PLUG::MaskData &data) override
Generates a binary mask for the given image data.
Definition segplugin.cpp:150
void onCudaChanged(bool enabled) override
onCudaChanged is called when the CUDA usage setting is changed in iVS3D.
Definition segplugin.cpp:304
VIS::VisualizationResult generatePreview(const PLUG::PreviewData &data) override
Generates a preview visualization based on the provided data. This function is executed asynchronousl...
Definition segplugin.cpp:178
PLUG::SettingsWidgetResult getSettingsWidget() override
getSettingsWidget creates and returns a settings QWidget for this plugin.
Definition segplugin.cpp:65
QMap< QString, QVariant > getSettings() const override
getSettings retrieves the current settings of the plugin as a map of key-value pairs....
Definition segplugin.cpp:109
void deactivate() override
deactivate is called when the plugin is deactivated in iVS3D. Plugins can override this method to per...
Definition segplugin.cpp:313
Definition segsettingswidget.h:10
std::shared_ptr< NeuralNet > NeuralNetPtr
Smart pointer type for managing NeuralNet instances.
Definition NeuralNet.h:121
tl::expected< void, Error > ApplySettingsResult
Type alias for the result of applying settings to a plugin, which can be either a successful void res...
Definition ibase.h:51
tl::expected< std::unique_ptr< QWidget >, Error > SettingsWidgetResult
Type alias for the result of a settings widget creation operation, which can be either a successful u...
Definition ibase.h:44
Struct containing data required to generate a binary mask. The image is resized to the working resolu...
Definition imask.h:26
Struct containing data required to generate a preview. The image is resized to the working resolution...
Definition ipreview.h:16
Definition segplugin.h:81