123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #include "LoadAlgorithm.h"
- #include "rapidjson/document.h"
- #include "rapidjson/writer.h"
- #include "rapidjson/stringbuffer.h"
- #include <codecvt>
- #include <string>
- #include <QtCore/qfile.h>
- #include <QtCore/qjsondocument.h>
- #include <qjsonobject.h>
- #include <iostream>
- #include <Windows.h>
- #include <QCoreApplication>
- #include <QtCore/QTextStream>
- #include "WriteLog.h"
- #include "JdydAlgorithnm.h"
- #include "YunDaISASImageRecognitionService.h"
- #include <qfileinfo.h>
- //#pragma execution_character_set("utf-8")
- using namespace rapidjson;
- using namespace std;
- LoadAlgorithm::LoadAlgorithm()
- {
- //plugins = new QMap<QString, AlgorithmModel*>();
- }
- 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;
- }
|