iVS3D v2.0.9
Loading...
Searching...
No Matches
visualSimilarity.h
1#pragma once
2
12#include <QComboBox>
13#include <QCoreApplication>
14#include <QDir>
15#include <QLabel>
16#include <QMap>
17#include <QRegularExpression>
18#include <QSignalBlocker>
19#include <QSizePolicy>
20#include <QSpinBox>
21#include <QTranslator>
22#include <QVBoxLayout>
23#include <QWidget>
24#include <QtConcurrent>
25
26#include <memory>
27#include <vector>
28
29#include <opencv2/core.hpp>
30#include <opencv2/opencv.hpp>
31#include <opencv2/dnn.hpp>
32#include <opencv2/imgcodecs.hpp>
33#include <opencv2/imgproc.hpp>
34#include <opencv2/tracking/tracking_by_matching.hpp>
35
36#include <NeuralNetFactory.h>
37
38#include "ibase.h"
39#include "iselection.h"
40#include "reader.h"
41
42#define RESSOURCE_PATH "/plugins/resources/neural_network_models/"
43#define MEM_THRESEHOLD 0.5f
44#define MAX_BATCH 100
45#define NN_STD {0.229, 0.224, 0.225}
46#define NN_MEAN {0.485, 0.456, 0.406}
47
48// visuals
49#define DESCRIPTION_STYLE "color: rgb(58, 58, 58); border-left: 6px solid rgb(58, 58, 58); border-top-right-radius: 5px; border-bottom-right-radius: 5px; background-color: lightblue;"
50#define UI_FRAMEREDUCTION_NAME QObject::tr("Select one frame every K frames")
51#define UI_FRAMEREDUCTION_DESC QObject::tr("Reduces the amount of selected frames by the factor K.")
52#define UI_NNNAME_NAME QObject::tr("Selected Neural Network")
53#define UI_NNNAME_DESC QObject::tr("The drop-down shows all files in plugins/resources/neural_network_models matching ImageEmbedding*.onnx")
54
55// json settings
56#define FRAMEREDUCTION_JSON_NAME "K"
57#define NNNAME_JSON_NAME "NN-Name"
58
59// log file
60#define LF_TIMER_KEYFRAMES "keyframeSelection"
61#define LF_TTHRESHOLD "tDistThreshold"
62#define LF_RESULT_INFO_TAG "result_info"
63#define LF_COMPUTE_INFO_TAG "compute_info"
64#define LF_SELECTION_INFO_TAG "selection_info"
65#define LF_TIMER_NN "NN feeding timer"
66#define LF_TIMER_KMEANS "kMeans timer"
67#define LF_TIMER_BUFFER "safe buffer timer"
68
81{
82 Q_OBJECT
83 Q_PLUGIN_METADATA(IID "iVS3D.IBase")
84 Q_INTERFACES(PLUG::IBase PLUG::ISelection)
85
86public:
88 ~VisualSimilarity() override = default;
89
90 // IBase
92 QString getName() const override;
93 QMap<QString, QVariant> getSettings() const override;
95 const QMap<QString, QVariant>& settings) override;
97 const PLUG::InputData& input) override;
98 void onCudaChanged(bool enabled) override;
99
100 // ISelection
101 PLUG::SelectionResult selectImages(const PLUG::SelectionData& data,
102 volatile bool& cancelFlag) override;
103
104signals:
105 void syncSettingsWidget(int frameReduction,
106 QString nnFileName,
107 QStringList availableNns,
108 int maxFrames);
109
110
111private slots:
112 void slot_selectedNNChanged(const QString& nnName);
113 void slot_frameReductionChanged(int value);
114
115private:
116 std::unique_ptr<QWidget> createSettingsWidget();
117 PLUG::SelectionResult sampleImages(const std::vector<uint>& imageList,
118 std::shared_ptr<LogFileParent> logFile,
119 volatile bool& cancelFlag);
120 bool bufferLookup(uint idx, cv::Mat* out) const;
121 cv::Mat getFeatureVector(const cv::Mat& totalVector, int position) const;
122 QStringList collectNns(const QString& path) const;
123 PLUG::Error mapInferenceError(const NN::NeuralError& err) const;
124
125 cv::Mat m_bufferMat = cv::Mat();
126 std::vector<uint> m_bufferUsedIdx;
127 Reader* m_reader = nullptr;
128 bool m_useCuda = false;
129
130 // parameters
131 int m_frameReduction = 30;
132 const QRegularExpression m_nnNameFormat = QRegularExpression("^(ImageEmbedding)\\w+.onnx$");
133 QString m_nnFileName = "ImageEmbedding_NAME_DIMENSION_WIDTHxHEIGHT.onnx";
134
135 // widgets
136 QSpinBox* m_frameReductionInput = nullptr;
137 QComboBox* m_nnNameInput = nullptr;
138
139 NN::NeuralNetPtr m_neuralNet = nullptr;
140 int m_featureDims = -1;
141};
Factory class for creating NeuralNet instances.
Represents an error that occurred in the neural network module and contains the error type and messag...
Definition NeuralError.h:48
Represents an error with a code and a message. The message is intended for display to the user.
Definition ierror.h:28
The IBase interface provides a base class for all plugin interfaces in iVS3D. It inherits from QObjec...
Definition ibase.h:102
Interface for keyframe/image-selection plugins in iVS3D.
Definition iselection.h:53
The Reader interface defines functions which are used for reading and parsing the import.
Definition reader.h:23
Definition visualSimilarity.h:81
PLUG::SettingsWidgetResult getSettingsWidget() override
getSettingsWidget creates and returns a settings QWidget for this plugin.
Definition visualSimilarity.cpp:34
QMap< QString, QVariant > getSettings() const override
getSettings retrieves the current settings of the plugin as a map of key-value pairs....
Definition visualSimilarity.cpp:51
void onCudaChanged(bool enabled) override
onCudaChanged is called when the CUDA usage setting is changed in iVS3D.
Definition visualSimilarity.cpp:120
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 visualSimilarity.cpp:59
PLUG::SelectionResult selectImages(const PLUG::SelectionData &data, volatile bool &cancelFlag) override
Selects images based on the provided selection data.
Definition visualSimilarity.cpp:130
QString getName() const override
getName returns the name of the plugin which will be displayed in the iVS3D interface.
Definition visualSimilarity.cpp:46
PLUG::InputLoadedResult onInputLoaded(const PLUG::InputData &input) override
onInputLoaded is called when a new input video or image set is loaded.
Definition visualSimilarity.cpp:93
std::shared_ptr< NeuralNet > NeuralNetPtr
Smart pointer type for managing NeuralNet instances.
Definition NeuralNet.h:121
tl::expected< void, Error > InputLoadedResult
Type alias for the result of handling an input loaded event, which can be either a successful void re...
Definition ibase.h:58
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 to encapsulate data related to the input loaded event in iVS3D.
Definition ibase.h:71
Data available for image selection in ISelection plugins.
Definition iselection.h:31