Commit 318e194dc6f1fd8277c2bd4f8fdea9c21f582cd3
1 parent
009ff903
first draft of DownloadTransform
Showing
1 changed file
with
72 additions
and
0 deletions
openbr/plugins/misc.cpp
| ... | ... | @@ -14,6 +14,7 @@ |
| 14 | 14 | * limitations under the License. * |
| 15 | 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 16 | 16 | |
| 17 | +#include <QtNetwork> | |
| 17 | 18 | #include <QElapsedTimer> |
| 18 | 19 | #include <QRegularExpression> |
| 19 | 20 | #include <opencv2/highgui/highgui.hpp> |
| ... | ... | @@ -83,6 +84,77 @@ BR_REGISTER(Transform, DecodeTransform) |
| 83 | 84 | |
| 84 | 85 | /*! |
| 85 | 86 | * \ingroup transforms |
| 87 | + * \brief Downloads an image from a URL | |
| 88 | + * \author Josh Klontz \cite jklontz | |
| 89 | + */ | |
| 90 | +class DownloadTransform : public UntrainableMetaTransform | |
| 91 | +{ | |
| 92 | + Q_OBJECT | |
| 93 | + Q_ENUMS(Mode) | |
| 94 | + | |
| 95 | +public: | |
| 96 | + enum Mode { Permissive, | |
| 97 | + Encoded, | |
| 98 | + Decoded }; | |
| 99 | +private: | |
| 100 | + BR_PROPERTY(Mode, mode, Encoded) | |
| 101 | + | |
| 102 | + mutable QNetworkAccessManager nam; | |
| 103 | + mutable QMutex namLock; | |
| 104 | + | |
| 105 | + void project(const Template &src, Template &dst) const | |
| 106 | + { | |
| 107 | + dst.file = src.file; | |
| 108 | + QString url = src.file.get<QString>("URL").simplified(); | |
| 109 | + if (url.isEmpty()) | |
| 110 | + return; | |
| 111 | + | |
| 112 | + if (url.startsWith("file://")) | |
| 113 | + url = url.mid(7); | |
| 114 | + | |
| 115 | + QIODevice *device = NULL; | |
| 116 | + if (QFileInfo(url).exists()) { | |
| 117 | + device = new QFile(url); | |
| 118 | + device->open(QIODevice::ReadOnly); | |
| 119 | + } else { | |
| 120 | + namLock.lock(); | |
| 121 | + QNetworkReply *reply = nam.get(QNetworkRequest(url)); | |
| 122 | + namLock.unlock(); | |
| 123 | + | |
| 124 | + reply->waitForReadyRead(-1); | |
| 125 | + while (!reply->isFinished()) | |
| 126 | + QCoreApplication::processEvents(); | |
| 127 | + | |
| 128 | + if (reply->error() != QNetworkReply::NoError) { | |
| 129 | + qDebug() << reply->errorString() << url; | |
| 130 | + reply->deleteLater(); | |
| 131 | + } else { | |
| 132 | + device = reply; | |
| 133 | + } | |
| 134 | + } | |
| 135 | + | |
| 136 | + if (!device) | |
| 137 | + return; | |
| 138 | + | |
| 139 | + const QByteArray data = device->readAll(); | |
| 140 | + delete device; | |
| 141 | + device = NULL; | |
| 142 | + | |
| 143 | + Mat encoded(1, data.size(), CV_8UC1, (void*)data.data()); | |
| 144 | + if (mode == Permissive) { | |
| 145 | + dst += encoded; | |
| 146 | + } else { | |
| 147 | + Mat decoded = imdecode(encoded, IMREAD_UNCHANGED); | |
| 148 | + if (!decoded.empty()) | |
| 149 | + dst += (mode == Encoded) ? encoded : decoded; | |
| 150 | + } | |
| 151 | + } | |
| 152 | +}; | |
| 153 | + | |
| 154 | +BR_REGISTER(Transform, DownloadTransform) | |
| 155 | + | |
| 156 | +/*! | |
| 157 | + * \ingroup transforms | |
| 86 | 158 | * \brief Prints the template's file to stdout or stderr. |
| 87 | 159 | * \author Josh Klontz \cite jklontz |
| 88 | 160 | */ | ... | ... |