diff --git a/openbr/plugins/slidingwindow.cpp b/openbr/plugins/slidingwindow.cpp index f5d88ef..65b174f 100644 --- a/openbr/plugins/slidingwindow.cpp +++ b/openbr/plugins/slidingwindow.cpp @@ -40,21 +40,13 @@ class SlidingWindowTransform : public Transform { Q_OBJECT Q_PROPERTY(br::Transform *transform READ get_transform WRITE set_transform RESET reset_transform STORED false) - Q_PROPERTY(int minSize READ get_minSize WRITE set_minSize RESET reset_minSize STORED false) Q_PROPERTY(int stepSize READ get_stepSize WRITE set_stepSize RESET reset_stepSize STORED false) Q_PROPERTY(bool takeFirst READ get_takeFirst WRITE set_takeFirst RESET reset_takeFirst STORED false) - Q_PROPERTY(bool negSamples READ get_negSamples WRITE set_negSamples RESET reset_negSamples STORED false) - Q_PROPERTY(int negToPosRatio READ get_negToPosRatio WRITE set_negToPosRatio RESET reset_negToPosRatio STORED false) - Q_PROPERTY(double maxOverlap READ get_maxOverlap WRITE set_maxOverlap RESET reset_maxOverlap STORED false) Q_PROPERTY(int windowWidth READ get_windowWidth WRITE set_windowWidth RESET reset_windowWidth STORED false) Q_PROPERTY(float threshold READ get_threshold WRITE set_threshold RESET reset_threshold STORED false) BR_PROPERTY(br::Transform *, transform, NULL) - BR_PROPERTY(int, minSize, 8) BR_PROPERTY(int, stepSize, 1) BR_PROPERTY(bool, takeFirst, false) - BR_PROPERTY(bool, negSamples, true) - BR_PROPERTY(int, negToPosRatio, 1) - BR_PROPERTY(double, maxOverlap, 0) BR_PROPERTY(int, windowWidth, 24) BR_PROPERTY(float, threshold, 0) @@ -65,11 +57,84 @@ private: void train(const TemplateList &data) { - // only calculate if the work hasn't been done - aspectRatio = data.first().file.get("aspectRatio", -1); + float aspectRatio = data.first().file.get("aspectRatio", -1); if (aspectRatio == -1) aspectRatio = getAspectRatio(data); windowHeight = (int) qRound((float) windowWidth / aspectRatio); + if (transform->trainable) { + transform->train(data); + } + } + + void project(const Template &src, Template &dst) const + { + dst = src; + // no need to slide a window over ground truth data + if (src.file.getBool("Train", false)) return; + + dst.file.clearRects(); + float scale = src.file.get("scale", 1); + Template windowTemplate(src.file, src); + for (double y = 0; y + windowHeight < src.m().rows; y += stepSize) { + for (double x = 0; x + windowWidth < src.m().cols; x += stepSize) { + Mat windowMat(src, Rect(x, y, windowWidth, windowHeight)); + windowTemplate.replace(0,windowMat); + Template detect; + transform->project(windowTemplate, detect); + float conf = detect.m().at(0); + + // the result will be in the Label + if (conf > threshold) { + dst.file.appendRect(QRectF((float) x * scale, (float) y * scale, (float) windowWidth * scale, (float) windowHeight * scale)); + QList confidences = dst.file.getList("Confidences", QList()); + confidences.append(conf); + dst.file.setList("Confidences", confidences); + if (takeFirst) + return; + } + } + } + } +}; + +BR_REGISTER(Transform, SlidingWindowTransform) + +/*! + * \ingroup transforms + * \brief . + * \author Austin Blanton \cite imaus10 + */ +class BuildScalesTransform : public Transform +{ + Q_OBJECT + Q_PROPERTY(br::Transform *transform READ get_transform WRITE set_transform RESET reset_transform STORED false) + Q_PROPERTY(double scaleFactor READ get_scaleFactor WRITE set_scaleFactor RESET reset_scaleFactor STORED false) + Q_PROPERTY(bool takeLargestScale READ get_takeLargestScale WRITE set_takeLargestScale RESET reset_takeLargestScale STORED false) + Q_PROPERTY(int windowWidth READ get_windowWidth WRITE set_windowWidth RESET reset_windowWidth STORED false) + Q_PROPERTY(int negToPosRatio READ get_negToPosRatio WRITE set_negToPosRatio RESET reset_negToPosRatio STORED false) + Q_PROPERTY(int minSize READ get_minSize WRITE set_minSize RESET reset_minSize STORED false) + Q_PROPERTY(double maxOverlap READ get_maxOverlap WRITE set_maxOverlap RESET reset_maxOverlap STORED false) + Q_PROPERTY(bool negSamples READ get_negSamples WRITE set_negSamples RESET reset_negSamples STORED false) + BR_PROPERTY(br::Transform *, transform, NULL) + BR_PROPERTY(double, scaleFactor, 0.75) + BR_PROPERTY(bool, takeLargestScale, false) + BR_PROPERTY(int, windowWidth, 24) + BR_PROPERTY(int, negToPosRatio, 1) + BR_PROPERTY(int, minSize, 8) + BR_PROPERTY(double, maxOverlap, 0) + BR_PROPERTY(bool, negSamples, true) + +public: + BuildScalesTransform() : Transform(false, true) {} +private: + int windowHeight; + + void train(const TemplateList &_data) + { + TemplateList data = _data; // have to make a copy b/c data is const + aspectRatio = getAspectRatio(data); + data.first().file.set("aspectRatio", aspectRatio); + windowHeight = (int) qRound((float) windowWidth / aspectRatio); if (transform->trainable) { TemplateList full; @@ -142,71 +207,6 @@ private: return false; } - void project(const Template &src, Template &dst) const - { - dst = src; - // no need to slide a window over ground truth data - if (src.file.getBool("Train", false)) return; - - dst.file.clearRects(); - int scale = src.file.get("scale", 1); - Template windowTemplate(src.file, src); - for (double y = 0; y + windowHeight < src.m().rows; y += stepSize) { - for (double x = 0; x + windowWidth < src.m().cols; x += stepSize) { - Mat windowMat(src, Rect(x, y, windowWidth, windowHeight)); - windowTemplate.replace(0,windowMat); - Template detect; - transform->project(windowTemplate, detect); - float conf = detect.m().at(0); - - // the result will be in the Label - if (conf > threshold) { - dst.file.appendRect(QRectF((float) x * scale, (float) y * scale, (float) windowWidth * scale, (float) windowHeight * scale)); - QList confidences = dst.file.getList("Confidences", QList()); - confidences.append(conf); - dst.file.setList("Confidences", confidences); - if (takeFirst) - return; - } - } - } - } - - float aspectRatio; -}; - -BR_REGISTER(Transform, SlidingWindowTransform) - -/*! - * \ingroup transforms - * \brief . - * \author Austin Blanton \cite imaus10 - */ -class BuildScalesTransform : public Transform -{ - Q_OBJECT - Q_PROPERTY(br::Transform *transform READ get_transform WRITE set_transform RESET reset_transform STORED false) - Q_PROPERTY(double scaleFactor READ get_scaleFactor WRITE set_scaleFactor RESET reset_scaleFactor STORED false) - Q_PROPERTY(bool takeLargestScale READ get_takeLargestScale WRITE set_takeLargestScale RESET reset_takeLargestScale STORED false) - Q_PROPERTY(int windowWidth READ get_windowWidth WRITE set_windowWidth RESET reset_windowWidth STORED false) - BR_PROPERTY(br::Transform *, transform, NULL) - BR_PROPERTY(double, scaleFactor, 0.75) - BR_PROPERTY(bool, takeLargestScale, false) - BR_PROPERTY(int, windowWidth, 24) - -public: - BuildScalesTransform() : Transform(false, true) {} -private: - - void train(const TemplateList &data) - { - aspectRatio = getAspectRatio(data); - // have to make a copy b/c data is const - TemplateList cp = data; - cp.first().file.set("aspectRatio", aspectRatio); - if (transform->trainable) - transform->train(cp); - } void project(const Template &src, Template &dst) const {