From 31ff1732fd80b271e80b235cb3d28b5fb906b064 Mon Sep 17 00:00:00 2001 From: APTX Date: Wed, 23 Feb 2022 21:35:21 +0900 Subject: [PATCH] Cleaner shutdown Making net owned by the plugin instance is somewhat problematic. It's only used on the worker thread, which might run when the instance is deleted. Also fixes a compile issue that wasn't present on windows/msvc. --- .../featureannotations.cpp | 54 ++++++++++--------- .../feature_annotations/featureannotations.h | 6 ++- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/featureplugins/feature_annotations/featureannotations.cpp b/featureplugins/feature_annotations/featureannotations.cpp index 2fc2daf..d2ecfe9 100644 --- a/featureplugins/feature_annotations/featureannotations.cpp +++ b/featureplugins/feature_annotations/featureannotations.cpp @@ -2,20 +2,21 @@ #include #include +#include +#include -Q_LOGGING_CATEGORY(annotationsCategory, "Annotations") -Q_LOGGING_CATEGORY(annotationsStatsCategory, "Annotations.stats") -Q_LOGGING_CATEGORY(annotationsVerboseCategory, "Annotations.verbose") +#include #include #include -#include -#include - // random windows.h defines #undef interface +Q_LOGGING_CATEGORY(annotationsCategory, "Annotations") +Q_LOGGING_CATEGORY(annotationsStatsCategory, "Annotations.stats") +Q_LOGGING_CATEGORY(annotationsVerboseCategory, "Annotations.verbose") + using namespace std; using namespace dlib; @@ -34,32 +35,33 @@ using detection_net_type = rcon5_r>>>>>>>; -namespace { -detection_net_type net; -bool valid = false; -} // namespace +struct FeatureAnnoationsInstance::Private +{ + detection_net_type net; +}; FeatureAnnoations::FeatureAnnoations(QObject *parent) : QObject{parent} { - try { - deserialize("C:/_C/anime_face_recognition/mmod_network.dat") >> net; - valid = true; - } catch (const std::exception &ex) { - qCWarning(annotationsCategory) - << "Failed to read neural network. Error:" << ex.what(); - } } FeaturePluginInstance * FeatureAnnoations::createInstance(QObject *instance, PlayerFeaturePluginInterface *interface) { - if (!valid) - throw std::exception{"Network failed to initialize"}; return new FeatureAnnoationsInstance(instance, interface); } FeatureAnnoationsInstance::FeatureAnnoationsInstance( QObject *instance, PlayerFeaturePluginInterface *player, QObject *) - : FeaturePluginInstance{instance, player} { + : FeaturePluginInstance{instance, player}, + m_private{std::make_unique()} { + try { + deserialize("/mnt/windows/_C/anime_face_recognition/" + "mmod_network-1000-labeled-adjusted.dat") >> + m_private->net; + } catch (const std::exception &ex) { + qCWarning(annotationsCategory) + << "Failed to read neural network. Error:" << ex.what(); + throw; + } qCDebug(annotationsCategory) << "Registering with instance" << instance; connect(&m_watcher, SIGNAL(finished()), this, SLOT(onResultReady())); @@ -76,10 +78,11 @@ FeatureAnnoationsInstance::FeatureAnnoationsInstance( toggleAnnotations(); } +FeatureAnnoationsInstance::~FeatureAnnoationsInstance() = default; -QList FeatureAnnoationsInstance::featurePluginActions() const -{ - return {Action{"Enable annotations", "P", SLOT()}}; +QList +FeatureAnnoationsInstance::featurePluginActions() const { + return {Action{"Enable annotations", "P", SLOT()}}; } void FeatureAnnoationsInstance::onFrameChanged(const QImage &image) { @@ -93,8 +96,9 @@ void FeatureAnnoationsInstance::onFrameChanged(const QImage &image) { qCDebug(annotationsVerboseCategory) << "Starting annotation detection..."; + auto &prv = *m_private; auto future = QtConcurrent::run( - [](QImage image) -> PlayerFeaturePluginInterface::AnnotationList { + [&prv](QImage image) -> PlayerFeaturePluginInterface::AnnotationList { try { const int maxHeight{600}; if (image.size().height() > maxHeight) @@ -114,7 +118,7 @@ void FeatureAnnoationsInstance::onFrameChanged(const QImage &image) { // dlib::save_bmp(img, "testimage.bmp"); - auto dets = net(img); + auto dets = prv.net(img); PlayerFeaturePluginInterface::AnnotationList al; for (auto &&d : dets) al << PlayerFeaturePluginInterface::Annotation{ diff --git a/featureplugins/feature_annotations/featureannotations.h b/featureplugins/feature_annotations/featureannotations.h index d04c9b9..d0403b7 100644 --- a/featureplugins/feature_annotations/featureannotations.h +++ b/featureplugins/feature_annotations/featureannotations.h @@ -30,7 +30,9 @@ public: class FeatureAnnoationsInstance : public QObject, public FeaturePluginInstance { Q_OBJECT public: - FeatureAnnoationsInstance(QObject *instance, PlayerFeaturePluginInterface *, QObject *parent = nullptr); + FeatureAnnoationsInstance(QObject *instance, PlayerFeaturePluginInterface *, + QObject *parent = nullptr); + ~FeatureAnnoationsInstance() override; // FeaturePluginInstance interface QList featurePluginActions() const override; @@ -46,6 +48,8 @@ private: void toggleAnnotations(); bool m_enabled = false; + struct Private; + std::unique_ptr m_private; QTimer m_fpsTimer; int m_fpsCounter = 0; QFutureWatcher m_watcher; -- 2.52.0