-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGui.cpp
More file actions
312 lines (255 loc) · 12.3 KB
/
Gui.cpp
File metadata and controls
312 lines (255 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "../Gui.h"
#include <vector>
#include <boost/filesystem.hpp>
#include <QAction>
#include <QFileDialog>
#include <QShortcut>
#include <QKeySequence>
#include <QListWidget>
#include "opencv2/highgui/highgui.hpp"
#include "biotracker/serialization/SerializationData.h"
#include <cereal/types/polymorphic.hpp>
#include <cereal/archives/json.hpp>
#include <cereal/types/vector.hpp>
namespace BioTracker {
namespace Gui {
Gui::Gui()
: QObject()
, m_biotracker()
, m_mainWindow(m_biotracker) {
initConnects();
m_mainWindow.show();
}
void Gui::initConnects() {
// File -> Open Video
QObject::connect(m_mainWindow.getUi().actionOpen_Video, &QAction::triggered,
this, &Gui::browseVideo);
QObject::connect(m_mainWindow.getUi().actionOpen_Picture, &QAction::triggered,
this, &Gui::browsePictures);
QObject::connect(m_mainWindow.getUi().actionOpen_Camera, &QAction::triggered,
this, &Gui::browseCameras);
QObject::connect(m_mainWindow.getUi().actionLoad_Tracker, &QAction::triggered,
this, &Gui::loadTracker);
QObject::connect(m_mainWindow.getUi().actionLoad_tracking_data,
&QAction::triggered, this, &Gui::loadTrackingData);
QObject::connect(m_mainWindow.getUi().actionSave_tracking_data,
&QAction::triggered, this, &Gui::storeTrackingData);
QObject::connect(m_mainWindow.getUi().actionQuit, &QAction::triggered, this,
&Gui::exit);
QObject::connect(&m_biotracker, &Core::BioTrackerApp::frameCalculated,
m_mainWindow.getVideoControl(), &VideoControlWidget::frameCalculated);
QObject::connect(&m_biotracker, &Core::BioTrackerApp::frameCalculated,
&m_mainWindow, &MainWindow::frameCalculated);
QObject::connect(&m_biotracker, &Core::BioTrackerApp::trackerSelected,
&m_mainWindow, &MainWindow::trackerSelected);
QObject::connect(&m_biotracker, &Core::BioTrackerApp::notify,
&m_mainWindow.getNotification(), &NotificationWidget::notify);
QObject::connect(&m_biotracker, &Core::BioTrackerApp::fileOpened,
m_mainWindow.getVideoControl(), &VideoControlWidget::fileOpened);
}
void Gui::browseVideo() {
static const QString videoFilter("Video files (*.avi *.wmv *.mp4 *.mkv *.mov)");
std::vector<boost::filesystem::path> files;
const QString filename = QFileDialog::getOpenFileName(&m_mainWindow,
"Open video", "", videoFilter);
if (!filename.isEmpty()) {
m_biotracker.openVideo(boost::filesystem::path(filename.toStdString()));
m_mainWindow.getVideoControl()->updateWidgets();
}
}
void Gui::browsePictures() {
static const QString imageFilter(
"image files (*.png *.jpg *.jpeg *.gif *.bmp *.jpe *.ppm *.tiff *.tif *.sr *.ras *.pbm *.pgm *.jp2 *.dib)");
std::vector<boost::filesystem::path> files;
for (QString const &path : QFileDialog::getOpenFileNames(&m_mainWindow,
"Open image files", "", imageFilter)) {
files.push_back(boost::filesystem::path(path.toStdString()));
}
if (!files.empty()) {
m_biotracker.openImages(files);
m_mainWindow.getVideoControl()->updateWidgets();
}
}
void Gui::browseCameras() {
OpenCameraDialog &cameraDialog = m_mainWindow.getOpenCameraDialog();
// Preparing list widget
QListWidget *cameraListWidget = cameraDialog.getUi().cameraList;
cameraListWidget->clear();
BioTracker::Core::Settings settings = m_biotracker.getSettings();
BioTracker::Core::TrackerStatus trackerStatus = m_biotracker.getTrackingThread().getStatus();
// Add Item for each camera
cv::VideoCapture vcap;
boost::optional<uint8_t> mediaTypeOpt = settings.maybeGetValueOfParam<uint8_t>(GuiParam::MEDIA_TYPE);
GuiParam::MediaType mediaType = mediaTypeOpt ? static_cast<GuiParam::MediaType>(*mediaTypeOpt) : GuiParam::MediaType::NoMedia;
boost::optional<int> camIdOpt = settings.maybeGetValueOfParam<int>(CaptureParam::CAP_CAMERA_ID);
int camId = camIdOpt ? *camIdOpt : -1;
// OpenCV does not have an API to list camera devices https://github.com/Itseez/opencv/issues/4269
for (int i = 0; i <= 254; i++) {
QListWidgetItem* item = new QListWidgetItem(QString("Camera ") + QString::number(static_cast<int>(i)));
if (mediaType == GuiParam::MediaType::Camera && camId == i && trackerStatus != BioTracker::Core::TrackerStatus::NothingLoaded) {
item->setText(item->text() + QString(" (in use)"));
item->setFlags(Qt::NoItemFlags);
} else {
vcap = cv::VideoCapture(i);
if (!vcap.isOpened()) {
break;
}
vcap.release();
}
cameraListWidget->addItem(item);
}
if (cameraDialog.exec() == QDialog::Accepted) {
// Getting chosen
int row = cameraListWidget->currentRow();
if (row >= 0) {
m_biotracker.openCamera(row);
m_mainWindow.getVideoControl()->updateWidgets();
}
}
}
void Gui::loadTracker() {
// TODO make this platform dependend (dlls only for windows, so`s only for linux etc)
static const QString trackerFilter("BioTracker algorithms (*.tracker.dll *.tracker.so *.tracker.zmq)");
const QString path = QFileDialog::getOpenFileName(&m_mainWindow, "Load Tracker",
"", trackerFilter);
if (!path.isEmpty()) {
boost::filesystem::path libraryPath(path.toStdString());
m_biotracker.getRegistry().loadTrackerLibrary(libraryPath);
}
}
void Gui::loadTrackingData() {
static const QString trackingdataFilter("Trackingdata Files (*.tdat)");
const QString path = QFileDialog::getOpenFileName(&m_mainWindow, "Load trackingdata", "", trackingdataFilter);
if (!path.isEmpty()) {
std::ifstream is(path.toStdString());
cereal::JSONInputArchive ar(is);
Core::Serialization::Data sdata;
ar(sdata);
if (m_biotracker.getTrackingThread().getTrackerType()){
assert(m_biotracker.getTrackingThread().getTrackerType());
const std::string trackerType =
Core::Registry::getInstance().getStringByType().at(m_biotracker.getTrackingThread().getTrackerType().get());
if (sdata.getType() != trackerType) {
QMessageBox::warning(&m_mainWindow, "Unable to load tracking data",
"Tracker type does not match.");
return;
}
} else {
// try to automatically select the required tracking algorithm
const auto& typeByString = Core::Registry::getInstance().getTypeByString();
const auto it = typeByString.find(sdata.getType());
if (it != typeByString.end()) {
m_biotracker.getTrackingThread().setTrackingAlgorithm(
Core::Registry::getInstance().makeNewTracker(
Core::Registry::getInstance().getTypeByString().at(sdata.getType()), m_biotracker.getSettings()
)
);
} else {
QMessageBox::warning(&m_mainWindow, "Unable to load tracking data",
"Unknown tracker type.");
return;
}
}
const boost::optional<std::vector<std::string>> currentFiles = getOpenFiles();
if (!currentFiles) {
QMessageBox::warning(&m_mainWindow, "Unable to load tracking data",
"No file opened.");
return;
}
const boost::optional<std::string> hash = getFileHash(currentFiles.get().front(), currentFiles.get().size());
if (!hash) {
QMessageBox::warning(&m_mainWindow, "Unable to load tracking data",
"Could not calculate file hash.");
return;
}
if (sdata.getFileSha1Hash() != hash.get()) {
auto buttonClicked = QMessageBox::warning(&m_mainWindow, "Unable to load tracking data",
"File hash does not match", QMessageBox::Ok | QMessageBox::Ignore);
if (buttonClicked == QMessageBox::Ok)
return;
}
m_biotracker.getTrackingThread().loadTrackedObjects(sdata.getTrackedObjects());
}
}
void Gui::storeTrackingData() {
if (!m_biotracker.getTrackingThread().getTrackerType()) {
QMessageBox::warning(&m_mainWindow, "Unable to store tracking data",
"No tracker selected.");
return;
}
QFileDialog dialog(&m_mainWindow, "Save tracking data");
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setDefaultSuffix("tdat");
dialog.setNameFilter("Data Files (*.tdat)");
if (dialog.exec()) {
const QStringList filenames = dialog.selectedFiles();
if (!filenames.empty()) {
assert(m_biotracker.getTrackingThread().getTrackerType());
const std::string trackerType =
Core::Registry::getInstance().getStringByType().at(m_biotracker.getTrackingThread().getTrackerType().get());
const boost::optional<std::vector<std::string>> currentFiles = getOpenFiles();
if (!currentFiles) {
QMessageBox::warning(&m_mainWindow, "Unable to store tracking data",
"No file opened.");
return;
}
const boost::optional<std::string> hash = getFileHash(currentFiles.get().front(), currentFiles.get().size());
if (!hash) {
QMessageBox::warning(&m_mainWindow, "Unable to store tracking data",
"Could not calculate file hash.");
return;
}
Core::Serialization::Data sdata(trackerType, hash.get(), getFilenamesFromPaths(currentFiles.get()),
m_biotracker.getTrackingThread().getTrackedObjects().get());
std::ofstream ostream(filenames.first().toStdString(), std::ios::binary);
cereal::JSONOutputArchive archive(ostream);
archive(std::move(sdata));
}
}
}
boost::optional<std::vector<std::string>> Gui::getOpenFiles() {
Core::Settings settings = m_biotracker.getSettings();
boost::optional<uint8_t> mediaTypeOpt = settings.maybeGetValueOfParam<uint8_t>(GuiParam::MEDIA_TYPE);
GuiParam::MediaType mediaType = mediaTypeOpt ? static_cast<GuiParam::MediaType>(*mediaTypeOpt) : GuiParam::MediaType::NoMedia;
boost::optional<std::vector<std::string>> filename;
switch (mediaType) {
case GuiParam::MediaType::Images:
filename = settings.getValueOfParam<std::vector<std::string>>(PictureParam::PICTURE_FILES);
break;
case GuiParam::MediaType::Video:
filename = std::vector<std::string>();
filename.get().push_back(settings.getValueOfParam<std::string>(CaptureParam::CAP_VIDEO_FILE));
break;
default:
return boost::optional<std::vector<std::string>>();
}
// cap_video_file and picture_file can be set, but empty. therefore, we
// need to check if the parameter actually contains a file name.
if (filename && !filename.get().empty()) return filename;
return boost::optional<std::vector<std::string>>();
}
boost::optional<std::string> Gui::getFileHash(const std::string &filename, const size_t numFiles) const {
QCryptographicHash sha1Generator(QCryptographicHash::Sha1);
QFile file(QString::fromStdString(filename));
if (file.open(QIODevice::ReadOnly)) {
// calculate hash from first 4096 bytes of file
sha1Generator.addData(file.peek(4096));
sha1Generator.addData(QByteArray::number(static_cast<qulonglong>(numFiles)));
return QString(sha1Generator.result().toHex()).toStdString();
}
return boost::optional<std::string>();
}
std::vector<std::string> Gui::getFilenamesFromPaths(const std::vector<std::string> &paths) const {
std::vector<std::string> filenames;
filenames.reserve(paths.size());
for (std::string const& path : paths) {
const QFileInfo fi(QString::fromStdString(path));
filenames.push_back(fi.baseName().toStdString());
}
return filenames;
}
void Gui::exit() {
}
} // Gui
} // BioTracker