Commit 3836f4a29f23c9c93891bfd2d1f33861b673f1fc
1 parent
08179e6d
Ready for groupRectangles refactor
Showing
4 changed files
with
22 additions
and
21 deletions
openbr/openbr_plugin.h
| ... | ... | @@ -1422,7 +1422,7 @@ public: |
| 1422 | 1422 | static Classifier *make(QString str, QObject *parent); /*!< \brief Make a classifier from a string. */ |
| 1423 | 1423 | |
| 1424 | 1424 | virtual void train(const QList<cv::Mat> &images, const QList<float> &labels) = 0; |
| 1425 | - virtual float classify(const cv::Mat &image, float *confidence = NULL) const = 0; | |
| 1425 | + virtual float classify(const cv::Mat &image, bool process = true, float *confidence = NULL) const = 0; | |
| 1426 | 1426 | |
| 1427 | 1427 | // Slots for representation |
| 1428 | 1428 | virtual cv::Mat preprocess(const cv::Mat &image) const = 0; | ... | ... |
openbr/plugins/classification/boostedforest.cpp
| ... | ... | @@ -129,18 +129,24 @@ class BoostedForestClassifier : public Classifier |
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - float classify(const Mat &image, float *confidence) const | |
| 132 | + float classify(const Mat &image, bool process, float *confidence) const | |
| 133 | 133 | { |
| 134 | + Mat m; | |
| 135 | + if (process) | |
| 136 | + m = preprocess(image); | |
| 137 | + else | |
| 138 | + m = image; | |
| 139 | + | |
| 134 | 140 | float sum = 0; |
| 135 | 141 | for (int i = 0; i < classifiers.size(); i++) { |
| 136 | 142 | Node *node = classifiers[i]; |
| 137 | 143 | |
| 138 | 144 | while (node->left) { |
| 139 | 145 | if (representation->maxCatCount() > 1) { |
| 140 | - int c = (int)representation->evaluate(image, node->featureIdx); | |
| 146 | + int c = (int)representation->evaluate(m, node->featureIdx); | |
| 141 | 147 | node = (node->subset[c >> 5] & (1 << (c & 31))) ? node->left : node->right; |
| 142 | 148 | } else { |
| 143 | - double val = representation->evaluate(image, node->featureIdx); | |
| 149 | + double val = representation->evaluate(m, node->featureIdx); | |
| 144 | 150 | node = val <= node->threshold ? node->left : node->right; |
| 145 | 151 | } |
| 146 | 152 | } | ... | ... |
openbr/plugins/classification/cascade.cpp
| ... | ... | @@ -152,11 +152,11 @@ class CascadeClassifier : public Classifier |
| 152 | 152 | } |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - float classify(const Mat &image, float *confidence) const | |
| 155 | + float classify(const Mat &image, bool process, float *confidence) const | |
| 156 | 156 | { |
| 157 | 157 | float stageConf = 0.0f; |
| 158 | 158 | foreach (const Classifier *stage, stages) { |
| 159 | - float result = stage->classify(image, &stageConf); | |
| 159 | + float result = stage->classify(image, process, &stageConf); | |
| 160 | 160 | if (result == 0.0f) { |
| 161 | 161 | if (confidence) |
| 162 | 162 | *confidence += stageConf; |
| ... | ... | @@ -219,7 +219,7 @@ private: |
| 219 | 219 | if (!imgHandler.getPos(pos)) |
| 220 | 220 | qFatal("Cannot get another positive sample!"); |
| 221 | 221 | |
| 222 | - if (classify(pos, &confidence) > 0.0f) { | |
| 222 | + if (classify(pos, true, &confidence) > 0.0f) { | |
| 223 | 223 | printf("POS current samples: %d\r", images.size()); |
| 224 | 224 | images.append(pos); |
| 225 | 225 | labels.append(1.0f); |
| ... | ... | @@ -235,7 +235,7 @@ private: |
| 235 | 235 | if (!imgHandler.getNeg(neg)) |
| 236 | 236 | qFatal("Cannot get another negative sample!"); |
| 237 | 237 | |
| 238 | - if (classify(neg, &confidence) > 0.0f) { | |
| 238 | + if (classify(neg, true, &confidence) > 0.0f) { | |
| 239 | 239 | printf("NEG current samples: %d\r", images.size() - posCount); |
| 240 | 240 | images.append(neg); |
| 241 | 241 | labels.append(0.0f); | ... | ... |
openbr/plugins/imgproc/slidingwindow.cpp
| ... | ... | @@ -53,12 +53,6 @@ class SlidingWindowTransform : public MetaTransform |
| 53 | 53 | |
| 54 | 54 | BR_PROPERTY(QString, model, "") |
| 55 | 55 | |
| 56 | - void init() | |
| 57 | - { | |
| 58 | - QDataStream stream; | |
| 59 | - load(stream); | |
| 60 | - } | |
| 61 | - | |
| 62 | 56 | void train(const TemplateList &data) |
| 63 | 57 | { |
| 64 | 58 | classifier->train(data.data(), File::get<float>(data, "Label", -1)); |
| ... | ... | @@ -90,8 +84,7 @@ class SlidingWindowTransform : public MetaTransform |
| 90 | 84 | Mat m; |
| 91 | 85 | OpenCVUtils::cvtUChar(t[i], m); |
| 92 | 86 | std::vector<Rect> rects; |
| 93 | - std::vector<int> rejectLevels; | |
| 94 | - std::vector<double> levelWeights; | |
| 87 | + std::vector<float> confidences; | |
| 95 | 88 | |
| 96 | 89 | if (maxObjectSize.height == 0 || maxObjectSize.width == 0) |
| 97 | 90 | maxObjectSize = m.size(); |
| ... | ... | @@ -123,21 +116,23 @@ class SlidingWindowTransform : public MetaTransform |
| 123 | 116 | for (int x = 0; x < processingRectSize.width; x += step) { |
| 124 | 117 | Mat window = repImage(Rect(Point(x, y), Size(originalWindowSize.width + dx, originalWindowSize.height + dy))).clone(); |
| 125 | 118 | |
| 126 | - float gypWeight; | |
| 127 | - int result = classifier->classify(window, &gypWeight); | |
| 119 | + float confidence; | |
| 120 | + int result = classifier->classify(window, false, &confidence); | |
| 128 | 121 | |
| 129 | - if (12 - result < 4) { | |
| 122 | + if (result == 1) { | |
| 130 | 123 | rects.push_back(Rect(cvRound(x*factor), cvRound(y*factor), windowSize.width, windowSize.height)); |
| 131 | - rejectLevels.push_back(result); | |
| 132 | - levelWeights.push_back(gypWeight); | |
| 124 | + confidences.push_back(confidence); | |
| 133 | 125 | } |
| 134 | 126 | |
| 127 | + // Add ROC mode | |
| 128 | + | |
| 135 | 129 | if (result == 0) |
| 136 | 130 | x += step; |
| 137 | 131 | } |
| 138 | 132 | } |
| 139 | 133 | } |
| 140 | 134 | |
| 135 | + // groupRectangles(rects, confidences, eps); | |
| 141 | 136 | groupRectangles(rects, rejectLevels, levelWeights, minNeighbors, eps); |
| 142 | 137 | |
| 143 | 138 | if (!enrollAll && rects.empty()) | ... | ... |