-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmod.cpp
More file actions
227 lines (184 loc) · 6.34 KB
/
mmod.cpp
File metadata and controls
227 lines (184 loc) · 6.34 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
// source: http://dlib.net/dnn_mmod_ex.cpp.html
#include "mmod.h"
#include "widget.h"
#include <AnnotatorLib/Annotation.h>
#include <AnnotatorLib/Commands/NewAnnotation.h>
#include <AnnotatorLib/Frame.h>
#include <AnnotatorLib/Session.h>
#include <dlib/data_io.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_processing.h>
#include <dlib/opencv/cv_image.h>
#include <dlib/svm_threaded.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <ctype.h>
#include <iostream>
#include <memory>
#include <chrono>
#include <thread>
using namespace Annotator::Plugins;
Annotator::Plugins::MMOD::MMOD() { widget.setMMOD(this); }
MMOD::~MMOD() {}
QString MMOD::getName() { return "MMOD"; }
QWidget *MMOD::getWidget() { return &widget; }
bool MMOD::setFrame(shared_ptr<Frame> frame, cv::Mat image) {
this->lastFrame = this->frame;
this->frame = frame;
this->frameImg = image;
return lastFrame != frame;
}
// first call
void MMOD::setObject(shared_ptr<Object> object) {
if (object != this->object) {
this->object = object;
widget.setObjectPixmap(getImgCrop(object->getFirstAnnotation(), 96));
}
}
shared_ptr<Object> MMOD::getObject() const { return object; }
void MMOD::setLastAnnotation(shared_ptr<Annotation> /*annotation*/) {}
std::vector<shared_ptr<Commands::Command>> MMOD::getCommands() {
std::vector<shared_ptr<Commands::Command>> commands;
if (object == nullptr || frame == nullptr || lastFrame == nullptr ||
lastFrame == frame)
return commands;
try {
cv::Rect res = findObject();
if (res.width > 0 && res.height > 0) {
int x = res.x;
int y = res.y;
int w = res.width;
int h = res.height;
shared_ptr<Commands::NewAnnotation> nA =
std::make_shared<Commands::NewAnnotation>(project->getSession(),
this->object, this->frame,
x, y, w, h, 0.9f);
commands.push_back(nA);
}
} catch (std::exception &e) {
}
return commands;
}
void MMOD::train() {
if (!object) return;
trainThread = std::thread([this] { this->trainWorker(); });
}
void MMOD::stop() {
stopTraining = true;
trainThread.join();
}
void MMOD::getImagesTrain() {
assert(object);
this->images_train.clear();
this->boxes_train.clear();
this->object->getAnnotations();
for (auto annotation : this->object->getAnnotations()) {
std::shared_ptr<AnnotatorLib::Annotation> a = annotation.second.lock();
cv::Mat image =
project->getImageSet()->getImage(a->getFrame()->getFrameNumber());
dlib::matrix<dlib::rgb_pixel> dlibImage;
dlib::assign_image(dlibImage, dlib::cv_image<dlib::rgb_pixel>(image));
this->images_train.push_back(dlibImage);
std::vector<dlib::mmod_rect> rects;
long x1 = std::max(0L, (long)a->getX());
long y1 = std::max(0L, (long)a->getY());
long x2 =
std::min((long)image.cols - 1L, (long)a->getX() + (long)a->getWidth());
long y2 =
std::min((long)image.rows - 1L, (long)a->getY() + long(a->getHeight()));
dlib::rectangle rect(x1, y1, x2, y2);
rects.push_back(dlib::mmod_rect(rect));
this->boxes_train.push_back(rects);
}
}
void MMOD::loadNet(std::string file) {
try {
dlib::deserialize(file) >> net;
} catch (...) {
}
}
void MMOD::saveNet(std::string file) {
try {
net.clean();
dlib::serialize(file) << net;
} catch (...) {
}
}
void MMOD::trainWorker() {
stopTraining = false;
widget.setProgress(10);
getImagesTrain();
widget.setProgress(20);
dlib::mmod_options options(boxes_train, 20 * 20, 10 * 10);//
net = net_type(options);
dlib::dnn_trainer<net_type> trainer(net);
trainer.set_learning_rate(0.1);
trainer.be_verbose();
trainer.set_synchronization_file("mmod_sync", std::chrono::minutes(5));
trainer.set_iterations_without_progress_threshold(300);
widget.setProgress(30);
std::vector<dlib::matrix<dlib::rgb_pixel>> mini_batch_samples;
std::vector<std::vector<dlib::mmod_rect>> mini_batch_labels;
dlib::random_cropper cropper;
cropper.set_chip_dims(200, 200);
cropper.set_min_object_size(0.2);
dlib::rand rnd;
// Run the trainer until the learning rate gets small. This will probably
// take several
// hours.
while (!stopTraining && trainer.get_learning_rate() >= 1e-4) {
try {
cropper(50, images_train, boxes_train, mini_batch_samples,
mini_batch_labels);
// We can also randomly jitter the colors and that often helps a detector
// generalize better to new images.
for (auto &&img : mini_batch_samples) disturb_colors(img, rnd);
trainer.train_one_step(mini_batch_samples, mini_batch_labels);
} catch (dlib::impossible_labeling_error &e) {
std::cout << this->getName().toStdString() << ": " << e.what()
<< std::endl;
}
}
// wait for training threads to stop
trainer.get_net();
widget.setProgress(50);
widget.setProgress(0);
}
cv::Rect MMOD::findObject() {
dlib::matrix<dlib::rgb_pixel> dlibImage;
dlib::assign_image(dlibImage,
dlib::cv_image<dlib::rgb_pixel>(this->frameImg));
dlib::pyramid_up(dlibImage);
std::vector<dlib::mmod_rect> dets = net(dlibImage);
if (dets.size() < 1) return cv::Rect();
dlib::rectangle found = dets[0];
return cv::Rect(found.left(), found.top(), found.width(), found.height());
}
QPixmap MMOD::getImgCrop(shared_ptr<AnnotatorLib::Annotation> annotation,
int size) const {
if (annotation == nullptr) return QPixmap();
cv::Mat cropped = getImg(annotation);
cropped.convertTo(cropped, CV_8U);
cv::cvtColor(cropped, cropped, CV_BGR2RGB);
QImage img((const unsigned char *)(cropped.data), cropped.cols, cropped.rows,
cropped.step, QImage::Format_RGB888);
QPixmap pim = QPixmap::fromImage(img);
pim = pim.scaledToHeight(size);
return pim;
}
cv::Mat MMOD::getImg(shared_ptr<Annotation> annotation) const {
cv::Mat tmp = project->getImageSet()->getImage(
annotation->getFrame()->getFrameNumber());
float x = std::max(annotation->getX(), 0.f);
float y = std::max(annotation->getY(), 0.f);
float w = std::min(annotation->getWidth(), tmp.cols - x);
float h = std::min(annotation->getHeight(), tmp.rows - y);
cv::Rect rect(x, y, w, h);
cv::Mat cropped;
try {
tmp(rect).copyTo(cropped);
} catch (cv::Exception &e) {
std::cout << e.what();
}
return cropped;
}