Commit b573c3bb23b565df56144329f077355ed2071829
1 parent
b29a6349
Store each detection as its own Template, using the file metadata Detection and Confidence
Showing
1 changed file
with
77 additions
and
49 deletions
openbr/plugins/slidingwindow.cpp
| @@ -68,32 +68,37 @@ private: | @@ -68,32 +68,37 @@ private: | ||
| 68 | 68 | ||
| 69 | void project(const Template &src, Template &dst) const | 69 | void project(const Template &src, Template &dst) const |
| 70 | { | 70 | { |
| 71 | - dst = src; | 71 | + (void)src;(void)dst;qFatal("don't do that"); |
| 72 | + } | ||
| 73 | + | ||
| 74 | + void project(const TemplateList &src, TemplateList &dst) const | ||
| 75 | + { | ||
| 72 | // no need to slide a window over ground truth data | 76 | // no need to slide a window over ground truth data |
| 73 | - if (src.file.getBool("Train", false)) return; | ||
| 74 | - | ||
| 75 | - dst.file.clearRects(); | ||
| 76 | - float scale = src.file.get<float>("scale", 1); | ||
| 77 | - Template windowTemplate(src.file, src); | ||
| 78 | - QList<float> confidences = dst.file.getList<float>("Confidences", QList<float>()); | ||
| 79 | - for (double y = 0; y + windowHeight < src.m().rows; y += stepSize) { | ||
| 80 | - for (double x = 0; x + windowWidth < src.m().cols; x += stepSize) { | ||
| 81 | - Mat windowMat(src, Rect(x, y, windowWidth, windowHeight)); | ||
| 82 | - windowTemplate.replace(0,windowMat); | ||
| 83 | - Template detect; | ||
| 84 | - transform->project(windowTemplate, detect); | ||
| 85 | - float conf = detect.m().at<float>(0); | ||
| 86 | - | ||
| 87 | - // the result will be in the Label | ||
| 88 | - if (conf > threshold) { | ||
| 89 | - dst.file.appendRect(QRectF((float) x * scale, (float) y * scale, (float) windowWidth * scale, (float) windowHeight * scale)); | ||
| 90 | - confidences.append(conf); | ||
| 91 | - if (takeFirst) | ||
| 92 | - return; | 77 | + if (src.first().file.getBool("Train", false)) { |
| 78 | + dst = src; | ||
| 79 | + return; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + foreach (const Template &t, src) { | ||
| 83 | + float scale = t.file.get<float>("scale", 1); | ||
| 84 | + for (double y = 0; y + windowHeight < t.m().rows; y += stepSize) { | ||
| 85 | + for (double x = 0; x + windowWidth < t.m().cols; x += stepSize) { | ||
| 86 | + Mat windowMat(t.m(), Rect(x, y, windowWidth, windowHeight)); | ||
| 87 | + Template detect; | ||
| 88 | + transform->project(Template(t.file, windowMat), detect); | ||
| 89 | + | ||
| 90 | + // the result will be the only value in the Mat | ||
| 91 | + float conf = detect.m().at<float>(0); | ||
| 92 | + if (conf > threshold) { | ||
| 93 | + detect.file.set("Detection", QRectF((float) x * scale, (float) y * scale, (float) windowWidth * scale, (float) windowHeight * scale)); | ||
| 94 | + detect.file.set("Confidence", conf); | ||
| 95 | + dst.append(detect); | ||
| 96 | + if (takeFirst) | ||
| 97 | + return; | ||
| 98 | + } | ||
| 93 | } | 99 | } |
| 94 | } | 100 | } |
| 95 | } | 101 | } |
| 96 | - dst.file.setList<float>("Confidences", confidences); | ||
| 97 | } | 102 | } |
| 98 | 103 | ||
| 99 | void store(QDataStream &stream) const | 104 | void store(QDataStream &stream) const |
| @@ -174,7 +179,7 @@ static TemplateList cropTrainingSamples(const TemplateList &data, const float as | @@ -174,7 +179,7 @@ static TemplateList cropTrainingSamples(const TemplateList &data, const float as | ||
| 174 | 179 | ||
| 175 | /*! | 180 | /*! |
| 176 | * \ingroup transforms | 181 | * \ingroup transforms |
| 177 | - * \brief . | 182 | + * \brief Pass along images at different scales. |
| 178 | * \author Austin Blanton \cite imaus10 | 183 | * \author Austin Blanton \cite imaus10 |
| 179 | */ | 184 | */ |
| 180 | class BuildScalesTransform : public MetaTransform | 185 | class BuildScalesTransform : public MetaTransform |
| @@ -219,25 +224,38 @@ private: | @@ -219,25 +224,38 @@ private: | ||
| 219 | 224 | ||
| 220 | void project(const Template &src, Template &dst) const | 225 | void project(const Template &src, Template &dst) const |
| 221 | { | 226 | { |
| 222 | - dst = src; | 227 | + (void)src;(void)dst;qFatal("please don't"); |
| 228 | + } | ||
| 229 | + | ||
| 230 | + void project(const TemplateList &src, TemplateList &dst) const | ||
| 231 | + { | ||
| 223 | // do not scale images during training | 232 | // do not scale images during training |
| 224 | - if (src.file.getBool("Train", false)) return; | ||
| 225 | - | ||
| 226 | - int rows = src.m().rows; | ||
| 227 | - int cols = src.m().cols; | ||
| 228 | - int windowHeight = (int) qRound((float) windowWidth / aspectRatio); | ||
| 229 | - float startScale; | ||
| 230 | - if ((cols / rows) > aspectRatio) | ||
| 231 | - startScale = qRound((float) rows / (float) windowHeight); | ||
| 232 | - else | ||
| 233 | - startScale = qRound((float) cols / (float) windowWidth); | ||
| 234 | - for (float scale = startScale; scale >= minScale; scale -= (1.0 - scaleFactor)) { | ||
| 235 | - Template scaleImg(src.file, Mat()); | ||
| 236 | - scaleImg.file.set("scale", scale); | ||
| 237 | - resize(src, scaleImg, Size(qRound(cols / scale), qRound(rows / scale))); | ||
| 238 | - transform->project(scaleImg, dst); | ||
| 239 | - if (takeLargestScale && !dst.file.rects().empty()) | ||
| 240 | - return; | 233 | + if (src.first().file.getBool("Train", false)) { |
| 234 | + dst = src; | ||
| 235 | + return; | ||
| 236 | + } | ||
| 237 | + | ||
| 238 | + foreach(const Template &t, src) { | ||
| 239 | + int rows = t.m().rows; | ||
| 240 | + int cols = t.m().cols; | ||
| 241 | + int windowHeight = (int) qRound((float) windowWidth / aspectRatio); | ||
| 242 | + float startScale; | ||
| 243 | + if ((cols / rows) > aspectRatio) | ||
| 244 | + startScale = qRound((float) rows / (float) windowHeight); | ||
| 245 | + else | ||
| 246 | + startScale = qRound((float) cols / (float) windowWidth); | ||
| 247 | + for (float scale = startScale; scale >= minScale; scale -= (1.0 - scaleFactor)) { | ||
| 248 | + Template scaleImg(t.file, Mat()); | ||
| 249 | + scaleImg.file.set("scale", scale); | ||
| 250 | + resize(t.m(), scaleImg.m(), Size(qRound(cols / scale), qRound(rows / scale))); | ||
| 251 | + TemplateList results; | ||
| 252 | + TemplateList input; | ||
| 253 | + input.append(scaleImg); | ||
| 254 | + transform->project(input, results); | ||
| 255 | + dst.append(results); | ||
| 256 | + if (takeLargestScale && !dst.empty()) | ||
| 257 | + return; | ||
| 258 | + } | ||
| 241 | } | 259 | } |
| 242 | } | 260 | } |
| 243 | 261 | ||
| @@ -328,12 +346,19 @@ private: | @@ -328,12 +346,19 @@ private: | ||
| 328 | 346 | ||
| 329 | void project(const Template &src, Template &dst) const | 347 | void project(const Template &src, Template &dst) const |
| 330 | { | 348 | { |
| 331 | - dst = src; | ||
| 332 | - if (!dst.file.contains("Confidences")) | ||
| 333 | - return; | 349 | + (void)src;(void)dst;qFatal("nope"); |
| 350 | + } | ||
| 334 | 351 | ||
| 335 | - //Compute overlap between rectangles and create discrete Laplacian matrix | ||
| 336 | - QList<Rect> rects = OpenCVUtils::toRects(src.file.rects()); | 352 | + void project(const TemplateList &src, TemplateList &dst) const |
| 353 | + { | ||
| 354 | + QList<Rect> rects; | ||
| 355 | + QList<float> confidences; | ||
| 356 | + foreach (const Template &t, src) { | ||
| 357 | + rects.append(OpenCVUtils::toRect(t.file.get<QRectF>("Detection"))); | ||
| 358 | + confidences.append(t.file.get<float>("Confidence")); | ||
| 359 | + } | ||
| 360 | + | ||
| 361 | + // Compute overlap between rectangles and create discrete Laplacian matrix | ||
| 337 | int n = rects.size(); | 362 | int n = rects.size(); |
| 338 | if (n == 0) | 363 | if (n == 0) |
| 339 | return; | 364 | return; |
| @@ -397,7 +422,6 @@ private: | @@ -397,7 +422,6 @@ private: | ||
| 397 | cnts[i] = 0; | 422 | cnts[i] = 0; |
| 398 | } | 423 | } |
| 399 | 424 | ||
| 400 | - QList<float> confidences = dst.file.getList<float>("Confidences"); | ||
| 401 | for (int i = 0; i < n; i++) { | 425 | for (int i = 0; i < n; i++) { |
| 402 | mx = 0.0; | 426 | mx = 0.0; |
| 403 | mxIdx = -1; | 427 | mxIdx = -1; |
| @@ -432,8 +456,12 @@ private: | @@ -432,8 +456,12 @@ private: | ||
| 432 | } | 456 | } |
| 433 | } | 457 | } |
| 434 | 458 | ||
| 435 | - dst.file.setRects(consolidatedRects); | ||
| 436 | - dst.file.setList<float>("Confidences", consolidatedConfidences); | 459 | + for (int i=0; i<consolidatedRects.size(); i++) { |
| 460 | + Template t(src.first().file); | ||
| 461 | + t.file.set("Detection", OpenCVUtils::fromRect(consolidatedRects.at(i))); | ||
| 462 | + t.file.set("Confidence", consolidatedConfidences.at(i)); | ||
| 463 | + dst.append(t); | ||
| 464 | + } | ||
| 437 | } | 465 | } |
| 438 | }; | 466 | }; |
| 439 | 467 |