Commit 228020c44e5beb7d66db9f167fe7646a57b0c0bc
1 parent
22e1be95
Implemented LoadStoreDistance
Showing
2 changed files
with
81 additions
and
3 deletions
openbr/openbr_plugin.cpp
| ... | ... | @@ -1686,15 +1686,17 @@ Distance *Distance::make(QString str, QObject *parent) |
| 1686 | 1686 | if (Globals->abbreviations.contains(str)) |
| 1687 | 1687 | return make(Globals->abbreviations[str], parent); |
| 1688 | 1688 | |
| 1689 | + // Check for use of '<...>' as shorthand for LoadStore(...) | |
| 1690 | + if (str.startsWith('<') && str.endsWith('>')) | |
| 1691 | + return make("LoadStore(" + str.mid(1, str.size()-2) + ")", parent); | |
| 1692 | + | |
| 1689 | 1693 | { // Check for use of '+' as shorthand for Pipe(...) |
| 1690 | 1694 | QStringList words = parse(str, '+'); |
| 1691 | 1695 | if (words.size() > 1) |
| 1692 | 1696 | return make("Pipe([" + words.join(",") + "])", parent); |
| 1693 | 1697 | } |
| 1694 | 1698 | |
| 1695 | - File f = "." + str; | |
| 1696 | - Distance *distance = Factory<Distance>::make(f); | |
| 1697 | - | |
| 1699 | + Distance *const distance = Factory<Distance>::make("." + str); | |
| 1698 | 1700 | distance->setParent(parent); |
| 1699 | 1701 | return distance; |
| 1700 | 1702 | } | ... | ... |
openbr/plugins/core/loadstore.cpp
| ... | ... | @@ -163,6 +163,82 @@ private: |
| 163 | 163 | |
| 164 | 164 | BR_REGISTER(Transform, LoadStoreTransform) |
| 165 | 165 | |
| 166 | +/*! | |
| 167 | + * \ingroup distances | |
| 168 | + * \brief Caches Distance training. | |
| 169 | + * \author Josh Klontz \cite jklontz | |
| 170 | + */ | |
| 171 | +class LoadStoreDistance : public Distance | |
| 172 | +{ | |
| 173 | + Q_OBJECT | |
| 174 | + Q_PROPERTY(QString distanceString READ get_distanceString WRITE set_distanceString RESET reset_distanceString STORED false) | |
| 175 | + Q_PROPERTY(QString fileName READ get_fileName WRITE set_fileName RESET reset_fileName STORED false) | |
| 176 | + BR_PROPERTY(QString, distanceString, QString()) | |
| 177 | + BR_PROPERTY(QString, fileName, QString()) | |
| 178 | + | |
| 179 | + QSharedPointer<Distance> distance; | |
| 180 | + | |
| 181 | +private: | |
| 182 | + void init() | |
| 183 | + { | |
| 184 | + const QString resolvedFileName = getFileName(); | |
| 185 | + if (resolvedFileName.isEmpty()) { | |
| 186 | + distance.reset(Distance::make(distanceString)); | |
| 187 | + return; | |
| 188 | + } | |
| 189 | + | |
| 190 | + qDebug("Loading %s", qPrintable(resolvedFileName)); | |
| 191 | + QFile file(resolvedFileName); | |
| 192 | + if (!file.open(QFile::ReadOnly)) | |
| 193 | + qFatal("Failed to open %s for reading.", qPrintable(resolvedFileName)); | |
| 194 | + | |
| 195 | + QDataStream stream(&file); | |
| 196 | + stream >> distanceString; | |
| 197 | + | |
| 198 | + distance.reset(Distance::make(distanceString)); | |
| 199 | + distance->load(stream); | |
| 200 | + } | |
| 201 | + | |
| 202 | + QString getFileName() const | |
| 203 | + { | |
| 204 | + foreach (const QString &file, QStringList() << fileName | |
| 205 | + << Globals->sdkPath + "/share/openbr/models/distances/" + fileName) | |
| 206 | + if (QFileInfo(file).exists()) | |
| 207 | + return file; | |
| 208 | + return QString(); | |
| 209 | + } | |
| 210 | + | |
| 211 | + void train(const TemplateList &src) | |
| 212 | + { | |
| 213 | + if (QFileInfo(getFileName()).exists()) | |
| 214 | + return; | |
| 215 | + | |
| 216 | + qDebug("Training %s", qPrintable(fileName)); | |
| 217 | + distance->train(src); | |
| 218 | + | |
| 219 | + qDebug("Storing %s", qPrintable(fileName)); | |
| 220 | + QFile file(fileName); | |
| 221 | + if (!file.open(QFile::WriteOnly)) | |
| 222 | + qFatal("Failed to open %s for writing.", qPrintable(fileName)); | |
| 223 | + | |
| 224 | + QDataStream stream(&file); | |
| 225 | + stream << distanceString; | |
| 226 | + distance->store(stream); | |
| 227 | + } | |
| 228 | + | |
| 229 | + float compare(const Template &a, const Template &b) const | |
| 230 | + { | |
| 231 | + return distance->compare(a, b); | |
| 232 | + } | |
| 233 | + | |
| 234 | + float compare(const uchar *a, const uchar *b, size_t size) const | |
| 235 | + { | |
| 236 | + return distance->compare(a, b, size); | |
| 237 | + } | |
| 238 | +}; | |
| 239 | + | |
| 240 | +BR_REGISTER(Distance, LoadStoreDistance) | |
| 241 | + | |
| 166 | 242 | } // namespace br |
| 167 | 243 | |
| 168 | 244 | #include "core/loadstore.moc" | ... | ... |