Commit 3df533ac158b154c998f80c1f9782350f70fc0b0
1 parent
32bac187
final breaking refactor of utGallery
Showing
1 changed file
with
8 additions
and
135 deletions
openbr/plugins/gallery/binary.cpp
| ... | ... | @@ -204,154 +204,27 @@ BR_REGISTER(Gallery, galGallery) |
| 204 | 204 | * \brief A contiguous array of br_universal_template. |
| 205 | 205 | * \author Josh Klontz \cite jklontz |
| 206 | 206 | */ |
| 207 | -class utGallery : public BinaryGallery | |
| 207 | +class tGallery : public BinaryGallery | |
| 208 | 208 | { |
| 209 | 209 | Q_OBJECT |
| 210 | 210 | |
| 211 | 211 | Template readTemplate() |
| 212 | 212 | { |
| 213 | - Template t; | |
| 214 | - br_universal_template ut; | |
| 215 | - if (gallery.read((char*)&ut, sizeof(br_universal_template)) == sizeof(br_universal_template)) { | |
| 216 | - QByteArray data(ut.mdSize + ut.fvSize, Qt::Uninitialized); | |
| 217 | - char *dst = data.data(); | |
| 218 | - qint64 bytesNeeded = ut.mdSize + ut.fvSize; | |
| 219 | - while (bytesNeeded > 0) { | |
| 220 | - qint64 bytesRead = gallery.read(dst, bytesNeeded); | |
| 221 | - if (bytesRead <= 0) { | |
| 222 | - qDebug() << gallery.errorString(); | |
| 223 | - qFatal("Unexepected EOF while reading universal template data, needed: %d more of: %d bytes.", int(bytesNeeded), int(ut.mdSize + ut.fvSize)); | |
| 224 | - } | |
| 225 | - bytesNeeded -= bytesRead; | |
| 226 | - dst += bytesRead; | |
| 227 | - } | |
| 228 | - | |
| 229 | - t.file.set("AlgorithmID", ut.algorithmID); | |
| 230 | - t.file.set("Metadata", QString(data.data())); | |
| 231 | - char *dataStart = data.data() + ut.mdSize; | |
| 232 | - uint32_t dataSize = ut.fvSize; | |
| 233 | - if ((ut.algorithmID <= -1) && (ut.algorithmID >= -3)) { | |
| 234 | - t.file.set("FrontalFace", QRectF(ut.x, ut.y, ut.width, ut.height)); | |
| 235 | - uint32_t *rightEyeX = reinterpret_cast<uint32_t*>(dataStart); | |
| 236 | - dataStart += sizeof(uint32_t); | |
| 237 | - uint32_t *rightEyeY = reinterpret_cast<uint32_t*>(dataStart); | |
| 238 | - dataStart += sizeof(uint32_t); | |
| 239 | - uint32_t *leftEyeX = reinterpret_cast<uint32_t*>(dataStart); | |
| 240 | - dataStart += sizeof(uint32_t); | |
| 241 | - uint32_t *leftEyeY = reinterpret_cast<uint32_t*>(dataStart); | |
| 242 | - dataStart += sizeof(uint32_t); | |
| 243 | - dataSize -= sizeof(uint32_t)*4; | |
| 244 | - t.file.set("First_Eye", QPointF(*rightEyeX, *rightEyeY)); | |
| 245 | - t.file.set("Second_Eye", QPointF(*leftEyeX, *leftEyeY)); | |
| 246 | - } else if (ut.algorithmID == 7) { | |
| 247 | - // binary data consisting of a single channel matrix, of a supported type. | |
| 248 | - // 4 element header: | |
| 249 | - // uint16 datatype (single channel opencv datatype code) | |
| 250 | - // uint32 matrix rows | |
| 251 | - // uint32 matrix cols | |
| 252 | - // uint16 matrix depth (max 512) | |
| 253 | - // Followed by serialized data, in row-major order (in r/c), with depth values | |
| 254 | - // for each layer listed in order (i.e. rgb, rgb etc.) | |
| 255 | - // #### NOTE! matlab's default order is col-major, so some work should | |
| 256 | - // be done on the matlab side to make sure that the initial serialization is correct. | |
| 257 | - uint16_t dataType = *reinterpret_cast<uint32_t*>(dataStart); | |
| 258 | - dataStart += sizeof(uint16_t); | |
| 259 | - | |
| 260 | - uint32_t matrixRows = *reinterpret_cast<uint32_t*>(dataStart); | |
| 261 | - dataStart += sizeof(uint32_t); | |
| 262 | - | |
| 263 | - uint32_t matrixCols = *reinterpret_cast<uint32_t*>(dataStart); | |
| 264 | - dataStart += sizeof(uint32_t); | |
| 265 | - | |
| 266 | - uint16_t matrixDepth= *reinterpret_cast<uint16_t*>(dataStart); | |
| 267 | - dataStart += sizeof(uint16_t); | |
| 268 | - | |
| 269 | - // Set metadata | |
| 270 | - t.file.set("X", ut.x); | |
| 271 | - t.file.set("Y", ut.y); | |
| 272 | - t.file.set("Width", ut.width); | |
| 273 | - t.file.set("Height", ut.height); | |
| 274 | - t.file.set("Confidence", ut.confidence); | |
| 275 | - | |
| 276 | - t.append(cv::Mat(matrixRows, matrixCols, CV_MAKETYPE(dataType, matrixDepth), dataStart).clone() /* We don't want a shallow copy! */); | |
| 277 | - return t; | |
| 278 | - } else { | |
| 279 | - t.file.set("X", ut.x); | |
| 280 | - t.file.set("Y", ut.y); | |
| 281 | - t.file.set("Width", ut.width); | |
| 282 | - t.file.set("Height", ut.height); | |
| 283 | - t.file.set("Confidence", ut.confidence); | |
| 284 | - } | |
| 285 | - t.append(cv::Mat(1, dataSize, CV_8UC1, dataStart).clone() /* We don't want a shallow copy! */); | |
| 286 | - } else { | |
| 287 | - if (!gallery.atEnd()) | |
| 288 | - qWarning("Failed to read universal template header!"); | |
| 289 | - gallery.close(); | |
| 290 | - } | |
| 213 | + const br_const_utemplate ut = Template::readUniversalTemplate(gallery); | |
| 214 | + const Template t = Template::fromUniversalTemplate(ut); | |
| 215 | + Template::freeUniversalTemplate(ut); | |
| 291 | 216 | return t; |
| 292 | 217 | } |
| 293 | 218 | |
| 294 | 219 | void writeTemplate(const Template &t) |
| 295 | 220 | { |
| 296 | - const int32_t algorithmID = (t.isEmpty() || t.file.fte) ? 0 : t.file.get<int32_t>("AlgorithmID"); | |
| 297 | - | |
| 298 | - // QUrl::fromUserInput provides some nice functionality in terms of completing URLs | |
| 299 | - // e.g. C:/test.jpg -> file://C:/test.jpg and google.com/image.jpg -> http://google.com/image.jpg | |
| 300 | - const QByteArray metadata = QUrl::fromUserInput(t.file.get<QString>("URL", t.file.name)).toEncoded(); | |
| 301 | - | |
| 302 | - int32_t x = 0, y = 0; | |
| 303 | - uint32_t width = 0, height = 0; | |
| 304 | - float confidence = 0; | |
| 305 | - QByteArray header; | |
| 306 | - if ((algorithmID <= -1) && (algorithmID >= -3)) { | |
| 307 | - const QRectF frontalFace = t.file.get<QRectF>("FrontalFace"); | |
| 308 | - x = frontalFace.x(); | |
| 309 | - y = frontalFace.y(); | |
| 310 | - width = frontalFace.width(); | |
| 311 | - height = frontalFace.height(); | |
| 312 | - | |
| 313 | - const QPointF firstEye = t.file.get<QPointF>("First_Eye"); | |
| 314 | - const QPointF secondEye = t.file.get<QPointF>("Second_Eye"); | |
| 315 | - const uint32_t rightEyeX = firstEye.x(); | |
| 316 | - const uint32_t rightEyeY = firstEye.y(); | |
| 317 | - const uint32_t leftEyeX = secondEye.x(); | |
| 318 | - const uint32_t leftEyeY = secondEye.y(); | |
| 319 | - | |
| 320 | - header.append((const char*)&rightEyeX, sizeof(uint32_t)); | |
| 321 | - header.append((const char*)&rightEyeY, sizeof(uint32_t)); | |
| 322 | - header.append((const char*)&leftEyeX , sizeof(uint32_t)); | |
| 323 | - header.append((const char*)&leftEyeY , sizeof(uint32_t)); | |
| 324 | - } else { | |
| 325 | - x = t.file.get<int32_t>("X", 0); | |
| 326 | - y = t.file.get<int32_t>("Y", 0); | |
| 327 | - width = t.file.get<uint32_t>("Width", 0); | |
| 328 | - height = t.file.get<uint32_t>("Height", 0); | |
| 329 | - confidence = t.file.get<uint32_t>("Confidence", 0); | |
| 330 | - } | |
| 331 | - | |
| 332 | - gallery.write((const char*) &algorithmID, sizeof(int32_t)); | |
| 333 | - gallery.write((const char*) &x , sizeof(int32_t)); | |
| 334 | - gallery.write((const char*) &y , sizeof(int32_t)); | |
| 335 | - gallery.write((const char*) &width , sizeof(uint32_t)); | |
| 336 | - gallery.write((const char*) &height , sizeof(uint32_t)); | |
| 337 | - gallery.write((const char*) &confidence , sizeof(float)); | |
| 338 | - | |
| 339 | - const uint32_t mdSize = metadata.size() + 1; | |
| 340 | - gallery.write((const char*) &mdSize, sizeof(uint32_t)); | |
| 341 | - | |
| 342 | - const uint32_t signatureSize = (algorithmID == 0) ? 0 : t.m().rows * t.m().cols * t.m().elemSize(); | |
| 343 | - const uint32_t fvSize = header.size() + signatureSize; | |
| 344 | - gallery.write((const char*) &fvSize, sizeof(uint32_t)); | |
| 345 | - | |
| 346 | - gallery.write((const char*) metadata.data(), mdSize); | |
| 347 | - if (algorithmID != 0) { | |
| 348 | - gallery.write(header); | |
| 349 | - gallery.write((const char*) t.m().data, signatureSize); | |
| 350 | - } | |
| 221 | + const br_utemplate ut = Template::toUniversalTemplate(t); | |
| 222 | + gallery.write((const char*) ut, sizeof(br_universal_template) + ut->mdSize + ut->fvSize); | |
| 223 | + Template::freeUniversalTemplate(ut); | |
| 351 | 224 | } |
| 352 | 225 | }; |
| 353 | 226 | |
| 354 | -BR_REGISTER(Gallery, utGallery) | |
| 227 | +BR_REGISTER(Gallery, tGallery) | |
| 355 | 228 | |
| 356 | 229 | /*! |
| 357 | 230 | * \ingroup galleries | ... | ... |