#include "Algorithm.h" #include #pragma execution_character_set("utf-8") CDetectorAlgorithm::CDetectorAlgorithm() : m_mutex(QReadWriteLock::Recursive) { m_nID = LP_DETECTOR_INVALID_ID; m_nParamBaseID = 0; m_nOutParamBaseID = 0; m_vecParams.clear(); m_vecOutParams.clear(); m_mapParams.clear(); m_mapOutParams.clear(); m_pAlgo = NULL; m_pTask = NULL; m_bIsFinished = false; m_bIsExec = true; m_dExecTime = 0.0; } CDetectorAlgorithm::~CDetectorAlgorithm() { CleanUp(); } bool CDetectorAlgorithm::Initialize(IDetectorTask* pTask, IAlgorithmLibMgr* pAlgoLibMgr) { if (!pTask) return false; m_pTask = pTask; m_pAlgoLibMgr = pAlgoLibMgr; return true; } void CDetectorAlgorithm::SetID(int nID) { m_nID = nID; } int CDetectorAlgorithm::GetID() const { return m_nID; } int CDetectorAlgorithm::GetParamBaseID() const { return m_nParamBaseID; } int CDetectorAlgorithm::GetOutParamBaseID() const { return m_nOutParamBaseID; } void CDetectorAlgorithm::Release() { delete this; } void CDetectorAlgorithm::CleanUp() { for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; QString str = p->strName; if (NULL != p) { delete p; } } m_vecParams.clear(); for (QMap::iterator iter = m_vecOutParams.begin(); iter != m_vecOutParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; if (NULL != p) { delete p; } } m_vecOutParams.clear(); if (m_pAlgo) { delete m_pAlgo; m_pAlgo = NULL; } if (m_lib.isLoaded()) { m_lib.unload(); } } bool CDetectorAlgorithm::SetAlgorithmInfo(const PLP_DETECTOR_ALGORITHM pAlgorithmInfo) { if (!pAlgorithmInfo) return false; m_tAlgorithmInfo.nID = pAlgorithmInfo->nID; m_tAlgorithmInfo.strName = pAlgorithmInfo->strName; m_tAlgorithmInfo.strDescription = pAlgorithmInfo->strDescription; m_tAlgorithmInfo.nRoiID = pAlgorithmInfo->nRoiID; m_tAlgorithmInfo.strPath = pAlgorithmInfo->strPath; m_tAlgorithmInfo.strLibName = pAlgorithmInfo->strLibName; m_tAlgorithmInfo.strFuncName = pAlgorithmInfo->strFuncName; return true; } PLP_DETECTOR_ALGORITHM CDetectorAlgorithm::GetAlgorithmInfo() { return &m_tAlgorithmInfo; } PLP_ALGORITHM_PARAM CDetectorAlgorithm::AddParam(const PLP_ALGORITHM_PARAM pParam, bool bRet /* = true */) {//添加输入参数 if (NULL == pParam) return NULL; if (bRet){ LP_ALGORITHM_PARAM* p = new LP_ALGORITHM_PARAM; p->nID = ++m_nParamBaseID; p->nTaskID = pParam->nTaskID; p->nSrcAlgoID = pParam->nSrcAlgoID; p->nSrcParamID = pParam->nSrcParamID; p->strName = pParam->strName; p->type = pParam->type; p->value = pParam->value; p->strDescription = pParam->strDescription; p->bIsSave = pParam->bIsSave; p->bIsLock = pParam->bIsLock; //m_vecParams.push_back(p); m_mapParams.insert(p->strName, p); m_vecParams[p->strName] = p; return p; } else { if (m_vecParams.contains(pParam->strName)) { QMap::iterator its= m_vecParams.find(pParam->strName); (*its)->nTaskID = pParam->nTaskID; (*its)->nSrcAlgoID = pParam->nSrcAlgoID; (*its)->nSrcParamID = pParam->nSrcParamID; (*its)->value = pParam->value; //(*its)->strDescription = pParam->strDescription; (*its)->bIsSave = pParam->bIsSave; (*its)->bIsLock = pParam->bIsLock; if (m_mapParams.contains(pParam->strName)) m_mapParams[pParam->strName] = (*its); return (*its); } } return NULL; } bool CDetectorAlgorithm::DeleteParam(int nParamID) {//删除输入参数 for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; if (NULL != p) { if (p->nID == nParamID) { iter = m_vecParams.erase(iter); m_mapParams.remove(p->strName); delete *iter; return true; } } } return false; } PLP_ALGORITHM_PARAM CDetectorAlgorithm::GetParam(int nParamID) { for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { if (NULL != *iter) { PLP_ALGORITHM_PARAM p = *iter; if (p->nID == nParamID) return p; } } return NULL; } int CDetectorAlgorithm::EnumParam(LP_ALGORITHM_PARAM** lppParam, int nCount) { if (!lppParam || nCount < m_vecParams.size()) return m_vecParams.size(); int nIndex = 0; for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { if (NULL != *iter) { lppParam[nIndex] = *iter; nIndex++; } } return nIndex; } PLP_ALGORITHM_PARAM CDetectorAlgorithm::AddOutParam(const PLP_ALGORITHM_PARAM pParam, bool bRet /* = true */) { if (NULL == pParam) return NULL; if (bRet)//添加模式 { PLP_ALGORITHM_PARAM p = new LP_ALGORITHM_PARAM; p->nID = ++m_nOutParamBaseID; p->nTaskID = pParam->nTaskID; p->nSrcAlgoID = pParam->nSrcAlgoID; p->nSrcParamID = pParam->nSrcParamID; p->strName = pParam->strName; p->type = pParam->type; p->value = pParam->value; p->strDescription = pParam->strDescription; p->bIsSave = pParam->bIsSave; m_vecOutParams[p->strName] = p; m_mapOutParams.insert(p->strName, p); return p; } else//修改输出参数模式 { if (m_vecOutParams.contains(pParam->strName)) { QMap::iterator its = m_vecOutParams.find(pParam->strName); (*its)->nSrcAlgoID = pParam->nTaskID; (*its)->nSrcAlgoID = pParam->nSrcAlgoID; (*its)->nSrcParamID = pParam->nSrcParamID; (*its)->value = pParam->value; //(*its)->strDescription = pParam->strDescription; (*its)->bIsSave = pParam->bIsSave; m_mapOutParams[pParam->strName] = (*its); return (*its); } } return NULL; } bool CDetectorAlgorithm::DeleteOutParam(int nOutParamID) { for (QMap::iterator iter = m_vecOutParams.begin(); iter != m_vecOutParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; if (NULL != p) { if (p->nID == nOutParamID) { iter = m_vecOutParams.erase(iter); m_mapOutParams.remove(p->strName); delete *iter; return true; } } } return false; } PLP_ALGORITHM_PARAM CDetectorAlgorithm::GetOutParam(int nOutParamID) { for (QMap::iterator iter = m_vecOutParams.begin(); iter != m_vecOutParams.end(); ++iter) { if (NULL != *iter) { PLP_ALGORITHM_PARAM p = *iter; if (p->nID == nOutParamID) return p; } } return NULL; } int CDetectorAlgorithm::EnumOutParam(LP_ALGORITHM_PARAM** lppOutParam, int nCount) { if (!lppOutParam || nCount < m_vecOutParams.size()) return m_vecOutParams.size(); int nIndex = 0; for (QMap::iterator iter = m_vecOutParams.begin(); iter != m_vecOutParams.end(); ++iter) { if (NULL != *iter) { lppOutParam[nIndex] = *iter; nIndex++; } } return nIndex; } bool CDetectorAlgorithm::SerializeParamPropertyToJson(QJsonObject* pJsonObject, QVariantMap& vProperty) { if (!pJsonObject) return false; if (vProperty.empty()) return true; QJsonArray PropertyArray; for (QMap::iterator iter = vProperty.begin(); iter != vProperty.end(); ++iter) { if (NULL != *iter) { QJsonObject propertys; propertys.insert("prop_name", iter.key()); propertys.insert("prop_type", iter.value().type()); switch (iter.value().type()) { case QMetaType::Bool: { propertys.insert("prop_value", iter.value().toBool()); break; } case QMetaType::Int: { propertys.insert("prop_value", iter.value().toInt()); break; } case QMetaType::Double: { propertys.insert("prop_value", iter.value().toDouble()); break; } case QMetaType::QString: { propertys.insert("prop_value", iter.value().toString()); break; } default: break; } PropertyArray.push_back(propertys); } } pJsonObject->insert("property", PropertyArray); return true; } bool CDetectorAlgorithm::SerializeToJson(QJsonDocument* pJsonDocument, QJsonArray* pAlgorithmArray) { QJsonObject jsonObject; jsonObject.insert("algo_name", m_tAlgorithmInfo.strName); jsonObject.insert("algo_description", m_tAlgorithmInfo.strDescription); jsonObject.insert("algo_path", m_tAlgorithmInfo.strPath); jsonObject.insert("algo_roi_id", m_tAlgorithmInfo.nRoiID); jsonObject.insert("algo_id", m_nID); jsonObject.insert("algo_base_id", m_nParamBaseID); jsonObject.insert("algo_OutParam_base_id", m_nOutParamBaseID); jsonObject.insert("algo_lib_name", m_tAlgorithmInfo.strLibName); jsonObject.insert("algo_func_name", m_tAlgorithmInfo.strFuncName); jsonObject.insert("algo_exec_enabled", m_bIsExec); QJsonArray paramArray; for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { if (*iter) { PLP_ALGORITHM_PARAM p = (PLP_ALGORITHM_PARAM)*iter; QJsonObject jsonParam; jsonParam.insert("param_id", p->nID); jsonParam.insert("param_src_task_id", p->nTaskID); jsonParam.insert("param_src_algo_id", p->nSrcAlgoID); jsonParam.insert("param_src_param_id", p->nSrcParamID); jsonParam.insert("param_name", p->strName); jsonParam.insert("param_type", p->type); jsonParam.insert("param_lock", p->bIsLock); switch (p->type) { case LP_INT: { jsonParam.insert("param_value", p->value.toInt()); break; } case LP_BOOLEAN: { jsonParam.insert("param_value", p->value.toBool()); break; } case LP_STRING: { jsonParam.insert("param_value", p->value.toString()); break; } case LP_DOUBLE: { jsonParam.insert("param_value", p->value.toDouble()); break; } case LP_POINT: { jsonParam.insert("param_value_point_1", p->value.toPoint().x()); jsonParam.insert("param_value_point_2", p->value.toPoint().y()); break; } case LP_POINTF: { jsonParam.insert("param_value_point_1", p->value.toPointF().x()); jsonParam.insert("param_value_point_2", p->value.toPointF().y()); break; } case LP_IMAGE: { break; } case LP_RECT: { QPolygonF polygon = p->value.value(); jsonParam.insert("param_value_ptx0", polygon.at(0).x()); jsonParam.insert("param_value_pty0", polygon.at(0).y()); jsonParam.insert("param_value_ptx1", polygon.at(1).x()); jsonParam.insert("param_value_pty1", polygon.at(1).y()); jsonParam.insert("param_value_ptx2", polygon.at(2).x()); jsonParam.insert("param_value_pty2", polygon.at(2).y()); jsonParam.insert("param_value_ptx3", polygon.at(3).x()); jsonParam.insert("param_value_pty3", polygon.at(3).y()); break; } case LP_TRANSFORM: { QTransform tranform = p->value.value(); jsonParam.insert("param_value_m11", tranform.m11()); jsonParam.insert("param_value_m12", tranform.m12()); jsonParam.insert("param_value_m13", tranform.m13()); jsonParam.insert("param_value_m21", tranform.m21()); jsonParam.insert("param_value_m22", tranform.m22()); jsonParam.insert("param_value_m23", tranform.m23()); jsonParam.insert("param_value_m31", tranform.m31()); jsonParam.insert("param_value_m32", tranform.m32()); jsonParam.insert("param_value_m33", tranform.m33()); break; } case LP_MAT: { cv::Mat matResult = p->value.value(); //QString strExePath = QCoreApplication::applicationDirPath(); QString strFilePath = m_pTask->GetPath();// strExePath + LP_DETECTOR_BUSSINESS_IN_PARAM_FILE_DIR; QString strName = QString("%1_%2_%3_%4").arg(m_pTask->GetParentSolution()->GetID()).arg(m_pTask->GetID()).arg(m_nID).arg(p->nID); QString strFileName = strName + "_in" + ".bmp"; QString strFileName2 = strFileName; strFileName2.replace(".bmp", ".wsf"); QString strFilePath2 = strFilePath + strFileName2; strFilePath += strFileName; jsonParam.insert("param_file_path", strFileName2); std::string strpath = strFilePath.toLocal8Bit().toStdString(); if (!cv::imwrite(strpath/*strFilePath.toLatin1().data()*/, matResult)) break; QFile(strFilePath2).remove(); QFile::rename(strFilePath, strFilePath2); QFile(strFilePath).remove(); if (m_pTask) m_pTask->AppendOtherFile(strFilePath2); break; } case LP_ROI: { LP_DETECTOR_ROI_DATA roiData = p->value.value(); int nCount = roiData.records.size(); if (nCount > 0) { QJsonArray recordArray; for (int i = 0; i < nCount; i++) { QJsonArray itemArray; Item_List record = roiData.records.at(i); int nItemCount = record.size(); for (int j = 0; j < nItemCount; j++) { QJsonObject itemJson; Feature_List item = record.at(j); itemJson.insert("roi_item_type", item.first); switch (item.first) { case RECTANGLE: case ELLIPSE: { itemJson.insert("roi_item_value1", item.second.at(0)); itemJson.insert("roi_item_value2", item.second.at(1)); itemJson.insert("roi_item_value3", item.second.at(2)); itemJson.insert("roi_item_value4", item.second.at(3)); break; } case CIRCLE: { itemJson.insert("roi_item_value1", item.second.at(0)); itemJson.insert("roi_item_value2", item.second.at(1)); itemJson.insert("roi_item_value3", item.second.at(2)); break; } case POLY: { break; } default: break; } itemArray.push_back(itemJson); } recordArray.push_back(itemArray); } jsonParam.insert("param_value", recordArray); } break; } case LP_LINEF: case LP_RECTF: case LP_POLYGONF: { QPolygonF polygon = p->value.value(); for (int i = 0; i < polygon.size(); i++) { QString strPtKey = QString("%1%2").arg("param_value_pt").arg(QString::number(i)); QString strPtValue = QString("%1,%2").arg(QString::number(polygon.at(i).x())).arg(QString::number(polygon.at(i).y())); jsonParam.insert(strPtKey, strPtValue); } break; } case LP_USER: { if (m_pAlgo) { m_pAlgo->SaveUserParamValue(m_pTask, this, p->strName); } else { if (m_pAlgoLibMgr) { IAlgorithmLib* pAlgoLib = m_pAlgoLibMgr->GetAlgorithmLib(m_tAlgorithmInfo.strLibName); if (pAlgoLib) { pAlgoLib->SaveUserParamValue(m_pTask, this, p->strName); } } } break; } default: break; } jsonParam.insert("param_description", p->strDescription); SerializeParamPropertyToJson(&jsonParam, p->vProperty); paramArray.push_back(jsonParam); } } if (paramArray.size() > 0) jsonObject.insert("paramArray", paramArray); QJsonArray outParamArray; for (QMap::iterator iter = m_vecOutParams.begin(); iter != m_vecOutParams.end(); ++iter) { if (*iter) { PLP_ALGORITHM_PARAM p = (PLP_ALGORITHM_PARAM)*iter; QJsonObject jsonParam; jsonParam.insert("out_param_id", p->nID); jsonParam.insert("out_param_dest_task_id", p->nTaskID); jsonParam.insert("out_param_dest_algo_id", p->nSrcAlgoID); jsonParam.insert("out_param_dest_param_id", p->nSrcParamID); jsonParam.insert("out_param_name", p->strName); jsonParam.insert("out_param_type", p->type); jsonParam.insert("out_param_description", p->strDescription); jsonParam.insert("out_param_save", p->bIsSave); switch (p->type) { case LP_INT: { jsonParam.insert("out_param_value", p->value.toInt()); break; } case LP_BOOLEAN: { jsonParam.insert("out_param_value", p->value.toBool()); break; } case LP_STRING: { jsonParam.insert("out_param_value", p->value.toString()); break; } case LP_DOUBLE: { jsonParam.insert("out_param_value", p->value.toDouble()); break; } case LP_IMAGE: { break; } case LP_MAT: { break; } case LP_ROI: { LP_DETECTOR_ROI_DATA roiData = p->value.value(); int nCount = roiData.records.size(); if (nCount > 0) { QJsonArray recordArray; for (int i = 0; i < nCount; i++) { QJsonArray itemArray; Item_List record = roiData.records.at(i); int nItemCount = record.size(); for (int j = 0; j < nItemCount; j++) { QJsonObject itemJson; Feature_List item = record.at(j); itemJson.insert("roi_item_type", item.first); switch (item.first) { case RECTANGLE: case ELLIPSE: { itemJson.insert("roi_item_value1", item.second.at(0)); itemJson.insert("roi_item_value2", item.second.at(1)); itemJson.insert("roi_item_value3", item.second.at(2)); itemJson.insert("roi_item_value4", item.second.at(3)); break; } case CIRCLE: { itemJson.insert("roi_item_value1", item.second.at(0)); itemJson.insert("roi_item_value2", item.second.at(1)); itemJson.insert("roi_item_value3", item.second.at(2)); break; } case POLY: { break; } default: break; } itemArray.push_back(itemJson); } recordArray.push_back(itemArray); } jsonParam.insert("out_param_value", recordArray); } break; } case LP_POLYGONF: { break; } case LP_USER: { if (m_pAlgo) { m_pAlgo->SaveUserParamValue(m_pTask, this, p->strName); } else { if (m_pAlgoLibMgr) { IAlgorithmLib* pAlgoLib = m_pAlgoLibMgr->GetAlgorithmLib(m_tAlgorithmInfo.strLibName); if (pAlgoLib) { pAlgoLib->SaveUserParamValue(m_pTask, this, p->strName); } } } break; } default: break; } outParamArray.push_back(jsonParam); } } if (outParamArray.size() > 0) jsonObject.insert("outParamArray", outParamArray); pAlgorithmArray->push_back(jsonObject); return true; } bool CDetectorAlgorithm::SerializeParamPropertyFromJson(QJsonObject* pJsonObject, QVariantMap& vProperty) { if (!pJsonObject) return false; if (!pJsonObject->contains("property")) return true; vProperty.clear(); QJsonArray propertyArray = pJsonObject->value("property").toArray(); for (int i = 0; i < propertyArray.size(); i++) { QJsonObject propertyObject = propertyArray[i].toObject(); switch (propertyObject.value("prop_type").toInt()) { case QMetaType::Bool: { vProperty.insert(propertyObject.value("prop_name").toString(), propertyObject.value("prop_value").toBool()); break; } case QMetaType::Int: { vProperty.insert(propertyObject.value("prop_name").toString(), propertyObject.value("prop_value").toInt()); break; } case QMetaType::Double: { vProperty.insert(propertyObject.value("prop_name").toString(), propertyObject.value("prop_value").toDouble()); break; } case QMetaType::QString: { vProperty.insert(propertyObject.value("prop_name").toString(), propertyObject.value("prop_value").toString()); break; } default: break; } } return true; } bool CDetectorAlgorithm::SerializeFromJson(QJsonObject* pJsonObject) { m_nID = pJsonObject->value("algo_id").toInt(); if (m_tAlgorithmInfo.strPath.size() > 0) { Load(m_tAlgorithmInfo.strPath.left(m_tAlgorithmInfo.strPath.lastIndexOf("."))); } else { Load(m_tAlgorithmInfo.strName); } if (m_Init == false) { if (m_pAlgo) { m_pAlgo->Init(m_pTask, this); m_Init = true; } } QJsonArray paramArray = pJsonObject->value("paramArray").toArray(); for (int i = 0; i < paramArray.size(); i++) { LP_ALGORITHM_PARAM param; QJsonObject jsonObject = paramArray[i].toObject(); param.nID = jsonObject.value("param_id").toInt(); param.nTaskID = jsonObject.value("param_src_task_id").toInt(LP_DETECTOR_INVALID_ID); param.nSrcAlgoID = jsonObject.value("param_src_algo_id").toInt(); param.nSrcParamID = jsonObject.value("param_src_param_id").toInt(); param.strName = jsonObject.value("param_name").toString(); param.type = (AlgoParamType)jsonObject.value("param_type").toInt(); param.bIsLock = jsonObject.value("param_lock").toBool(); switch (param.type) { case LP_INT: { param.value = jsonObject.value("param_value").toInt(); break; } case LP_BOOLEAN: { param.value = jsonObject.value("param_value").toBool(); break; } case LP_STRING: { param.value = jsonObject.value("param_value").toString(); break; } case LP_DOUBLE: { param.value = jsonObject.value("param_value").toDouble(); break; } case LP_POINT: { QPoint pt; pt.setX(jsonObject.value("param_value_point_1").toInt()); pt.setY(jsonObject.value("param_value_point_2").toInt()); param.value = pt; break; } case LP_POINTF: { QPointF pt; pt.setX(jsonObject.value("param_value_point_1").toDouble()); pt.setY(jsonObject.value("param_value_point_2").toDouble()); param.value = pt; break; } case LP_IMAGE: { break; } case LP_RECT: { QPolygonF polygon; polygon << QPointF(jsonObject.value("param_value_ptx0").toDouble(), jsonObject.value("param_value_pty0").toDouble()) << QPointF(jsonObject.value("param_value_ptx1").toDouble(), jsonObject.value("param_value_pty1").toDouble()) << QPointF(jsonObject.value("param_value_ptx2").toDouble(), jsonObject.value("param_value_pty2").toDouble()) << QPointF(jsonObject.value("param_value_ptx3").toDouble(), jsonObject.value("param_value_pty3").toDouble()); param.value = polygon; break; } case LP_TRANSFORM: { QTransform tranform; qreal m11 = jsonObject.value("param_value_m11").toDouble(); qreal m12 = jsonObject.value("param_value_m12").toDouble(); qreal m13 = jsonObject.value("param_value_m13").toDouble(); qreal m21 = jsonObject.value("param_value_m21").toDouble(); qreal m22 = jsonObject.value("param_value_m22").toDouble(); qreal m23 = jsonObject.value("param_value_m23").toDouble(); qreal m31 = jsonObject.value("param_value_m31").toDouble(); qreal m32 = jsonObject.value("param_value_m32").toDouble(); qreal m33 = jsonObject.value("param_value_m33").toDouble(); tranform.setMatrix(m11, m12, m13, m21, m22, m23, m31, m32, m33); param.value = tranform; break; } case LP_MAT: { //QString strExePath = QCoreApplication::applicationDirPath(); QString strFilePath = m_pTask->GetPath(); //strExePath + LP_DETECTOR_BUSSINESS_IN_PARAM_FILE_DIR; QString strFileName = jsonObject.value("param_file_path").toString(); strFilePath += strFileName; QFile file(strFilePath); if (file.exists()) { std::string strpath = strFilePath.toLocal8Bit().toStdString(); cv::Mat resultFile = cv::imread(strpath, CV_LOAD_IMAGE_UNCHANGED); param.value.setValue(resultFile); if (m_pTask) m_pTask->AppendOtherFile(QString(strpath.c_str())); } break; } case LP_ROI: { LP_DETECTOR_ROI_DATA roiData; roiData.img = m_pTask->GetTaskInfo()->templateImg; QJsonArray recordArray = jsonObject.value("param_value").toArray(); int nRecordCount = recordArray.size(); if (nRecordCount > 0) { Record_List recordList; for (int i = 0; i < nRecordCount; i++) { QJsonArray itemArray = recordArray.at(i).toArray(); int nItemCount = itemArray.size(); if (nItemCount > 0) { Item_List itemList; for (int j = 0; j < nItemCount; j++) { QJsonObject itemJson = itemArray.at(j).toObject(); Feature_List item; item.first = itemJson.value("roi_item_type").toInt(); switch (item.first) { case RECTANGLE: case ELLIPSE: { item.second.push_back(itemJson.value("roi_item_value1").toDouble()); item.second.push_back(itemJson.value("roi_item_value2").toDouble()); item.second.push_back(itemJson.value("roi_item_value3").toDouble()); item.second.push_back(itemJson.value("roi_item_value4").toDouble()); break; } case CIRCLE: { item.second.push_back(itemJson.value("roi_item_value1").toDouble()); item.second.push_back(itemJson.value("roi_item_value2").toDouble()); item.second.push_back(itemJson.value("roi_item_value3").toDouble()); break; } case POLY: { break; } default: break; } itemList.push_back(item); } recordList.push_back(itemList); } roiData.records = recordList; } } param.value.setValue(roiData); break; } case LP_LINEF: case LP_RECTF: case LP_POLYGONF: { QPolygonF polygon; QStringList ptList = jsonObject.keys(); int k = 0; for (int i = 0; i < ptList.size(); i++) { if (ptList.at(i).contains("param_value_pt")) { QString strPtKey = QString("%1%2").arg("param_value_pt").arg(QString::number(k)); QString strPtValue = jsonObject.value(strPtKey).toString(); QStringList strList = strPtValue.split(","); if (strList.size() == 2) { polygon << QPointF(strList.at(0).toDouble(), strList.at(1).toDouble()); } k++; } } param.value = polygon; } case LP_USER: { if (m_pAlgo) { m_pAlgo->LoadUserParamValue(m_pTask, this, param.strName, param.value); } else { if (m_pAlgoLibMgr) { IAlgorithmLib* pAlgoLib = m_pAlgoLibMgr->GetAlgorithmLib(m_tAlgorithmInfo.strLibName); if (pAlgoLib) { pAlgoLib->LoadUserParamValue(m_pTask, this, param.strName, param.value); } } } break; } default: break; } PLP_ALGORITHM_PARAM pParam = (PLP_ALGORITHM_PARAM)AddParam(¶m,false); if (pParam) { SerializeParamPropertyFromJson(&jsonObject, param.vProperty); } } QJsonArray outParamArray = pJsonObject->value("outParamArray").toArray(); for (int i = 0; i < outParamArray.size(); i++) { LP_ALGORITHM_PARAM param; QJsonObject jsonObject = outParamArray[i].toObject(); param.nID = jsonObject.value("out_param_id").toInt(); param.nTaskID = jsonObject.value("out_param_dest_task_id").toInt(LP_DETECTOR_INVALID_ID); param.nSrcAlgoID = jsonObject.value("out_param_dest_algo_id").toInt(LP_DETECTOR_INVALID_ID); param.nSrcParamID = jsonObject.value("out_param_dest_param_id").toInt(LP_DETECTOR_INVALID_ID); param.strName = jsonObject.value("out_param_name").toString(); param.type = (AlgoParamType)jsonObject.value("out_param_type").toInt(); switch (param.type) { case LP_INT: { param.value = jsonObject.value("out_param_value").toInt(); break; } case LP_BOOLEAN: { param.value = jsonObject.value("out_param_value").toBool(); break; } case LP_STRING: { param.value = jsonObject.value("out_param_value").toString(); break; } case LP_DOUBLE: { param.value = jsonObject.value("out_param_value").toDouble(); break; } case LP_IMAGE: { break; } case LP_MAT: { break; } case LP_ROI: { LP_DETECTOR_ROI_DATA roiData; QJsonArray recordArray = jsonObject.value("out_param_value").toArray(); int nRecordCount = recordArray.size(); if (nRecordCount > 0) { Record_List recordList; for (int i = 0; i < nRecordCount; i++) { QJsonArray itemArray = recordArray.at(i).toArray(); int nItemCount = itemArray.size(); if (nItemCount > 0) { Item_List itemList; for (int j = 0; j < nItemCount; j++) { QJsonObject itemJson = itemArray.at(j).toObject(); Feature_List item; item.first = itemJson.value("roi_item_type").toInt(); switch (item.first) { case RECTANGLE: case ELLIPSE: { item.second.push_back(itemJson.value("roi_item_value1").toDouble()); item.second.push_back(itemJson.value("roi_item_value2").toDouble()); item.second.push_back(itemJson.value("roi_item_value3").toDouble()); item.second.push_back(itemJson.value("roi_item_value4").toDouble()); break; } case CIRCLE: { item.second.push_back(itemJson.value("roi_item_value1").toDouble()); item.second.push_back(itemJson.value("roi_item_value2").toDouble()); item.second.push_back(itemJson.value("roi_item_value3").toDouble()); break; } case POLY: { break; } default: break; } itemList.push_back(item); } recordList.push_back(itemList); } roiData.records = recordList; } } param.value.setValue(roiData); break; } case LP_POLYGONF: { break; } case LP_USER: { if (m_pAlgo) { m_pAlgo->LoadUserParamValue(m_pTask, this, param.strName, param.value); } else { if (m_pAlgoLibMgr) { IAlgorithmLib* pAlgoLib = m_pAlgoLibMgr->GetAlgorithmLib(m_tAlgorithmInfo.strLibName); if (pAlgoLib) { pAlgoLib->LoadUserParamValue(m_pTask, this, param.strName, param.value); } } } break; } default: break; } param.bIsSave = jsonObject.value("out_param_save").toBool(); PLP_ALGORITHM_PARAM pParam = (PLP_ALGORITHM_PARAM)AddOutParam(¶m,false); } m_nParamBaseID = pJsonObject->value("algo_base_id").toInt(); m_nOutParamBaseID = pJsonObject->value("algo_OutParam_base_id").toInt(); PLP_ALGORITHM_PARAM relyCoordinate = GetParamByName(LP_DETECTOR_ALGO_PARAM_RELY_COORDINATE); PLP_ALGORITHM_PARAM relatvieRoi = GetParamByName(LP_DETECTOR_ALGO_PARAM_RELATIVE_ROI); if (relyCoordinate && relatvieRoi) { QPolygonF polygon = relatvieRoi->value.value(); QTransform transform = relyCoordinate->value.value(); m_worldRoi = transform.map(polygon); } return true; } bool CDetectorAlgorithm::Load(QString dllPath) { if (m_pAlgo) { delete m_pAlgo; m_pAlgo = NULL; } if (m_lib.isLoaded()) { m_lib.unload(); } QString strDll = dllPath + ".dll"; QString strFileName = dllPath.mid(dllPath.lastIndexOf("/") + 1) + ".dll"; QFile file(strDll); if (file.exists()) { m_lib.setFileName(strDll); if (!m_lib.load()) return false; } else { //QString strExePath = QCoreApplication::applicationDirPath(); QString strDefaultFilePath = m_pTask->GetPath();// strExePath + LP_DETECTOR_BUSSINESS_CONFIG_ALGO_DIR + strFileName; QFile defaultFile(strDefaultFilePath); if (defaultFile.exists()) { m_lib.setFileName(strDefaultFilePath); if (!m_lib.load()) return false; } } FnLpAlgoNewInstance pfnLpNewInstance = (FnLpAlgoNewInstance)m_lib.resolve("LpAlgoNewInstance"); if (pfnLpNewInstance) pfnLpNewInstance(&m_pAlgo); if (!m_pAlgo) return false; return true; } QString CDetectorAlgorithm::showVariant(const AlgoParamType type, const QVariant &var) { switch (type) { case LP_INT: { return QString::number(var.toInt()); break; } case LP_BOOLEAN: case LP_STRING: { return var.toString(); break; } case LP_DOUBLE: { return QString::number(var.toDouble()); break; } case LP_IMAGE: { if (!var.isNull()) { QImage img = var.value(); if (!img.isNull()) { return QString("[%1*%2]").arg(img.width()).arg(img.height()); } } break; } case LP_POINTF: { QPointF pt = var.toPointF(); QString str = "(%1,%2)"; str = str.arg(pt.x()).arg(pt.y()); return str; break; } case LP_POINT: { QPoint pt = var.toPoint(); QString str = "(%1,%2)"; str = str.arg(pt.x()).arg(pt.y()); return str; break; } case LP_MAT: { cv::Mat matResult = var.value(); return QString("[%1*%2]").arg(matResult.cols).arg(matResult.rows); break; } case LP_ROI: { LP_DETECTOR_ROI_DATA roi = var.value(); int nCount = roi.records.size(); if (nCount > 0) { for (int i = 0; i < nCount; i++) { Item_List record = roi.records.at(i); int nItemCount = record.size(); for (int j = 0; j < nItemCount; j++) { Feature_List item = record.at(j); switch (item.first) { case RECTANGLE: case ELLIPSE: { QString strResult = "(%1,%2,%3,%4)"; strResult = strResult.arg(item.second.at(0)).arg(item.second.at(1)).arg(item.second.at(2)).arg(item.second.at(3)); return strResult; break; } case CIRCLE: { QString strResult = "(%1,%2,%3)"; strResult = strResult.arg(item.second.at(0)).arg(item.second.at(1)).arg(item.second.at(2)); return strResult; break; } case POLY: { break; } default: break; } } } } break; } default: break; } return var.toString(); } QList CDetectorAlgorithm::UpdateRelyOnAlgo() { if (!m_pTask) return QList(); QList rltList; int nAlgoCount = m_pTask->EnumAlgorithm(NULL, 0); if (nAlgoCount > 0) { CDetectorAlgorithm** lppAlgo = new CDetectorAlgorithm*[nAlgoCount]; nAlgoCount = m_pTask->EnumAlgorithm((IDetectorAlgorithm**)lppAlgo, nAlgoCount); for (int i = 0; i < nAlgoCount; i++) { if (lppAlgo[i]) { int nParamCount = lppAlgo[i]->EnumParam(NULL, 0); if (nParamCount > 0) { PLP_ALGORITHM_PARAM* lppParam = new PLP_ALGORITHM_PARAM[nParamCount]; nParamCount = lppAlgo[i]->EnumParam(lppParam, nParamCount); for (int j = 0; j < nParamCount; j++) { if (lppParam[j] && lppParam[j]->nSrcAlgoID == m_nID) { PLP_ALGORITHM_PARAM param = this->GetOutParam(lppParam[j]->nSrcParamID); if (param) { lppParam[j]->value = param->value; // QString strParaName = param->strName; // QString str = showVariant(param->type, param->value); rltList.push_back(lppAlgo[i]->GetID()); if (lppParam[j]->strName == LP_DETECTOR_ALGO_PARAM_RELY_COORDINATE && lppParam[j]->type == LP_TRANSFORM) { lppAlgo[i]->InitRoiInfo(); } } } } delete[] lppParam; } } } delete[] lppAlgo; } return rltList; } QList CDetectorAlgorithm::GetRelyOnAlgo() { if (!m_pTask) return QList(); QList rltList; int nAlgoCount = m_pTask->EnumAlgorithm(NULL, 0); if (nAlgoCount > 0) { CDetectorAlgorithm** lppAlgo = new CDetectorAlgorithm*[nAlgoCount]; nAlgoCount = m_pTask->EnumAlgorithm((IDetectorAlgorithm**)lppAlgo, nAlgoCount); for (int i = 0; i < nAlgoCount; i++) { if (lppAlgo[i]) { int nParamCount = lppAlgo[i]->EnumParam(NULL, 0); if (nParamCount > 0) { PLP_ALGORITHM_PARAM* lppParam = new PLP_ALGORITHM_PARAM[nParamCount]; nParamCount = lppAlgo[i]->EnumParam(lppParam, nParamCount); for (int j = 0; j < nParamCount; j++) { if (lppParam[j] && lppParam[j]->nSrcAlgoID == m_nID) { PLP_ALGORITHM_PARAM param = this->GetOutParam(lppParam[j]->nSrcParamID); if (param) { rltList.push_back(lppAlgo[i]->GetID()); } } } delete[] lppParam; } } } delete[] lppAlgo; } return rltList; } bool CDetectorAlgorithm::Exec() { //quint64 msec = QDateTime::currentMSecsSinceEpoch(); int64 start = cv::getTickCount(); for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; if (p && !p->bIsLock && p->nSrcAlgoID != LP_DETECTOR_INVALID_ID && p->nSrcParamID != LP_DETECTOR_INVALID_ID) { //qWarning() << "handle rely algo"; IDetectorAlgorithm* pAlgo = m_pTask->GetAlgorithm(p->nSrcAlgoID); if (pAlgo) { if (pAlgo->Exec()) { PLP_ALGORITHM_PARAM pOutValue = pAlgo->GetOutParam(p->nSrcParamID); if (pOutValue && (pOutValue->type == p->type || p->type == LP_VARIANT)) { p->value = pOutValue->value; } } else return false; } } } if (m_pAlgo) { qDebug("running old version"); m_pAlgo->Exec(m_pTask, this); } else { qDebug("running new version"); if (m_pAlgoLibMgr) { IAlgorithmLib* pAlgoLib = m_pAlgoLibMgr->GetAlgorithmLib(m_tAlgorithmInfo.strLibName); if (pAlgoLib) { if (!InitRoiInfo()) { // qWarning("init algorithm roi info fail"); } pAlgoLib->CallFunc(m_tAlgorithmInfo.strFuncName, m_pTask, this); } } } m_dExecTime = (cv::getTickCount() - start) / cv::getTickFrequency() * 1000; qDebug()<::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; if (p && !p->bIsLock && p->nSrcAlgoID != LP_DETECTOR_INVALID_ID && p->nSrcParamID != LP_DETECTOR_INVALID_ID) { CDetectorAlgorithm* pAlgo = (CDetectorAlgorithm*)m_pTask->GetAlgorithm(p->nSrcAlgoID); if (pAlgo && pAlgo->IsEnabled()) { if (pAlgo->ExecOnce()) { PLP_ALGORITHM_PARAM pOutValue = pAlgo->GetOutParam(p->nSrcParamID); if (pOutValue && (pOutValue->type == p->type || p->type == LP_VARIANT)) { p->value = pOutValue->value; } } else return false; } } } if (m_pAlgo) { m_pAlgo->Exec(m_pTask, this); m_bIsFinished = true; } else { if (m_pAlgoLibMgr) { IAlgorithmLib* pAlgoLib = m_pAlgoLibMgr->GetAlgorithmLib(m_tAlgorithmInfo.strLibName); if (pAlgoLib) { if (!InitRoiInfo()) { // qWarning("init algorithm roi info fail"); } pAlgoLib->CallFunc(m_tAlgorithmInfo.strFuncName, m_pTask, this); m_bIsFinished = true; } } } //QueryPerformanceCounter(&litmp); //endTime = litmp.QuadPart; //dfMinus = (double)(endTime - startTime); //dfTime = dfMinus / dfFreq; m_dExecTime = (cv::getTickCount() - start) / cv::getTickFrequency() * 1000; qDebug() << QString("algo name:%1, algo exec time: %2").arg(m_tAlgorithmInfo.strName).arg(m_dExecTime); return true; } void CDetectorAlgorithm::SetRoiInfo(const QPolygonF& roi) { //QWriteLocker locker(&m_mutex); //m_Roi = roi; m_worldRoi = roi; } bool CDetectorAlgorithm::InitRoiInfo() { PLP_ALGORITHM_PARAM relyCoordinateParam = GetParamByName(LP_DETECTOR_ALGO_PARAM_RELY_COORDINATE); PLP_ALGORITHM_PARAM relativeRoiParam = GetParamByName(LP_DETECTOR_ALGO_PARAM_RELATIVE_ROI); PLP_ALGORITHM_PARAM RoiParam = GetParamByName("roi"); if (!relyCoordinateParam || !relativeRoiParam || !RoiParam) return false; //获取输入参数 QPolygonF relativeRoi = relativeRoiParam->value.value(); QTransform relyCoordinate = relyCoordinateParam->value.value(); QPolygonF roi = RoiParam->value.value(); //判断roi是否存在,如果是,则更新对应的relative_roi if (/*m_Roi.size()*/ roi.size() > 0) { relativeRoi = relyCoordinate.inverted().map(roi); UpdateParamValueByName("roi", (QVariant)QPolygonF()); UpdateParamValueByName(LP_DETECTOR_ALGO_PARAM_RELATIVE_ROI, (QVariant)relativeRoi); //m_Roi.clear(); } //计算世界坐标系下的roi //qWarning("relativeRoi:(%d,%d),(%d,%d),(%d,%d),(%d.%d)", relativeRoi.at(0).x(), relativeRoi.at(0).y(), relativeRoi.at(1).x(), relativeRoi.at(1).y(), relativeRoi.at(2).x(), relativeRoi.at(2).y(), relativeRoi.at(3).x(), relativeRoi.at(3).y()); //qWarning("relyCoordinate:%d,%d,%d,%d", relyCoordinate.m11(), relyCoordinate.m12(), relyCoordinate.m21(), relyCoordinate.m22()); m_worldRoi = relyCoordinate.map(relativeRoi); return true; } QVariant CDetectorAlgorithm::GetParamValue(QString strKey, AlgoParamType type) { if (m_mapParams.value(strKey)) { return m_mapParams.value(strKey)->value; } return NULL; } QVariant CDetectorAlgorithm::GetOutParamValue(QString strKey, AlgoParamType type) { if (m_mapOutParams.value(strKey)) { return m_mapOutParams.value(strKey)->value; } return NULL; } bool CDetectorAlgorithm::SetOutParamValue(QString strKey, QVariant value) { PLP_ALGORITHM_PARAM pParam = m_mapOutParams.value(strKey); if (pParam) { if (pParam->type == LP_IMAGE) { QImage img = value.value(); pParam->value = img; } else { pParam->value = value; } return true; } else { PLP_ALGORITHM_PARAM p = new LP_ALGORITHM_PARAM; p->nID = ++m_nOutParamBaseID; p->strName = strKey; p->type = LP_VARIANT; p->value = value; m_vecOutParams[p->strName] = p; m_mapOutParams.insert(p->strName, p); } return false; } bool CDetectorAlgorithm::Copy(IDetectorAlgorithm* pSrcAlgo, bool bRet /* = true */) { if (!pSrcAlgo) return false; if (bRet) m_nID = pSrcAlgo->GetID(); if (!SetAlgorithmInfo(pSrcAlgo->GetAlgorithmInfo())) return false; m_bIsExec = pSrcAlgo->IsEnabled(); int nParamCount = pSrcAlgo->EnumParam(NULL, 0); if (nParamCount > 0) { PLP_ALGORITHM_PARAM* lppParam = new PLP_ALGORITHM_PARAM[nParamCount]; nParamCount = pSrcAlgo->EnumParam(lppParam, nParamCount); for (int i = 0; i < nParamCount; i++) { if (lppParam[i]) { LP_ALGORITHM_PARAM* p = new LP_ALGORITHM_PARAM; p->nID = lppParam[i]->nID; p->nTaskID = lppParam[i]->nTaskID; p->nSrcAlgoID = lppParam[i]->nSrcAlgoID; p->nSrcParamID = lppParam[i]->nSrcParamID; p->strName = lppParam[i]->strName; p->type = lppParam[i]->type; p->value = lppParam[i]->value; p->strDescription = lppParam[i]->strDescription; p->bIsSave = lppParam[i]->bIsSave; p->bIsLock = lppParam[i]->bIsLock; m_vecParams[p->strName] = p; m_mapParams.insert(p->strName, p); qWarning() << "copy lock state :" << p->bIsLock; } } delete[] lppParam; } int nParamOutCount = pSrcAlgo->EnumOutParam(NULL, 0); if (nParamOutCount > 0) { PLP_ALGORITHM_PARAM* lppOutParam = new PLP_ALGORITHM_PARAM[nParamOutCount]; nParamOutCount = pSrcAlgo->EnumOutParam(lppOutParam, nParamOutCount); for (int i = 0; i < nParamOutCount; i++) { if (lppOutParam[i]) { LP_ALGORITHM_PARAM* p = new LP_ALGORITHM_PARAM; p->nID = lppOutParam[i]->nID; p->nTaskID = lppOutParam[i]->nTaskID; p->nSrcAlgoID = lppOutParam[i]->nSrcAlgoID; p->nSrcParamID = lppOutParam[i]->nSrcParamID; p->strName = lppOutParam[i]->strName; p->type = lppOutParam[i]->type; p->value = lppOutParam[i]->value; p->strDescription = lppOutParam[i]->strDescription; p->bIsSave = lppOutParam[i]->bIsSave; p->bIsLock = lppOutParam[i]->bIsLock; m_vecOutParams[p->strName] = p; m_mapOutParams.insert(p->strName, p); } } delete[] lppOutParam; } m_nParamBaseID = pSrcAlgo->GetParamBaseID(); m_nOutParamBaseID = pSrcAlgo->GetOutParamBaseID(); if (m_tAlgorithmInfo.strLibName.size() > 0 && m_tAlgorithmInfo.strFuncName.size() > 0) return true; if (m_tAlgorithmInfo.strPath.size() > 0) { Load(m_tAlgorithmInfo.strPath.left(m_tAlgorithmInfo.strPath.lastIndexOf("."))); } else { Load(m_tAlgorithmInfo.strName); } return true; } bool CDetectorAlgorithm::InitAlgo() { if (!m_pTask) return false; m_nOutParamBaseID = 0; m_nParamBaseID = 0; for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; if (NULL != p) { delete p; } } m_vecParams.clear(); m_mapParams.clear(); for (QMap::iterator iter = m_vecOutParams.begin(); iter != m_vecOutParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; if (NULL != p) { delete p; } } m_vecOutParams.clear(); m_mapOutParams.clear(); if (m_pAlgo) { return m_pAlgo->Init(m_pTask, this); } else { if (m_pAlgoLibMgr) { IAlgorithmLib* pAlgoLib = m_pAlgoLibMgr->GetAlgorithmLib(m_tAlgorithmInfo.strLibName); if (pAlgoLib) { return pAlgoLib->InitAlgo(m_pTask, this); } } } return true; } bool CDetectorAlgorithm::ImportParam(QJsonArray paramArray)//导入参数 { if (!paramArray.isEmpty() && paramArray.size() > 0) { m_nParamBaseID = 0; for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; if (NULL != p) { delete p; } } m_vecParams.clear(); } for (int i = 0; i < paramArray.size(); i++) { LP_ALGORITHM_PARAM param; QJsonObject jsonObject = paramArray[i].toObject(); // param.nID = jsonObject.value("param_id").toInt(); // param.nSrcAlgoID = jsonObject.value("param_src_algo_id").toInt(); // param.nSrcParamID = jsonObject.value("param_src_param_id").toInt(); param.strName = jsonObject.value("param_name").toString(); param.type = (AlgoParamType)jsonObject.value("param_type").toInt(); switch (param.type) { case LP_INT: { param.value = jsonObject.value("param_value").toInt(); break; } case LP_BOOLEAN: { param.value = jsonObject.value("param_value").toBool(); break; } case LP_STRING: { param.value = jsonObject.value("param_value").toString(); break; } case LP_DOUBLE: { param.value = jsonObject.value("param_value").toDouble(); break; } case LP_IMAGE: { break; } case LP_MAT: { QString strFileName = jsonObject.value("param_value").toString(); std::string strpath = strFileName.toLocal8Bit().toStdString(); cv::Mat resultFile = cv::imread(strpath, CV_LOAD_IMAGE_UNCHANGED); param.value.setValue(resultFile); if (m_pTask) m_pTask->AppendOtherFile(QString(strpath.c_str())); break; } case LP_ROI: { LP_DETECTOR_ROI_DATA roiData; //QString strExePath = QCoreApplication::applicationDirPath(); QString strFilePath = m_pTask->GetPath(); //strExePath + LP_DETECTOR_BUSSINESS_IMAGE_DIR; QString strFileName = strFilePath + QString::number(param.nID) + ".bmp"; std::string strpath = strFileName.toLocal8Bit().toStdString(); roiData.img = cv::imread(strpath, CV_LOAD_IMAGE_UNCHANGED); QJsonArray recordArray = jsonObject.value("param_value").toArray(); int nRecordCount = recordArray.size(); if (nRecordCount > 0) { Record_List recordList; for (int i = 0; i < nRecordCount; i++) { QJsonArray itemArray = recordArray.at(i).toArray(); int nItemCount = itemArray.size(); if (nItemCount > 0) { Item_List itemList; for (int j = 0; j < nItemCount; j++) { QJsonObject itemJson = itemArray.at(j).toObject(); Feature_List item; item.first = itemJson.value("roi_item_type").toInt(); switch (item.first) { case RECTANGLE: case ELLIPSE: { item.second.push_back(itemJson.value("roi_item_value1").toDouble()); item.second.push_back(itemJson.value("roi_item_value2").toDouble()); item.second.push_back(itemJson.value("roi_item_value3").toDouble()); item.second.push_back(itemJson.value("roi_item_value4").toDouble()); break; } case CIRCLE: { item.second.push_back(itemJson.value("roi_item_value1").toDouble()); item.second.push_back(itemJson.value("roi_item_value2").toDouble()); item.second.push_back(itemJson.value("roi_item_value3").toDouble()); break; } case POLY: { break; } default: break; } itemList.push_back(item); } recordList.push_back(itemList); } roiData.records = recordList; } } param.value.setValue(roiData); break; } default: break; } param.strDescription = jsonObject.value("param_description").toString(); PLP_ALGORITHM_PARAM pParam = (PLP_ALGORITHM_PARAM)AddParam(¶m,false); } return true; } QByteArray CDetectorAlgorithm::ExportParam() { QJsonDocument document; QJsonArray paramArray; for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { if (*iter) { PLP_ALGORITHM_PARAM p = (PLP_ALGORITHM_PARAM)*iter; QJsonObject jsonParam; // jsonParam.insert("param_id", p->nID); // jsonParam.insert("param_src_algo_id", p->nSrcAlgoID); // jsonParam.insert("param_src_param_id", p->nSrcParamID); jsonParam.insert("param_name", p->strName); jsonParam.insert("param_type", p->type); switch (p->type) { case LP_INT: { jsonParam.insert("param_value", p->value.toInt()); break; } case LP_BOOLEAN: { jsonParam.insert("param_value", p->value.toBool()); break; } case LP_STRING: { jsonParam.insert("param_value", p->value.toString()); break; } case LP_DOUBLE: { jsonParam.insert("param_value", p->value.toDouble()); break; } case LP_IMAGE: { break; } case LP_MAT: { cv::Mat matResult = p->value.value(); QString strExePath = m_pTask->GetPath(); //QCoreApplication::applicationDirPath(); QString strFilePath = strExePath + "/" + "paramfiles/"; QString strFileName = strFilePath + QString::number(p->nID) + ".bmp"; std::string strpath = strFileName.toLocal8Bit().toStdString(); if (!matResult.empty() && !cv::imwrite(strpath, matResult)) return false; jsonParam.insert("param_value", strFileName); break; } case LP_ROI: { LP_DETECTOR_ROI_DATA roiData = p->value.value(); QString strExePath = m_pTask->GetPath(); //QCoreApplication::applicationDirPath(); QString strFilePath = m_pTask->GetPath()+"/"; //strExePath + "/" + "images/"; QString strFileName = strFilePath + QString::number(p->nID) + ".bmp"; std::string strpath = strFileName.toLocal8Bit().toStdString(); if (!roiData.img.empty() && !cv::imwrite(strpath, roiData.img)) return false; int nCount = roiData.records.size(); if (nCount > 0) { QJsonArray recordArray; for (int i = 0; i < nCount; i++) { QJsonArray itemArray; Item_List record = roiData.records.at(i); int nItemCount = record.size(); for (int j = 0; j < nItemCount; j++) { QJsonObject itemJson; Feature_List item = record.at(j); itemJson.insert("roi_item_type", item.first); switch (item.first) { case RECTANGLE: case ELLIPSE: { itemJson.insert("roi_item_value1", item.second.at(0)); itemJson.insert("roi_item_value2", item.second.at(1)); itemJson.insert("roi_item_value3", item.second.at(2)); itemJson.insert("roi_item_value4", item.second.at(3)); break; } case CIRCLE: { itemJson.insert("roi_item_value1", item.second.at(0)); itemJson.insert("roi_item_value2", item.second.at(1)); itemJson.insert("roi_item_value3", item.second.at(2)); break; } case POLY: { break; } default: break; } itemArray.push_back(itemJson); } recordArray.push_back(itemArray); } jsonParam.insert("param_value", recordArray); } break; } default: break; } jsonParam.insert("param_description", p->strDescription); paramArray.push_back(jsonParam); } } if (paramArray.size() > 0) document.setArray(paramArray); return document.toJson(); } bool CDetectorAlgorithm::ImportOutParam(QJsonArray outParamArray)//导入参数 { if (!outParamArray.isEmpty() && outParamArray.size() > 0) { for (QMap::iterator iter = m_vecOutParams.begin(); iter != m_vecOutParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; if (NULL != p) { delete p; } } m_vecOutParams.clear(); m_mapOutParams.clear(); } for (int i = 0; i < outParamArray.size(); i++) { LP_ALGORITHM_PARAM param; QJsonObject jsonObject = outParamArray[i].toObject(); // param.nID = jsonObject.value("out_param_id").toInt(); param.strName = jsonObject.value("param_name").toString(); param.type = (AlgoParamType)jsonObject.value("param_type").toInt(); switch (param.type) { case LP_INT: { param.value = jsonObject.value("param_value").toInt(); break; } case LP_BOOLEAN: { param.value = jsonObject.value("param_value").toBool(); break; } case LP_STRING: { param.value = jsonObject.value("param_value").toString(); break; } case LP_DOUBLE: { param.value = jsonObject.value("param_value").toDouble(); break; } case LP_IMAGE: { break; } case LP_MAT: { QString strFileName = jsonObject.value("param_value").toString(); std::string strpath = strFileName.toLocal8Bit().toStdString(); cv::Mat resultFile = cv::imread(strpath); param.value.setValue(resultFile); if (m_pTask) m_pTask->AppendOtherFile(QString(strpath.c_str())); break; } case LP_ROI: { LP_DETECTOR_ROI_DATA roiData; //QString strExePath = QCoreApplication::applicationDirPath(); QString strFilePath = m_pTask->GetPath()+"/"; //strExePath + LP_DETECTOR_BUSSINESS_IMAGE_DIR; QString strFileName = strFilePath + QString::number(param.nID) + ".bmp"; std::string strpath = strFileName.toLocal8Bit().toStdString(); roiData.img = cv::imread(strpath, CV_LOAD_IMAGE_UNCHANGED); QJsonArray recordArray = jsonObject.value("param_value").toArray(); int nRecordCount = recordArray.size(); if (nRecordCount > 0) { Record_List recordList; for (int i = 0; i < nRecordCount; i++) { QJsonArray itemArray = recordArray.at(i).toArray(); int nItemCount = itemArray.size(); if (nItemCount > 0) { Item_List itemList; for (int j = 0; j < nItemCount; j++) { QJsonObject itemJson = itemArray.at(j).toObject(); Feature_List item; item.first = itemJson.value("roi_item_type").toInt(); switch (item.first) { case RECTANGLE: case ELLIPSE: { item.second.push_back(itemJson.value("roi_item_value1").toDouble()); item.second.push_back(itemJson.value("roi_item_value2").toDouble()); item.second.push_back(itemJson.value("roi_item_value3").toDouble()); item.second.push_back(itemJson.value("roi_item_value4").toDouble()); break; } case CIRCLE: { item.second.push_back(itemJson.value("roi_item_value1").toDouble()); item.second.push_back(itemJson.value("roi_item_value2").toDouble()); item.second.push_back(itemJson.value("roi_item_value3").toDouble()); break; } case POLY: { break; } default: break; } itemList.push_back(item); } recordList.push_back(itemList); } roiData.records = recordList; } } param.value.setValue(roiData); break; } default: break; } //param.strDescription = jsonObject.value("out_param_description").toString(); PLP_ALGORITHM_PARAM pParam = (PLP_ALGORITHM_PARAM)AddOutParam(¶m,false); } return true; } QByteArray CDetectorAlgorithm::ExportOutParam()//导出参数 { QJsonDocument document; QJsonArray outParamArray; for (QMap::iterator iter = m_vecOutParams.begin(); iter != m_vecOutParams.end(); ++iter) { if (*iter) { PLP_ALGORITHM_PARAM p = (PLP_ALGORITHM_PARAM)*iter; QJsonObject jsonParam; // jsonParam.insert("out_param_id", p->nID); jsonParam.insert("param_name", p->strName); jsonParam.insert("param_type", p->type); switch (p->type) { case LP_INT: { jsonParam.insert("param_value", p->value.toInt()); break; } case LP_BOOLEAN: { jsonParam.insert("param_value", p->value.toBool()); break; } case LP_STRING: { jsonParam.insert("param_value", p->value.toString()); break; } case LP_DOUBLE: { jsonParam.insert("param_value", p->value.toDouble()); break; } case LP_IMAGE: { break; } case LP_MAT: { cv::Mat matResult = p->value.value(); //QString strExePath = QCoreApplication::applicationDirPath(); QString strFilePath = m_pTask->GetPath()+"/"; //strExePath + "/" + "paramfiles/"; QString strFileName = strFilePath + QString::number(p->nID) + ".bmp"; std::string strpath = strFileName.toLocal8Bit().toStdString(); if (!matResult.empty() && !cv::imwrite(strpath, matResult)) return false; jsonParam.insert("param_value", strFileName); break; } case LP_ROI: { LP_DETECTOR_ROI_DATA roiData = p->value.value(); QString strExePath = QCoreApplication::applicationDirPath(); QString strFilePath = strExePath + "/" + "images/"; QString strFileName = strFilePath + QString::number(p->nID) + ".bmp"; std::string strpath = strFileName.toLocal8Bit().toStdString(); if (!roiData.img.empty() && !cv::imwrite(strpath, roiData.img)) return false; int nCount = roiData.records.size(); if (nCount > 0) { QJsonArray recordArray; for (int i = 0; i < nCount; i++) { QJsonArray itemArray; Item_List record = roiData.records.at(i); int nItemCount = record.size(); for (int j = 0; j < nItemCount; j++) { QJsonObject itemJson; Feature_List item = record.at(j); itemJson.insert("roi_item_type", item.first); switch (item.first) { case RECTANGLE: case ELLIPSE: { itemJson.insert("roi_item_value1", item.second.at(0)); itemJson.insert("roi_item_value2", item.second.at(1)); itemJson.insert("roi_item_value3", item.second.at(2)); itemJson.insert("roi_item_value4", item.second.at(3)); break; } case CIRCLE: { itemJson.insert("roi_item_value1", item.second.at(0)); itemJson.insert("roi_item_value2", item.second.at(1)); itemJson.insert("roi_item_value3", item.second.at(2)); break; } case POLY: { break; } default: break; } itemArray.push_back(itemJson); } recordArray.push_back(itemArray); } jsonParam.insert("param_value", recordArray); } break; } default: break; } jsonParam.insert("param_description", p->strDescription); outParamArray.push_back(jsonParam); } } if (outParamArray.size() > 0) document.setArray(outParamArray); return document.toJson(); } bool CDetectorAlgorithm::SwapParamOrder(int nID1, int nID2) { PLP_ALGORITHM_PARAM pParam1 = NULL, pParam2 = NULL; LP_ALGORITHM_PARAM tempParam; for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { if (NULL != *iter) { PLP_ALGORITHM_PARAM p = *iter; if (p->nID == nID1) { pParam1 = p; } if (p->nID == nID2) { pParam2 = p; } if (pParam1 && pParam2) { tempParam = *pParam1; *pParam1 = *pParam2; *pParam2 = tempParam; return true; } } } return false; } bool CDetectorAlgorithm::InsertParamOrder(int nID, int nIndex) { if (nIndex < 0 || nIndex > m_vecParams.count() - 1) return false; PLP_ALGORITHM_PARAM pParam = NULL; for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { PLP_ALGORITHM_PARAM p = *iter; if (NULL != p && p->nID == nID) { pParam = p; iter = m_vecParams.erase(iter); PLP_ALGORITHM_PARAM tempP = m_vecParams.take(iter.key()); delete tempP; break; } } if (pParam) m_vecParams.insert(pParam->strName, pParam); return true; } bool CDetectorAlgorithm::UpdateParamValue(int nParamID, QVariant& value) { PLP_ALGORITHM_PARAM pParam = GetParam(nParamID); if (!pParam) return false; pParam->value = value; return true; } PLP_ALGORITHM_PARAM CDetectorAlgorithm::GetParamByName(QString strName) { QReadLocker locker(&m_mutex); return m_mapParams.value(strName); } PLP_ALGORITHM_PARAM CDetectorAlgorithm::GetOutParamByName(QString strName) { QReadLocker locker(&m_mutex); return m_mapOutParams.value(strName); } bool CDetectorAlgorithm::UpdateParamValueByName(QString strName, QVariant& value) { QWriteLocker locker(&m_mutex); bool bRet = false; PLP_ALGORITHM_PARAM p = m_mapParams.value(strName); if (p) { p->value = value; bRet = true; } return bRet; } void CDetectorAlgorithm::LoadUserParamValue(int nParamID /*= LP_DETECTOR_INVALID_ID*/) { if (!m_pAlgo) return; if (nParamID == LP_DETECTOR_INVALID_ID) { for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { if (NULL != *iter) { PLP_ALGORITHM_PARAM p = *iter; if (p->type == LP_USER) m_pAlgo->LoadUserParamValue(m_pTask, this, p->strName, p->value); } } } else { PLP_ALGORITHM_PARAM p = GetParam(nParamID); if (p) { if (p->type == LP_USER) m_pAlgo->LoadUserParamValue(m_pTask, this, p->strName, p->value); } } } void CDetectorAlgorithm::SaveUserParamValue(int nParamID /*= LP_DETECTOR_INVALID_ID*/) { if (!m_pAlgo) return; if (nParamID == LP_DETECTOR_INVALID_ID) { for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { if (NULL != *iter) { PLP_ALGORITHM_PARAM p = *iter; if (p->type == LP_USER) m_pAlgo->SaveUserParamValue(m_pTask, this, p->strName); } } } else { PLP_ALGORITHM_PARAM p = GetParam(nParamID); if (p) { if (p->type == LP_USER) m_pAlgo->SaveUserParamValue(m_pTask, this, p->strName); } } } void CDetectorAlgorithm::ClearUserParamValue(int nParamID /*= LP_DETECTOR_INVALID_ID*/) { if (!m_pAlgo) return; if (nParamID == LP_DETECTOR_INVALID_ID) { for (QMap::iterator iter = m_vecParams.begin(); iter != m_vecParams.end(); ++iter) { if (NULL != *iter) { PLP_ALGORITHM_PARAM p = *iter; if (p->type == LP_USER) p->value.clear(); } } } else { PLP_ALGORITHM_PARAM p = GetParam(nParamID); if (p) { if (p->type == LP_USER) p->value.clear(); } } } bool CDetectorAlgorithm::HandleEvent(QString strParamName /*= ""*/, int event/* = -1*/) { IAlgorithmLib* pAlgoLib = m_pAlgoLibMgr->GetAlgorithmLib(m_tAlgorithmInfo.strLibName); if (pAlgoLib) { return pAlgoLib->HandleEvent(m_pTask, this, strParamName, event); } return false; }