Commit 4c12d6bef232125ea24004b4197e6c826c6c70e3
1 parent
9602dc4f
Decoupled toQVariantList
Showing
4 changed files
with
74 additions
and
58 deletions
openbr/core/qtutils.cpp
| ... | ... | @@ -401,12 +401,17 @@ QString toString(const QVariant &variant) |
| 401 | 401 | { |
| 402 | 402 | if (QString(variant.typeName()) == "QVariantList") return toString(qvariant_cast<QVariantList>(variant)); |
| 403 | 403 | else if (variant.canConvert(QVariant::String)) return variant.toString(); |
| 404 | - else if (variant.canConvert(QVariant::PointF)) return QString("(%1,%2)").arg(QString::number(qvariant_cast<QPointF>(variant).x()), | |
| 405 | - QString::number(qvariant_cast<QPointF>(variant).y())); | |
| 406 | - else if (variant.canConvert(QVariant::RectF)) return QString("(%1,%2,%3,%4)").arg(QString::number(qvariant_cast<QRectF>(variant).x()), | |
| 407 | - QString::number(qvariant_cast<QRectF>(variant).y()), | |
| 408 | - QString::number(qvariant_cast<QRectF>(variant).width()), | |
| 409 | - QString::number(qvariant_cast<QRectF>(variant).height())); | |
| 404 | + else if (variant.canConvert(QVariant::PointF)) { | |
| 405 | + QPointF point = qvariant_cast<QPointF>(variant); | |
| 406 | + return QString("(%1,%2)").arg(QString::number(point.x()),QString::number(point.y())); | |
| 407 | + } else if (variant.canConvert(QVariant::RectF)) { | |
| 408 | + QRectF rect = qvariant_cast<QRectF>(variant); | |
| 409 | + return QString("(%1,%2,%3,%4)").arg(QString::number(rect.x()), | |
| 410 | + QString::number(rect.y()), | |
| 411 | + QString::number(rect.width()), | |
| 412 | + QString::number(rect.height())); | |
| 413 | + } | |
| 414 | + | |
| 410 | 415 | return QString(); |
| 411 | 416 | } |
| 412 | 417 | ... | ... |
openbr/core/qtutils.h
| ... | ... | @@ -75,6 +75,16 @@ namespace QtUtils |
| 75 | 75 | QString toString(const QVariant &variant); |
| 76 | 76 | QString toString(const QVariantList &variantList); |
| 77 | 77 | |
| 78 | + template <typename T> | |
| 79 | + QVariantList toVariantList(const QList<T> &list) | |
| 80 | + { | |
| 81 | + QVariantList variantList; | |
| 82 | + foreach (const T &item, list) | |
| 83 | + variantList << item; | |
| 84 | + | |
| 85 | + return variantList; | |
| 86 | + } | |
| 87 | + | |
| 78 | 88 | /**** Point Utilities ****/ |
| 79 | 89 | float euclideanLength(const QPointF &point); |
| 80 | 90 | } | ... | ... |
openbr/openbr_plugin.cpp
| ... | ... | @@ -19,6 +19,7 @@ |
| 19 | 19 | #include <QFutureSynchronizer> |
| 20 | 20 | #include <QMetaProperty> |
| 21 | 21 | #include <QPointF> |
| 22 | +#include <QProcess> | |
| 22 | 23 | #include <QRect> |
| 23 | 24 | #include <QRegExp> |
| 24 | 25 | #include <QThreadPool> |
| ... | ... | @@ -40,6 +41,12 @@ |
| 40 | 41 | using namespace br; |
| 41 | 42 | using namespace cv; |
| 42 | 43 | |
| 44 | +// Some globals used to transfer data to Context::messageHandler so that | |
| 45 | +// we can restart the process if we try and fail to create a QApplication. | |
| 46 | +static bool creating_qapp = false; | |
| 47 | +static int * argc_ptr = NULL; | |
| 48 | +static char ** argv_ptr = NULL; | |
| 49 | + | |
| 43 | 50 | /* File - public methods */ |
| 44 | 51 | // Note that the convention for displaying metadata is as follows: |
| 45 | 52 | // [] for lists in which argument order does not matter (e.g. [FTO=false, Index=0]), |
| ... | ... | @@ -218,6 +225,11 @@ void File::appendRect(const QRectF &rect) |
| 218 | 225 | m_metadata["Rects"] = newRects; |
| 219 | 226 | } |
| 220 | 227 | |
| 228 | +void File::appendRect(const Rect &rect) | |
| 229 | +{ | |
| 230 | + appendRect(OpenCVUtils::fromRect(rect)); | |
| 231 | +} | |
| 232 | + | |
| 221 | 233 | void File::appendRects(const QList<QRectF> &rects) |
| 222 | 234 | { |
| 223 | 235 | QList<QVariant> newRects = m_metadata["Rects"].toList(); |
| ... | ... | @@ -226,6 +238,11 @@ void File::appendRects(const QList<QRectF> &rects) |
| 226 | 238 | m_metadata["Rects"] = newRects; |
| 227 | 239 | } |
| 228 | 240 | |
| 241 | +void File::appendRects(const QList<Rect> &rects) | |
| 242 | +{ | |
| 243 | + appendRects(OpenCVUtils::fromRects(rects)); | |
| 244 | +} | |
| 245 | + | |
| 229 | 246 | /* File - private methods */ |
| 230 | 247 | void File::init(const QString &file) |
| 231 | 248 | { |
| ... | ... | @@ -877,14 +894,28 @@ void br::Context::initialize(int &argc, char *argv[], QString sdkPath, bool use_ |
| 877 | 894 | break; |
| 878 | 895 | } |
| 879 | 896 | } |
| 897 | + | |
| 898 | + qInstallMessageHandler(messageHandler); | |
| 899 | + | |
| 880 | 900 | // We take in argc as a reference due to: |
| 881 | 901 | // https://bugreports.qt-project.org/browse/QTBUG-5637 |
| 882 | 902 | // QApplication should be initialized before anything else. |
| 883 | 903 | // Since we can't ensure that it gets deleted last, we never delete it. |
| 884 | 904 | if (QCoreApplication::instance() == NULL) { |
| 885 | 905 | #ifndef BR_EMBEDDED |
| 886 | - if (use_gui) application = new QApplication(argc, argv); | |
| 887 | - else application = new QCoreApplication(argc, argv); | |
| 906 | + if (use_gui) { | |
| 907 | + // Set up variables to be used in the message handler if this fails. | |
| 908 | + // Just so you know, we | |
| 909 | + creating_qapp = true; | |
| 910 | + argc_ptr = &argc; | |
| 911 | + argv_ptr = argv; | |
| 912 | + | |
| 913 | + application = new QApplication(argc, argv); | |
| 914 | + creating_qapp = false; | |
| 915 | + } | |
| 916 | + else { | |
| 917 | + application = new QCoreApplication(argc, argv); | |
| 918 | + } | |
| 888 | 919 | #else |
| 889 | 920 | application = new QCoreApplication(argc, argv); |
| 890 | 921 | #endif |
| ... | ... | @@ -910,7 +941,6 @@ void br::Context::initialize(int &argc, char *argv[], QString sdkPath, bool use_ |
| 910 | 941 | Globals->init(File()); |
| 911 | 942 | Globals->useGui = use_gui; |
| 912 | 943 | |
| 913 | - qInstallMessageHandler(messageHandler); | |
| 914 | 944 | |
| 915 | 945 | Common::seedRNG(); |
| 916 | 946 | |
| ... | ... | @@ -979,6 +1009,26 @@ void br::Context::messageHandler(QtMsgType type, const QMessageLogContext &conte |
| 979 | 1009 | static QMutex generalLock; |
| 980 | 1010 | QMutexLocker locker(&generalLock); |
| 981 | 1011 | |
| 1012 | + // If we are trying to create a QApplication, and get a fatal, then restart the process | |
| 1013 | + // with useGui set to 0. | |
| 1014 | + if (creating_qapp && type == QtFatalMsg) | |
| 1015 | + { | |
| 1016 | + // re-launch process with useGui = 0 | |
| 1017 | + std::cout << "Failed to initialize gui, restarting with -useGui 0" << std::endl; | |
| 1018 | + QStringList arguments; | |
| 1019 | + arguments.append("-useGui"); | |
| 1020 | + arguments.append("0"); | |
| 1021 | + for (int i=1; i < *argc_ptr; i++) | |
| 1022 | + { | |
| 1023 | + arguments.append(argv_ptr[i]); | |
| 1024 | + } | |
| 1025 | + // QProcess::execute blocks until the other process completes. | |
| 1026 | + QProcess::execute(argv_ptr[0], arguments); | |
| 1027 | + // have to unlock this for some reason | |
| 1028 | + locker.unlock(); | |
| 1029 | + std::exit(0); | |
| 1030 | + } | |
| 1031 | + | |
| 982 | 1032 | QString txt; |
| 983 | 1033 | if (type == QtDebugMsg) { |
| 984 | 1034 | if (Globals->quiet) return; | ... | ... |
openbr/plugins/template.cpp
| ... | ... | @@ -28,55 +28,6 @@ BR_REGISTER(Transform, KeepMetadataTransform) |
| 28 | 28 | |
| 29 | 29 | /*! |
| 30 | 30 | * \ingroup transforms |
| 31 | - * \brief Restricts the metadata available within a file | |
| 32 | - * \author Scott Klum \cite sklum | |
| 33 | - */ | |
| 34 | -class RestrictMetadataTransform : public MetaTransform | |
| 35 | -{ | |
| 36 | - Q_OBJECT | |
| 37 | - Q_PROPERTY(QString description READ get_description WRITE set_description RESET reset_description STORED false) | |
| 38 | - BR_PROPERTY(QString, description, "Identity") | |
| 39 | - Q_PROPERTY(QStringList pointSets READ get_pointSets WRITE set_pointSets RESET reset_pointSets STORED false) | |
| 40 | - BR_PROPERTY(QStringList, pointSets, QStringList()) | |
| 41 | - Q_PROPERTY(QStringList rectSets READ get_rectSets WRITE set_rectSets RESET reset_rectSets STORED false) | |
| 42 | - BR_PROPERTY(QStringList, rectSets, QStringList()) | |
| 43 | - | |
| 44 | - br::Transform* transform; | |
| 45 | - | |
| 46 | - void init() | |
| 47 | - { | |
| 48 | - transform = make(description); | |
| 49 | - } | |
| 50 | - | |
| 51 | - void train(const QList<TemplateList> &data) | |
| 52 | - { | |
| 53 | - // Update data to only include metadata from the given set | |
| 54 | - transform->train(data); | |
| 55 | - } | |
| 56 | - | |
| 57 | - void project(const Template &src, Template &dst) const | |
| 58 | - { | |
| 59 | - Template tmp = src; | |
| 60 | - | |
| 61 | - // Change this to clear only one if list isn't empty | |
| 62 | - tmp.file.clearPoints(); tmp.file.clearRects(); | |
| 63 | - | |
| 64 | - foreach(const QString& set, pointSets) tmp.file.appendPoints(src.getList<QPointF>(set)); | |
| 65 | - foreach(const QString& set, rectSets) tmp.file.appendRects(src.getList<QRectF>(set)); | |
| 66 | - | |
| 67 | - // Put a template through some transforms with the metadata restriction | |
| 68 | - transform->project(tmp, dst); | |
| 69 | - | |
| 70 | - // Keep original points/rects | |
| 71 | - dst.file.appendPoints(src.file.points()); | |
| 72 | - dst.file.appendRects(src.file.rects()); | |
| 73 | - } | |
| 74 | -}; | |
| 75 | - | |
| 76 | -BR_REGISTER(Transform, RestrictMetadataTransform) | |
| 77 | - | |
| 78 | -/*! | |
| 79 | - * \ingroup transforms | |
| 80 | 31 | * \brief Remove templates with the specified file extension or metadata value. |
| 81 | 32 | * \author Josh Klontz \cite jklontz |
| 82 | 33 | */ | ... | ... |