Commit de136a3b4e11f7276ef1a5a92279a80d376d68ff

Authored by Josh Klontz
1 parent f0bfec07

better implementation for caching pittpatt galleries

Showing 1 changed file with 44 additions and 95 deletions
openbr/plugins/pp5.cpp
@@ -305,13 +305,21 @@ class PP5CompareDistance : public Distance @@ -305,13 +305,21 @@ class PP5CompareDistance : public Distance
305 , public PP5Context 305 , public PP5Context
306 { 306 {
307 Q_OBJECT 307 Q_OBJECT
308 - mutable QMap<QString, ppr_gallery_type> cache; 308 +
  309 + struct NativeGallery
  310 + {
  311 + FileList files;
  312 + QList<int> faceIDs;
  313 + ppr_gallery_type gallery;
  314 + };
  315 +
  316 + mutable QMap<QString, NativeGallery> cache;
309 mutable QMutex cacheLock; 317 mutable QMutex cacheLock;
310 318
311 ~PP5CompareDistance() 319 ~PP5CompareDistance()
312 { 320 {
313 - foreach (ppr_gallery_type gallery, cache.values())  
314 - ppr_free_gallery(gallery); 321 + foreach (const NativeGallery &gallery, cache.values())
  322 + ppr_free_gallery(gallery.gallery);
315 } 323 }
316 324
317 float compare(const Template &target, const Template &query) const 325 float compare(const Template &target, const Template &query) const
@@ -323,7 +331,6 @@ class PP5CompareDistance : public Distance @@ -323,7 +331,6 @@ class PP5CompareDistance : public Distance
323 MatrixOutput *score = MatrixOutput::make(targetList.files(), queryList.files()); 331 MatrixOutput *score = MatrixOutput::make(targetList.files(), queryList.files());
324 compare(targetList, queryList, score); 332 compare(targetList, queryList, score);
325 return score->data.at<float>(0); 333 return score->data.at<float>(0);
326 -  
327 } 334 }
328 335
329 void compare(const TemplateList &target, const TemplateList &query, Output *output) const 336 void compare(const TemplateList &target, const TemplateList &query, Output *output) const
@@ -334,25 +341,27 @@ class PP5CompareDistance : public Distance @@ -334,25 +341,27 @@ class PP5CompareDistance : public Distance
334 QList<int> target_face_ids, query_face_ids; 341 QList<int> target_face_ids, query_face_ids;
335 enroll(target, &target_gallery, target_face_ids); 342 enroll(target, &target_gallery, target_face_ids);
336 enroll(query, &query_gallery, query_face_ids); 343 enroll(query, &query_gallery, query_face_ids);
  344 + compareNative(target_gallery, target_face_ids, query_gallery, query_face_ids, output);
  345 + ppr_free_gallery(target_gallery);
  346 + ppr_free_gallery(query_gallery);
  347 + }
