LoadAlgorithm.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "LoadAlgorithm.h"
  2. #include "rapidjson/document.h"
  3. #include "rapidjson/writer.h"
  4. #include "rapidjson/stringbuffer.h"
  5. #include <codecvt>
  6. #include <string>
  7. #include <QtCore/qfile.h>
  8. #include <QtCore/qjsondocument.h>
  9. #include <qjsonobject.h>
  10. #include <iostream>
  11. #include <Windows.h>
  12. #include <QCoreApplication>
  13. #include <QtCore/QTextStream>
  14. #include "WriteLog.h"
  15. #include "JdydAlgorithnm.h"
  16. #include "YunDaISASImageRecognitionService.h"
  17. #include <qfileinfo.h>
  18. //#pragma execution_character_set("utf-8")
  19. using namespace rapidjson;
  20. using namespace std;
  21. LoadAlgorithm::LoadAlgorithm()
  22. {
  23. //plugins = new QMap<QString, AlgorithmModel*>();
  24. }
  25. LoadAlgorithm::~LoadAlgorithm()
  26. {
  27. }
  28. bool LoadAlgorithm::Init()
  29. {
  30. //auto jsonpath = "D:/Project/qtclient/巡检服务/Yunda.ISAS.QT.DevFramework.Branch_2022.04.02_增加报警信息/bin/x64/Release/图像识别配置/AlgorithmConfiguration.json";
  31. try
  32. {
  33. /*std::thread([](int value) {
  34. }).detach();
  35. */
  36. auto jsonpath = QStringLiteral("图像识别配置/AlgorithmConfiguration.json");
  37. if (initialAlgorithmConfiguration(jsonpath))//老算法
  38. {
  39. QString retStr;
  40. QVariantMap typeMap = algorithmConfigurationTable.value(QStringLiteral("算法类型")).toMap();
  41. QVariantMap::iterator it;
  42. for (it = typeMap.begin(); it != typeMap.end(); it++)
  43. {
  44. auto list = it.value().toMap();
  45. QVariantMap::iterator itor;
  46. for (itor = list.begin(); itor != list.end(); itor++)
  47. {
  48. auto pluginPath = itor.value().toMap().value("plugin").toString();
  49. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  50. LoadAllAlgorithm(pluginPath);
  51. }
  52. YunDaISASImageRecognitionService::ConsoleLog(it.key() + " loaded");
  53. }
  54. }
  55. if (JdydAlgorithnm::Init())//新状态识别算法
  56. {
  57. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("模型加载完成"));
  58. }
  59. return true;
  60. }
  61. catch (const std::exception& ex)
  62. {
  63. YunDaISASImageRecognitionService::ConsoleLog(ex.what());
  64. }
  65. return false;
  66. }
  67. bool LoadAlgorithm::initialAlgorithmConfiguration(QString jsonPath)
  68. {
  69. QString jsonFilePath = jsonPath;
  70. QFile jsonFile(jsonFilePath);
  71. if (!jsonFile.open(QIODevice::ReadOnly)) {
  72. YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("找不到识别配置文件:AlgorithmConfiguration.json\r\n京张使用其他方式") );
  73. return false;
  74. }
  75. QByteArray allData = jsonFile.readAll();
  76. jsonFile.close();
  77. QJsonParseError json_error;
  78. QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
  79. if (json_error.error != QJsonParseError::NoError) {
  80. //qWarning() << json_error.errorString();
  81. return false;
  82. }
  83. QJsonObject rootObj = jsonDoc.object();
  84. algorithmConfigurationTable = rootObj.toVariantMap();
  85. return true;
  86. //qWarning() << algorithmConfigurationTable;
  87. }
  88. bool LoadAlgorithm::LoadAllAlgorithm(QString pluginName)
  89. {
  90. QString currentPath = QCoreApplication::applicationDirPath();
  91. QFileInfo fileInfo(currentPath+"//AlgorithmPlugin//"+pluginName+".dll");
  92. if (!fileInfo.exists())
  93. {
  94. return true;
  95. }
  96. QString retStr = "";
  97. //QString pluginName = "";
  98. QString resultsFile = "";
  99. //如果不包含则创建
  100. QLibrary* lib = new QLibrary(pluginName);
  101. PLUGIN_LIB_ENTRY func = (PLUGIN_LIB_ENTRY)lib->resolve(ENTRY_FUNCTION);
  102. if (func == nullptr) {
  103. QString strErr = lib->errorString();
  104. retStr = pluginName + QString(" ERROR:") + strErr;
  105. YunDaISASImageRecognitionService::ConsoleLog(retStr);
  106. //qWarning() << retStr;
  107. }
  108. else
  109. {
  110. try {
  111. AlgorithmModel* algorithmModel = (AlgorithmModel*)func();
  112. if (!algorithmModel) {
  113. retStr = QStringLiteral("入口函数获取实例失败!").arg(pluginName).arg(resultsFile);
  114. //return retStr;
  115. }
  116. else
  117. {
  118. delete lib;
  119. algorithmModel->initial();
  120. m_plugins.insert(pluginName.toStdString(), algorithmModel);
  121. YunDaISASImageRecognitionService::ConsoleLog("success loaded "+ pluginName);
  122. }
  123. }
  124. catch (const std::exception& e) {
  125. retStr = QStringLiteral("算法配置加载异常%1,%2,请检查%3").arg(pluginName).arg(resultsFile).arg(e.what());
  126. YunDaISASImageRecognitionService::ConsoleLog(retStr);
  127. delete m_plugins.take(pluginName.toStdString());
  128. }
  129. catch (...) {
  130. retStr = QStringLiteral("算法配置加载异常%1,%2,请检查").arg(pluginName).arg(resultsFile);
  131. YunDaISASImageRecognitionService::ConsoleLog(retStr);
  132. delete m_plugins.take(pluginName.toStdString());
  133. }
  134. }
  135. return false;
  136. }