From 7b17e41327f0e2b71ecf7d9ccc30929a6d909475 Mon Sep 17 00:00:00 2001 From: Josh Klontz Date: Tue, 31 Aug 2021 18:22:19 -0600 Subject: [PATCH] remove universal template --- openbr/CMakeLists.txt | 3 +-- openbr/openbr_plugin.cpp | 122 -------------------------------------------------------------------------------------------------------------------------- openbr/openbr_plugin.h | 7 ------- openbr/plugins/gallery/binary.cpp | 27 --------------------------- openbr/universal_template.cpp | 113 ----------------------------------------------------------------------------------------------------------------- openbr/universal_template.h | 104 -------------------------------------------------------------------------------------------------------- 6 files changed, 1 insertion(+), 375 deletions(-) delete mode 100644 openbr/universal_template.cpp delete mode 100644 openbr/universal_template.h diff --git a/openbr/CMakeLists.txt b/openbr/CMakeLists.txt index fcc7df0..8686db8 100644 --- a/openbr/CMakeLists.txt +++ b/openbr/CMakeLists.txt @@ -4,8 +4,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) # Collect source files set(SRC openbr.cpp - openbr_plugin.cpp - universal_template.cpp) + openbr_plugin.cpp) aux_source_directory(core BR_CORE) include(plugins/plugins.cmake) diff --git a/openbr/openbr_plugin.cpp b/openbr/openbr_plugin.cpp index 7ccefdd..bcb7ed4 100644 --- a/openbr/openbr_plugin.cpp +++ b/openbr/openbr_plugin.cpp @@ -407,128 +407,6 @@ static T findAndRemove(QVariantMap &map, const QString &key, const T &defaultVal return result; } -br_utemplate Template::toUniversalTemplate(const Template &t) -{ - QVariantMap map = t.file.localMetadata(); - - // QJsonObject::fromVariantMap (below) fails to convert - // QRects and QPoints to string, replacing them with null values. - // so hand-convert these weirdos - foreach (const QString &k, map.keys()) { - QVariant v = map[k]; - if (v.canConvert(QVariant::PointF) || v.canConvert(QVariant::RectF)) { - QString newv = QtUtils::toString(v); - map[k] = newv; - } - // lists of points and rects, too - else if (v.type() == QVariant::List) { - QVariantList oldlist = qvariant_cast(v); - if (!oldlist.isEmpty() && (oldlist.first().canConvert(QVariant::PointF) || oldlist.first().canConvert(QVariant::RectF))) { - QVariantList newlist; - foreach (const QVariant &subv, oldlist) { - newlist.append(QtUtils::toString(subv)); - } - map[k] = newlist; - } - } - } - - const int32_t algorithmID = findAndRemove (map, "AlgorithmID", 0); - const uint32_t frame = findAndRemove(map, "Frame" , std::numeric_limits::max()); - const int32_t x = findAndRemove (map, "X" , 0); - const int32_t y = findAndRemove (map, "Y" , 0); - const uint32_t width = findAndRemove(map, "Width" , 0); - const uint32_t height = findAndRemove(map, "Height" , 0); - const float confidence = findAndRemove (map, "Confidence" , 0); - const uint32_t personID = findAndRemove(map, "PersonID" , std::numeric_limits::max()); - const QByteArray metadata = QJsonDocument(QJsonObject::fromVariantMap(map)).toJson(); - const Mat &m = t; - return br_new_utemplate(algorithmID, frame, x, y, width, height, confidence, personID, metadata.constData(), (const char*) m.data, m.rows * m.cols * m.elemSize()); -} - -Template Template::fromUniversalTemplate(br_const_utemplate ut) -{ - QVariantMap map = QJsonDocument::fromJson(QByteArray((const char*) ut->data)).object().toVariantMap(); - - // QJsonDocument::fromJson doesn't know about QRects and QPoints - // so convert any QStrings that can be converted - foreach (const QString &k, map.keys()) { - QVariant v = map[k]; - QVariant newv; - bool istype; - if (v.type() == QVariant::String) { - QString vstr = qvariant_cast(v); - newv = QtUtils::toRect(vstr, &istype); - if (!istype) { - newv = QtUtils::toPoint(vstr, &istype); - if (!istype) { - newv = v; - } - } - map[k] = newv; - } - // convert lists of rects and points, too - else if (v.type() == QVariant::List) { - QVariantList oldlist = qvariant_cast(v); - if (!oldlist.isEmpty() && oldlist.first().type() == QVariant::String) { - QString test = qvariant_cast(oldlist.first()); - QtUtils::toRect(test, &istype); - QVariantList newlist; - if (istype) { - foreach (const QVariant &subv, oldlist) { - newlist.append(QtUtils::toRect(qvariant_cast(subv))); - } - } else { - QtUtils::toPoint(test, &istype); - if (istype) { - foreach (const QVariant &subv, oldlist) { - newlist.append(QtUtils::toPoint(qvariant_cast(subv))); - } - } else { - newlist = oldlist; - } - } - map[k] = newlist; - } - } - } - - map.insert("AlgorithmID", ut->algorithmID); - map.insert("Frame" , ut->frame ); - map.insert("X" , ut->x ); - map.insert("Y" , ut->y ); - map.insert("Width" , ut->width ); - map.insert("Height" , ut->height ); - map.insert("Confidence" , ut->confidence ); - map.insert("PersonID" , ut->personID ); - const Mat m = Mat(1, ut->fvSize, CV_8UC1, (void*)(ut->data + ut->mdSize)).clone(); - return Template(File(map), m); -} - -br_utemplate Template::readUniversalTemplate(QFile &file) -{ - const size_t headerSize = sizeof(br_universal_template); - br_universal_template *t = (br_universal_template*) malloc(headerSize); - file.read((char*) t, headerSize); - - const size_t dataSize = t->mdSize + t->fvSize; - t = (br_universal_template*) realloc(t, headerSize + dataSize); - file.read((char*) &t->data, dataSize); - return t; -} - -void Template::writeUniversalTemplate(QFile &file, br_const_utemplate t) -{ - const qint64 size = sizeof(br_universal_template) + t->mdSize + t->fvSize; - if (file.write((const char *) t, size) != size) - qFatal("Failed to write universal template!"); -} - -void Template::freeUniversalTemplate(br_const_utemplate t) -{ - free((void*) t); -} - QDataStream &br::operator<<(QDataStream &stream, const Template &t) { return stream << static_cast&>(t) << t.file; diff --git a/openbr/openbr_plugin.h b/openbr/openbr_plugin.h index d8e6e10..9b975db 100644 --- a/openbr/openbr_plugin.h +++ b/openbr/openbr_plugin.h @@ -41,7 +41,6 @@ #include #include #include -#include #include namespace br @@ -296,12 +295,6 @@ struct Template : public QList foreach (const cv::Mat &m, *this) other += m.clone(); return other; } - - static br_utemplate toUniversalTemplate(const Template &t); - static Template fromUniversalTemplate(br_const_utemplate ut); - static br_utemplate readUniversalTemplate(QFile &file); - static void writeUniversalTemplate(QFile &file, br_const_utemplate t); - static void freeUniversalTemplate(br_const_utemplate t); }; BR_EXPORT QDataStream &operator<<(QDataStream &stream, const Template &t); diff --git a/openbr/plugins/gallery/binary.cpp b/openbr/plugins/gallery/binary.cpp index 3c54b39..0ec083e 100644 --- a/openbr/plugins/gallery/binary.cpp +++ b/openbr/plugins/gallery/binary.cpp @@ -201,33 +201,6 @@ BR_REGISTER(Gallery, galGallery) /*! * \ingroup galleries - * \brief A contiguous array of br_universal_template. - * \author Josh Klontz \cite jklontz - */ -class utGallery : public BinaryGallery -{ - Q_OBJECT - - Template readTemplate() - { - const br_const_utemplate ut = Template::readUniversalTemplate(gallery); - const Template t = Template::fromUniversalTemplate(ut); - Template::freeUniversalTemplate(ut); - return t; - } - - void writeTemplate(const Template &t) - { - const br_utemplate ut = Template::toUniversalTemplate(t); - gallery.write((const char*) ut, sizeof(br_universal_template) + ut->mdSize + ut->fvSize); - Template::freeUniversalTemplate(ut); - } -}; - -BR_REGISTER(Gallery, utGallery) - -/*! - * \ingroup galleries * \brief Newline-separated URLs. * \author Josh Klontz \cite jklontz */ diff --git a/openbr/universal_template.cpp b/openbr/universal_template.cpp deleted file mode 100644 index 5098ad1..0000000 --- a/openbr/universal_template.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "universal_template.h" - -br_utemplate br_new_utemplate(int32_t algorithmID, uint32_t frame, int32_t x, int32_t y, uint32_t width, uint32_t height, float confidence, uint32_t personID, const char *metadata, const char *featureVector, uint32_t fvSize) -{ - const uint32_t mdSize = strlen(metadata) + 1; - br_utemplate utemplate = (br_utemplate) malloc(sizeof(br_universal_template) + mdSize + fvSize); - utemplate->algorithmID = algorithmID; - utemplate->frame = frame; - utemplate->x = x; - utemplate->y = y; - utemplate->width = width; - utemplate->height = height; - utemplate->confidence = confidence; - utemplate->personID = personID; - utemplate->mdSize = mdSize; - utemplate->fvSize = fvSize; - memcpy(reinterpret_cast(utemplate+1) + 0, metadata , mdSize); - memcpy(reinterpret_cast(utemplate+1) + mdSize, featureVector, fvSize); - return utemplate; -} - -void br_free_utemplate(br_const_utemplate utemplate) -{ - free((void*) utemplate); -} - -void br_append_utemplate(FILE *file, br_const_utemplate utemplate) -{ - fwrite(utemplate, sizeof(br_universal_template) + utemplate->mdSize + utemplate->fvSize, 1, file); -} - -void br_iterate_utemplates(br_const_utemplate begin, br_const_utemplate end, br_utemplate_callback callback, br_callback_context context) -{ - while (begin != end) { - callback(begin, context); - begin = reinterpret_cast(reinterpret_cast(begin) + sizeof(br_universal_template) + begin->mdSize + begin->fvSize); - if (begin > end) - qFatal("Overshot end of buffer"); - } -} - -static void callAndFree(br_utemplate_callback callback, br_utemplate t, br_callback_context context) -{ - callback(t, context); - free(t); -} - -static bool read_buffer(FILE *file, char *buffer, size_t bytes) -{ - size_t bytesRemaining = bytes; - while (bytesRemaining) { - const size_t bytesRead = fread(buffer, 1, bytesRemaining, file); - buffer += bytesRead; - bytesRemaining -= bytesRead; - - if (feof(file)) { - if ((bytesRemaining != bytes) && !fseek(file, bytesRemaining - bytes, SEEK_CUR)) // Try to rewind - bytesRemaining = bytes; - if (bytesRemaining == bytes) - return false; - qFatal("End of file after reading %d of %d bytes.", int(bytes - bytesRemaining), int(bytes)); - } - - if (ferror(file)) { - perror(NULL); - qFatal("Error after reading %d of %d bytes.", int(bytes - bytesRemaining), int(bytes)); - } - } - return true; -} - -int br_iterate_utemplates_file(FILE *file, br_utemplate_callback callback, br_callback_context context, bool parallel) -{ - int count = 0; - QFutureSynchronizer futures; - while (true) { - br_utemplate t = (br_utemplate) malloc(sizeof(br_universal_template)); - - if (!read_buffer(file, (char*) t, sizeof(br_universal_template))) { - free(t); - break; - } - - t = (br_utemplate) realloc(t, sizeof(br_universal_template) + t->mdSize + t->fvSize); - if (!read_buffer(file, (char*) &t->data, t->mdSize + t->fvSize)) { - free(t); - - // Try to rewind header read - if (fseek(file, -long(sizeof(br_universal_template)), SEEK_CUR)) - qFatal("Unable to recover from partial template read!"); - - break; - } - - if (parallel) futures.addFuture(QtConcurrent::run(callAndFree, callback, t, context)); - else callAndFree(callback, t, context); - count++; - } - return count; -} - -void br_log(const char *message) -{ - qDebug() << qPrintable(QTime::currentTime().toString("hh:mm:ss.zzz")) << "-" << message; -} diff --git a/openbr/universal_template.h b/openbr/universal_template.h deleted file mode 100644 index 026f868..0000000 --- a/openbr/universal_template.h +++ /dev/null @@ -1,104 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright 2014 Noblis * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); * - * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#ifndef BR_UNIVERSAL_TEMPLATE_H -#define BR_UNIVERSAL_TEMPLATE_H - -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -// Disable 'nonstandard extension used : zero-sized array in struct/union' warning -#ifdef _MSC_VER -# pragma warning(disable: 4200) -#endif // _MSC_VER - -/*! - * \brief A flat template format for representing arbitrary feature vectors. - */ -struct br_universal_template -{ - int32_t algorithmID; /*!< Interpretation of _data_ after _mdSize_. */ - uint32_t frame; /*!< Video frame number, or numeric_limits::max() for still images. */ - int32_t x; /*!< Region of interest horizontal offset (pixels). */ - int32_t y; /*!< Region of interest vertical offset (pixels). */ - uint32_t width; /*!< Region of interest horizontal size (pixels). */ - uint32_t height; /*!< Region of interest vertical size (pixels). */ - float confidence; /*!< Region of interest confidence. */ - uint32_t personID; /*!< Unique identifier or numeric_limits::max() if unknown. */ - uint32_t mdSize; /*!< Length of a null-terminated metadata string at the beginning of _data_, - including the null-terminator character itself. */ - uint32_t fvSize; /*!< Length of the feature vector after the metadata in _data_. */ - unsigned char data[]; /*!< (_mdSize_ + _fvSize_)-byte buffer. - The first _mdSize_ bytes represent the metadata. - The remaining _fvSize_ bytes represent the feature vector. */ -}; - -typedef struct br_universal_template *br_utemplate; -typedef const struct br_universal_template *br_const_utemplate; - -/*! - * \brief br_universal_template constructor. - * \see br_free_utemplate - */ -BR_EXPORT br_utemplate br_new_utemplate(int32_t algorithmID, uint32_t frame, int32_t x, int32_t y, uint32_t width, uint32_t height, float confidence, uint32_t personID, const char *metadata, const char *featureVector, uint32_t fvSize); - -/*! - * \brief br_universal_template destructor. - * \see br_new_utemplate - */ -BR_EXPORT void br_free_utemplate(br_const_utemplate utemplate); - -/*! - * \brief Serialize a br_universal_template to a file. - * \see br_append_utemplate_contents - */ -BR_EXPORT void br_append_utemplate(FILE *file, br_const_utemplate utemplate); - -/*! - * \brief br_universal_template iterator callback. - * \see br_iterate_utemplates - */ -typedef void *br_callback_context; -typedef void (*br_utemplate_callback)(br_const_utemplate, br_callback_context); - -/*! - * \brief Iterate over an inplace array of br_universal_template. - * \see br_iterate_utemplates_file - */ -BR_EXPORT void br_iterate_utemplates(br_const_utemplate begin, br_const_utemplate end, br_utemplate_callback callback, br_callback_context context); - -/*! - * \brief Iterate over br_universal_template in a file. - * \return The number of templates iterated - * \see br_iterate_utemplates - */ -BR_EXPORT int br_iterate_utemplates_file(FILE *file, br_utemplate_callback callback, br_callback_context context, bool parallel); - -/*! - * \brief Write a message annotated with the current time to stderr. - */ -BR_EXPORT void br_log(const char *message); - -#ifdef __cplusplus -} -#endif - -#endif // BR_UNIVERSAL_TEMPLATE_H -- libgit2 0.21.4