#include "LoadAlgorithm.h" #include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include #include #include #include #include #include #include #include #include #include "WriteLog.h" #include "JdydAlgorithnm.h" #include "YunDaISASImageRecognitionService.h" #include //#pragma execution_character_set("utf-8") using namespace rapidjson; using namespace std; LoadAlgorithm::LoadAlgorithm() { //plugins = new QMap(); } LoadAlgorithm::~LoadAlgorithm() { } bool LoadAlgorithm::Init() { //auto jsonpath = "D:/Project/qtclient/巡检服务/Yunda.ISAS.QT.DevFramework.Branch_2022.04.02_增加报警信息/bin/x64/Release/图像识别配置/AlgorithmConfiguration.json"; try { /*std::thread([](int value) { }).detach(); */ auto jsonpath = QStringLiteral("图像识别配置/AlgorithmConfiguration.json"); if (initialAlgorithmConfiguration(jsonpath))//老算法 { QString retStr; QVariantMap typeMap = algorithmConfigurationTable.value(QStringLiteral("算法类型")).toMap(); QVariantMap::iterator it; for (it = typeMap.begin(); it != typeMap.end(); it++) { auto list = it.value().toMap(); QVariantMap::iterator itor; for (itor = list.begin(); itor != list.end(); itor++) { auto pluginPath = itor.value().toMap().value("plugin").toString(); QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); LoadAllAlgorithm(pluginPath); } YunDaISASImageRecognitionService::ConsoleLog(it.key() + " loaded"); } } if (JdydAlgorithnm::Init())//新算法 { YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("模型加载完成")); } return true; } catch (const std::exception& ex) { YunDaISASImageRecognitionService::ConsoleLog(ex.what()); } return false; } bool LoadAlgorithm::initialAlgorithmConfiguration(QString jsonPath) { QString jsonFilePath = jsonPath; QFile jsonFile(jsonFilePath); if (!jsonFile.open(QIODevice::ReadOnly)) { YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("找不到识别配置文件:AlgorithmConfiguration.json\r\n京张使用其他方式") ); return false; } QByteArray allData = jsonFile.readAll(); jsonFile.close(); QJsonParseError json_error; QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error)); if (json_error.error != QJsonParseError::NoError) { //qWarning() << json_error.errorString(); return false; } QJsonObject rootObj = jsonDoc.object(); algorithmConfigurationTable = rootObj.toVariantMap(); return true; //qWarning() << algorithmConfigurationTable; } bool LoadAlgorithm::LoadAllAlgorithm(QString pluginName) { QString currentPath = QCoreApplication::applicationDirPath(); QFileInfo fileInfo(currentPath+"//AlgorithmPlugin//"+pluginName+".dll"); if (!fileInfo.exists()) { return true; } QString retStr = ""; //QString pluginName = ""; QString resultsFile = ""; //如果不包含则创建 QLibrary* lib = new QLibrary(pluginName); PLUGIN_LIB_ENTRY func = (PLUGIN_LIB_ENTRY)lib->resolve(ENTRY_FUNCTION); if (func == nullptr) { QString strErr = lib->errorString(); retStr = pluginName + QString(" ERROR:") + strErr; YunDaISASImageRecognitionService::ConsoleLog(retStr); //qWarning() << retStr; } else { try { AlgorithmModel* algorithmModel = (AlgorithmModel*)func(); if (!algorithmModel) { retStr = QStringLiteral("入口函数获取实例失败!").arg(pluginName).arg(resultsFile); //return retStr; } else { delete lib; algorithmModel->initial(); m_plugins.insert(pluginName.toStdString(), algorithmModel); YunDaISASImageRecognitionService::ConsoleLog("success loaded "+ pluginName); } } catch (const std::exception& e) { retStr = QStringLiteral("算法配置加载异常%1,%2,请检查%3").arg(pluginName).arg(resultsFile).arg(e.what()); YunDaISASImageRecognitionService::ConsoleLog(retStr); delete m_plugins.take(pluginName.toStdString()); } catch (...) { retStr = QStringLiteral("算法配置加载异常%1,%2,请检查").arg(pluginName).arg(resultsFile); YunDaISASImageRecognitionService::ConsoleLog(retStr); delete m_plugins.take(pluginName.toStdString()); } } return false; }