337 348
338 - ppr_similarity_matrix_type similarity_matrix;  
339 - TRY(ppr_compare_galleries(context, query_gallery, target_gallery, &similarity_matrix))  
340 -  
341 - for (int i=0; i<query_face_ids.size(); i++) {  
342 - int query_face_id = query_face_ids[i];  
343 - for (int j=0; j<target_face_ids.size(); j++) {  
344 - int target_face_id = target_face_ids[j]; 349 + void compareNative(ppr_gallery_type target, const QList<int> &targetIDs, ppr_gallery_type query, const QList<int> &queryIDs, Output *output) const
  350 + {
  351 + ppr_similarity_matrix_type simmat;
  352 + TRY(ppr_compare_galleries(context, query, target, &simmat))
  353 + for (int i=0; i<queryIDs.size(); i++) {
  354 + int query_face_id = queryIDs[i];
  355 + for (int j=0; j<targetIDs.size(); j++) {
  356 + int target_face_id = targetIDs[j];
345 float score = -std::numeric_limits<float>::max(); 357 float score = -std::numeric_limits<float>::max();
346 if ((query_face_id != -1) && (target_face_id != -1)) { 358 if ((query_face_id != -1) && (target_face_id != -1)) {
347 - TRY(ppr_get_face_similarity_score(context, similarity_matrix, query_face_id, target_face_id, &score)) 359 + TRY(ppr_get_face_similarity_score(context, simmat, query_face_id, target_face_id, &score))
348 } 360 }
349 output->setRelative(score, i, j); 361 output->setRelative(score, i, j);
350 } 362 }
351 } 363 }
352 -  
353 - ppr_free_similarity_matrix(similarity_matrix);  
354 - ppr_free_gallery(target_gallery);  
355 - ppr_free_gallery(query_gallery); 364 + ppr_free_similarity_matrix(simmat);
356 } 365 }
357 366
358 void enroll(const TemplateList &templates, ppr_gallery_type *gallery, QList<int> &face_ids) const 367 void enroll(const TemplateList &templates, ppr_gallery_type *gallery, QList<int> &face_ids) const
@@ -372,114 +381,54 @@ class PP5CompareDistance : public Distance @@ -372,114 +381,54 @@ class PP5CompareDistance : public Distance
372 } 381 }
373 } 382 }
374 383
375 - ppr_gallery_type cacheRetain(const File &gallery) const 384 + NativeGallery cacheRetain(const File &gallery) const
376 { 385 {
377 QMutexLocker locker(&cacheLock); 386 QMutexLocker locker(&cacheLock);
378 - ppr_gallery_type native_gallery; 387 + NativeGallery nativeGallery;
379 if (cache.contains(gallery.name)) { 388 if (cache.contains(gallery.name)) {
380 - native_gallery = cache[gallery.name]; 389 + nativeGallery = cache[gallery.name];
381 } else { 390 } else {
382 - TRY(ppr_read_gallery(context, qPrintable(gallery.name), &native_gallery)); 391 + ppr_create_gallery(context, &nativeGallery.gallery);
  392 + TemplateList templates = TemplateList::fromGallery(gallery);
  393 + enroll(templates, &nativeGallery.gallery, nativeGallery.faceIDs);
  394 + nativeGallery.files = templates.files();
383 if (gallery.get<bool>("retain")) 395 if (gallery.get<bool>("retain"))
384 - cache.insert(gallery.name, native_gallery); 396 + cache.insert(gallery.name, nativeGallery);
385 } 397 }
386 - return native_gallery; 398 + return nativeGallery;
387 } 399 }
388 400
389 - void cacheRelease(const File &gallery, ppr_gallery_type native_gallery) const 401 + void cacheRelease(const File &gallery, const NativeGallery &nativeGallery) const
390 { 402 {
391 QMutexLocker locker(&cacheLock); 403 QMutexLocker locker(&cacheLock);
392 if (cache.contains(gallery.name)) { 404 if (cache.contains(gallery.name)) {
393 if (gallery.get<bool>("release")) { 405 if (gallery.get<bool>("release")) {
394 cache.remove(gallery.name); 406 cache.remove(gallery.name);
395 - ppr_free_gallery(native_gallery); 407 + ppr_free_gallery(nativeGallery.gallery);
396 } 408 }
397 } else { 409 } else {
398 - ppr_free_gallery(native_gallery); 410 + ppr_free_gallery(nativeGallery.gallery);
399 } 411 }
400 } 412 }
401 413
402 bool compare(const File &targetGallery, const File &queryGallery, const File &output) const 414 bool compare(const File &targetGallery, const File &queryGallery, const File &output) const
403 { 415 {
404 - if ((targetGallery.suffix() != "PP5") || (queryGallery.suffix() != "PP5")) 416 + if (!targetGallery.get<bool>("native") || !queryGallery.get<bool>("native"))
405 return false; 417 return false;
406 418
407 - ppr_gallery_type native_target = cacheRetain(targetGallery);  
408 - ppr_gallery_type native_query = cacheRetain(queryGallery);  
409 -  
410 - ppr_similarity_matrix_type native_simmat;  
411 - TRY(ppr_compare_galleries(context, native_query, native_target, &native_simmat)) 419 + NativeGallery nativeTarget = cacheRetain(targetGallery);
  420 + NativeGallery nativeQuery = cacheRetain(queryGallery);
412 421
413 - int targets, queries;  
414 - TRY(ppr_get_num_faces(context, native_target, &targets))  
415 - TRY(ppr_get_num_faces(context, native_query, &queries))  
416 -  
417 - QStringList indicies;  
418 - for (int i=0; i<std::max(targets, queries); i++)  
419 - indicies.append(QString::number(i));  
420 - QScopedPointer<Output> o(Output::make(output, QStringList(indicies.mid(0, targets)), QStringList(indicies.mid(0, queries)))); 422 + QScopedPointer<Output> o(Output::make(output, nativeTarget.files, nativeQuery.files));
421 o->setBlock(0, 0); 423 o->setBlock(0, 0);
  424 + compareNative(nativeTarget.gallery, nativeTarget.faceIDs, nativeQuery.gallery, nativeQuery.faceIDs, o.data());
422 425
423 - for (int i=0; i<queries; i++)  
424 - for (int j=0; j<targets; j++) {  
425 - float score;  
426 - TRY(ppr_get_face_similarity_score(context, native_simmat, i, j, &score))  
427 - o->setRelative(score, i, j);  
428 - }  
429 -  
430 - ppr_free_similarity_matrix(native_simmat);  
431 - cacheRelease(targetGallery, native_target);  
432 - cacheRelease(queryGallery, native_query); 426 + cacheRelease(targetGallery, nativeTarget);
  427 + cacheRelease(queryGallery, nativeQuery);
433 return true; 428 return true;
434 } 429 }
435 }; 430 };
436 431
437 BR_REGISTER(Distance, PP5CompareDistance) 432 BR_REGISTER(Distance, PP5CompareDistance)
438 433
439 -/*!  
440 - * \ingroup galleries  
441 - * \brief For storing and comparing PittPatt templates natively  
442 - * \author Josh Klontz \cite jklontz  
443 - */  
444 -class PP5Gallery : public Gallery  
445 - , public PP5Context  
446 -{  
447 - Q_OBJECT  
448 - ppr_gallery_type gallery;  
449 - int face_id;  
450 -  
451 - ~PP5Gallery()  
452 - {  
453 - ppr_write_gallery(context, qPrintable(file.name), gallery);  
454 - ppr_free_gallery(gallery);  
455 - }  
456 -  
457 - void init()  
458 - {  
459 - face_id = 0;  
460 - ppr_create_gallery(context, &gallery);  
461 - }  
462 -  
463 - TemplateList readBlock(bool *done)  
464 - {  
465 - *done = true;  
466 - qFatal("PP5Gallery read not supported.");  
467 - return TemplateList();  
468 - }  
469 -  
470 - void write(const Template &t)  
471 - {  
472 - if (!t.m().data)  
473 - return;  
474 -  
475 - ppr_face_type face;  
476 - createFace(t, &face);  
477 - TRY(ppr_add_face(context, &gallery, face, face_id, face_id))  
478 - face_id++;  
479 - ppr_free_face(face);  
480 - }  
481 -};  
482 -  
483 -BR_REGISTER(Gallery, PP5Gallery)  
484 -  
485 #include "plugins/pp5.moc" 434 #include "plugins/pp5.moc"