#include "Task.h" #include "Roi.h" #include "Algorithm.h" CDetectorTask::CDetectorTask() { m_nID = LP_DETECTOR_INVALID_ID; m_nBaseID = 0; m_nAlgorithmBaseID = 0; m_nRunAlgoID = LP_DETECTOR_INVALID_ID; m_pSolution = NULL; m_pAlgoShare = NULL; m_vecROI.clear(); m_vecAlgorithm.clear(); m_bRunning = false; m_dExecTime = 0.0; } CDetectorTask::~CDetectorTask() { CleanUp(); } bool CDetectorTask::Initialize(IDetectorSolution* pSolution, IDetectorSystemInfo* pSystemInfo, IAlgorithmShare* pAlgoShare, IAlgorithmLibMgr* pAlgoLibMgr) { if (!pSolution || !pAlgoShare) return false; m_pSolution = pSolution; m_pSystemInfo = pSystemInfo; m_pAlgoShare = pAlgoShare; m_pAlgoLibMgr = pAlgoLibMgr; return true; } bool CDetectorTask::SetTaskInfo(const PLP_DETECTOR_TASK pTask) { if (NULL == pTask) return false; m_tTask.nID= pTask->nID; m_tTask.strName = pTask->strName; m_tTask.strDescription = pTask->strDescription; m_tTask.nCameraID = pTask->nCameraID; m_tTask.strDirPath = pTask->strDirPath; m_tTask.nGroupParamID = pTask->nGroupParamID; m_tTask.property = pTask->property; m_tTask.bIsRun = pTask->bIsRun; return true; } PLP_DETECTOR_TASK CDetectorTask::GetTaskInfo() { return &m_tTask; } IDetectorROI* CDetectorTask::AddROI(const PLP_DETECTOR_ROI pROI, bool bRet /* = true */) { if (NULL == pROI) return NULL; CDetectorROI* p = new CDetectorROI(); if (p->Initialize(this) && p->SetROIInfo(pROI)) { p->SetID(++m_nBaseID); m_vecROI.push_back(p); return p; } delete p; return NULL; } bool CDetectorTask::DeleteROI(int nROIID) { for (QVector::iterator iter = m_vecROI.begin(); iter != m_vecROI.end(); ++iter) { IDetectorROI *p = *iter; if (NULL != p) { if (p->GetID() == nROIID) { delete *iter; iter = m_vecROI.erase(iter); return true; } } } return false; } IDetectorROI* CDetectorTask::GetROI(int nROIID) { for (QVector::iterator iter = m_vecROI.begin(); iter != m_vecROI.end(); ++iter) { if (NULL != *iter) { IDetectorROI *p = *iter; if (p->GetID() == nROIID) return p; } } return NULL; } int CDetectorTask::EnumROI(IDetectorROI** lppROI, int nCount) { if (!lppROI || nCount < m_vecROI.size()) return m_vecROI.size(); int nIndex = 0; for (QVector::iterator iter = m_vecROI.begin(); iter != m_vecROI.end(); ++iter) { if (NULL != *iter) { lppROI[nIndex] = *iter; nIndex++; } } return nIndex; } IDetectorAlgorithm* CDetectorTask::AddAlgorithm(const PLP_DETECTOR_ALGORITHM pAlgorithm, bool bRet /* = true */) { if (NULL == pAlgorithm) return NULL; CDetectorAlgorithm* p = new CDetectorAlgorithm(); if (p->Initialize(this, m_pAlgoShare, m_pAlgoLibMgr) && p->SetAlgorithmInfo(pAlgorithm)) { ++m_nAlgorithmBaseID; p->SetID(pAlgorithm->nID); m_vecAlgorithm.push_back(p); return p; } return NULL; } bool CDetectorTask::DeleteAlgorithm(int nAlgorithmID) { if (m_nRunAlgoID == nAlgorithmID) m_nRunAlgoID = LP_DETECTOR_INVALID_ID; bool bRet = false; for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { IDetectorAlgorithm *p = *iter; if (NULL != p) { if (p->GetID() == nAlgorithmID) { delete *iter; iter = m_vecAlgorithm.erase(iter); bRet = true; break; } } } if (bRet) { for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { //重置所有依赖算法的ID IDetectorAlgorithm *p = *iter; if (p) { int nParamCount = p->EnumParam(NULL, 0); if (nParamCount > 0) { PLP_ALGORITHM_PARAM* lppParam = new PLP_ALGORITHM_PARAM[nParamCount]; nParamCount = p->EnumParam(lppParam, nParamCount); for (int i = 0; i < nParamCount; i++) { if (lppParam[i] && lppParam[i]->nSrcAlgoID == nAlgorithmID) { lppParam[i]->nSrcAlgoID = LP_DETECTOR_INVALID_ID; lppParam[i]->nSrcParamID = LP_DETECTOR_INVALID_ID; } } delete[] lppParam; } } } } return bRet; } IDetectorAlgorithm* CDetectorTask::GetAlgorithm(int nAlgorithmID) { for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { if (NULL != *iter) { IDetectorAlgorithm *p = *iter; if (p->GetID() == nAlgorithmID) return p; } } return NULL; } int CDetectorTask::EnumAlgorithm(IDetectorAlgorithm** lppAlgorithm, int nCount) { if (!lppAlgorithm || nCount < m_vecAlgorithm.size()) return m_vecAlgorithm.size(); int nIndex = 0; for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { if (NULL != *iter) { lppAlgorithm[nIndex] = *iter; nIndex++; } } return nIndex; } void CDetectorTask::SetID(int nID) { m_nID = nID; } int CDetectorTask::GetID() const { return m_nID; } int CDetectorTask::GetAlgoBaseID() const { return m_nAlgorithmBaseID; } void CDetectorTask::Release() { delete this; } bool CDetectorTask::SerializePropertyToJson(QJsonObject* pJsonObject) { if (!pJsonObject) return false; if (m_tTask.property.type() == QMetaType::QVariantMap) { QJsonArray PropertyArray; QMap mapVariant = m_tTask.property.toMap(); for (QMap::iterator iter = mapVariant.begin(); iter != mapVariant.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 CDetectorTask::SerializeToJson(QJsonObject& jsonObject) { jsonObject.insert("task_name", m_tTask.strName); jsonObject.insert("task_cameraID", m_tTask.nCameraID); jsonObject.insert("task_description", m_tTask.strDescription); jsonObject.insert("task_default_dir", m_tTask.strDirPath); jsonObject.insert("task_group_param_id", m_tTask.nGroupParamID); jsonObject.insert("task_run", m_tTask.bIsRun); jsonObject.insert("task_id", m_nID); jsonObject.insert("task_base_id", m_nBaseID); jsonObject.insert("task_algo_base_id", m_nAlgorithmBaseID); jsonObject.insert("task_algo_run_id", m_nRunAlgoID); if (!SerializePropertyToJson(&jsonObject)) return false; QJsonArray algoArray; for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { if (*iter) { CDetectorAlgorithm *p = (CDetectorAlgorithm*)*iter; if (!p->SerializeToJson(NULL, &algoArray)) return false; } } QJsonArray adapterArray; if (algoArray.size() > 0) jsonObject.insert("algoArray", algoArray); if (adapterArray.size() > 0) jsonObject.insert("adapterArray", adapterArray); return true; } bool CDetectorTask::SerializeFromJson(QJsonObject* pJsonObject) { if (!pJsonObject) return false; m_tTask.strName = pJsonObject->value("task_name").toString(); m_tTask.nCameraID = pJsonObject->value("task_cameraID").toInt(); m_tTask.strDescription = pJsonObject->value("task_description").toString(); m_tTask.strDirPath = pJsonObject->value("task_default_dir").toString(); m_tTask.nGroupParamID = pJsonObject->value("task_group_param_id").toInt(LP_DETECTOR_INVALID_ID); m_tTask.bIsRun = pJsonObject->value("task_run").toBool(true); m_nID = pJsonObject->value("task_id").toInt(); m_nRunAlgoID = pJsonObject->value("task_algo_run_id").toInt(); { //加载默认图片 QString strExePath = QCoreApplication::applicationDirPath(); QString strImagePath = strExePath + LP_DETECTOR_BUSSINESS_IMAGE_DIR; QString strImageName = strImagePath + QString::number(m_pSolution->GetID()) + "_" + QString::number(m_nID) + ".bmp"; std::string strpath = strImageName.toLocal8Bit().toStdString(); m_tTask.templateImg = cv::imread(strpath/*strImageName.toLatin1().data()*/, CV_LOAD_IMAGE_UNCHANGED); _strPath = strpath; strtempImgPath = strImageName; } if (!SerializePropertyFromJson(pJsonObject)) return false; QJsonArray algoArray = pJsonObject->value("algoArray").toArray(); for (int i = 0; i < algoArray.size(); i++) { LP_DETECTOR_ALGORITHM algoInfo; QJsonObject jsonObject = algoArray[i].toObject(); algoInfo.strName = jsonObject.value("algo_name").toString(); algoInfo.strDescription = jsonObject.value("algo_description").toString(); algoInfo.strPath = jsonObject.value("algo_path").toString(); algoInfo.nRoiID = jsonObject.value("algo_roi_id").toInt(); algoInfo.strLibName = jsonObject.value("algo_lib_name").toString(); algoInfo.strFuncName = jsonObject.value("algo_func_name").toString(); algoInfo.nID = jsonObject.value("algo_id").toInt(); bool bExec = jsonObject.value("algo_exec_enabled").toBool(true); CDetectorAlgorithm* pAlgo = (CDetectorAlgorithm*)AddAlgorithm(&algoInfo); pAlgo->SetEnabled(bExec); if (!pAlgo->SerializeFromJson(&jsonObject)) return false; } m_nBaseID = pJsonObject->value("task_base_id").toInt(); m_nAlgorithmBaseID = pJsonObject->value("task_algo_base_id").toInt(); return true; } bool CDetectorTask::SaveFile(const QString& strPath) { QJsonDocument document; QJsonObject jsonObject; if (!SerializeToJson(jsonObject)) { return false; } //保存默认图片 if (m_pSolution && !m_tTask.templateImg.empty()) { QString strExePath = QCoreApplication::applicationDirPath(); QString strFilePath = strExePath + LP_DETECTOR_BUSSINESS_IMAGE_DIR; QDir dir; if (!dir.exists(strFilePath)) { if (!dir.mkdir(strFilePath)) return false; } QString strFileName = strFilePath + QString::number(m_pSolution->GetID()) + "_" + QString::number(m_nID) + ".bmp"; std::string strpath = strFileName.toLocal8Bit().toStdString(); if (!cv::imwrite(strpath/*strFileName.toLatin1().data()*/, m_tTask.templateImg)) return false; } document.setObject(jsonObject); QByteArray byteArray = document.toJson(); if (byteArray.isNull()) return false; strParamFilePath = strPath; QFile file(strPath); if (file.open(QFile::WriteOnly | QFile::Text)) { file.write(byteArray); file.flush(); file.close(); return true; } return false; } bool CDetectorTask::LoadFile(const QString& strPath) { strParamFilePath = strPath; QJsonDocument document; QJsonParseError parse_error; QFile taskFile(strPath); if (!taskFile.open(QFile::ReadOnly)) { return false; } QByteArray byteArray; byteArray = taskFile.readAll(); if (byteArray.isEmpty()) { taskFile.close(); return false; } document = QJsonDocument::fromJson(byteArray, &parse_error); if (parse_error.error == QJsonParseError::NoError) { if (!document.isEmpty() && !document.isNull()) { if (document.isObject()) { QJsonObject taskJsonObject = document.object(); if (!SerializeFromJson(&taskJsonObject)) { taskFile.close(); return false; } } } } taskFile.close(); return true; } bool CDetectorTask::RemoveFile() { QFile::remove(strtempImgPath); QFile::remove(strParamFilePath); for each (QString strVar in strPathList) { QFile::remove(strVar); } strParamFilePath.clear(); return true; } bool CDetectorTask::AppendOtherFile(QString strpath) { if (strPathList.contains(strpath)) return false; else strPathList.append(strpath); return true; } bool CDetectorTask::SerializePropertyFromJson(QJsonObject* pJsonObject) { if (!pJsonObject) return false; if (!pJsonObject->contains("property")) return true; QJsonArray propertyArray = pJsonObject->value("property").toArray(); QMap mapProperty; for (int i = 0; i < propertyArray.size(); i++) { QJsonObject propertyObject = propertyArray[i].toObject(); switch (propertyObject.value("prop_type").toInt()) { case QMetaType::Bool: { mapProperty.insert(propertyObject.value("prop_name").toString(), propertyObject.value("prop_value").toBool()); break; } case QMetaType::Int: { mapProperty.insert(propertyObject.value("prop_name").toString(), propertyObject.value("prop_value").toInt()); break; } case QMetaType::Double: { mapProperty.insert(propertyObject.value("prop_name").toString(), propertyObject.value("prop_value").toDouble()); break; } case QMetaType::QString: { mapProperty.insert(propertyObject.value("prop_name").toString(), propertyObject.value("prop_value").toString()); break; } default: break; } } m_tTask.property.setValue(mapProperty); return true; } void CDetectorTask::CleanUp() { if (!m_tTask.templateImg.empty()) { m_tTask.templateImg.release(); //cvReleaseImage(&m_tTask.pImg); //m_tTask.pImg = NULL; } for (QVector::iterator iter = m_vecROI.begin(); iter != m_vecROI.end(); ++iter) { if (*iter) { (*iter)->Release(); } } m_vecROI.clear(); for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { if (*iter) { (*iter)->Release(); } } m_vecAlgorithm.clear(); } bool CDetectorTask::Copy(IDetectorTask* pSrcTask, bool bRet /* = true */) { if (!pSrcTask) return false; QString strName = pSrcTask->GetTaskInfo()->strName; if (bRet) m_nID = pSrcTask->GetID(); if (pSrcTask->GetRunAlgo()) { m_nRunAlgoID = pSrcTask->GetRunAlgo()->GetID(); } m_tTask.templateImg.release(); m_tTask.nID = m_nID; //m_tTask.pImg = NULL; if (!SetTaskInfo(pSrcTask->GetTaskInfo())) return false; int nAlgoCount = pSrcTask->EnumAlgorithm(NULL, 0); if (nAlgoCount > 0) { IDetectorAlgorithm** lppAlgo = new IDetectorAlgorithm*[nAlgoCount]; nAlgoCount = pSrcTask->EnumAlgorithm(lppAlgo, nAlgoCount); for (int i = 0; i < nAlgoCount; i++) { if (lppAlgo[i]) { CDetectorAlgorithm* pNewAlgo = (CDetectorAlgorithm*)AddAlgorithm(lppAlgo[i]->GetAlgorithmInfo()); IDetectorAlgorithm* p = lppAlgo[i]; if (pNewAlgo && pNewAlgo->Initialize(this, m_pAlgoShare, m_pAlgoLibMgr) && !pNewAlgo->Copy(lppAlgo[i])) { delete[] lppAlgo; return false; } } } delete[] lppAlgo; } int nRoiCount = pSrcTask->EnumROI(NULL, 0); if (nRoiCount > 0) { IDetectorROI** lppROI = new IDetectorROI*[nRoiCount]; nRoiCount = pSrcTask->EnumROI(lppROI, nRoiCount); for (int j = 0; j < nRoiCount; j++) { if (lppROI[j]) { CDetectorROI* pNewRoi = (CDetectorROI*)AddROI(lppROI[j]->GetROIInfo()); if (pNewRoi && !pNewRoi->Copy(lppROI[j])) { delete[] lppROI; return false; } } } delete[] lppROI; } //QString strFileName = LP_DETECTOR_BUSSINESS_IMAGE_DIR + QString::number(m_pSolution->GetID()) + "_" + QString::number(m_nID) + ".bmp"; //IplImage* pImage = cvLoadImage(strFileName.toLatin1().data()); //m_tTask.pImg = pImage; //m_tTask.pImg = NULL; m_nAlgorithmBaseID = pSrcTask->GetAlgoBaseID(); if (pSrcTask->GetRunAlgo()) { m_nRunAlgoID = pSrcTask->GetRunAlgo()->GetID(); } else m_nRunAlgoID = LP_DETECTOR_INVALID_ID; return true; } bool CDetectorTask::SetRunAlgo(int nAlgoID) { m_nRunAlgoID = nAlgoID; return true; } IDetectorAlgorithm* CDetectorTask::GetRunAlgo() { return GetAlgorithm(m_nRunAlgoID); } IDetectorSolution* CDetectorTask::GetParentSolution() { //if (m_pSolution) // return m_pSolution->GetID(); //return LP_DETECTOR_INVALID_ID; return m_pSolution; } int CDetectorTask::GetProductCount() { for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { if (*iter) { IDetectorAlgorithm* pAlgo = *iter; int nParamCount = pAlgo->EnumOutParam(NULL, 0); if (nParamCount > 0) { LP_ALGORITHM_PARAM** lppParam = new LP_ALGORITHM_PARAM*[nParamCount]; nParamCount = pAlgo->EnumOutParam(lppParam, nParamCount); for (int k = 0; k < nParamCount; k++) { if (lppParam[k]->bIsSave) { if (lppParam[k]->type == LP_LIST_INT) { QList listParam = lppParam[k]->value.toList(); } switch (lppParam[k]->type) { case LP_LIST_INT: case LP_LIST_STRING: case LP_LIST_BOOLEAN: case LP_LIST_DOUBLE: { QList listParam = lppParam[k]->value.toList(); return listParam.size(); } default: break; } } } delete[] lppParam; } } } return 1; } QVariant CDetectorTask::GetSystemInfoValue(const QString &key) { if (m_pSystemInfo) { return m_pSystemInfo->GetValue(key); } return NULL; } bool CDetectorTask::PushImage(PLP_DETECTOR_IMAGE_CACHE_DATA pImageData) { //if (!m_pImageSaveMgr) return false; //return m_pImageSaveMgr->PushImage(pImageData); } void CDetectorTask::LoadUserParamValue() { for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { if (NULL != *iter) { CDetectorAlgorithm *p = (CDetectorAlgorithm*)*iter; p->LoadUserParamValue(LP_DETECTOR_INVALID_ID); } } } void CDetectorTask::SaveUserParamValue() { for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { if (NULL != *iter) { CDetectorAlgorithm *p = (CDetectorAlgorithm*)*iter; p->SaveUserParamValue(LP_DETECTOR_INVALID_ID); } } } void CDetectorTask::ClearUserParamValue() { for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { if (NULL != *iter) { CDetectorAlgorithm *p = (CDetectorAlgorithm*)*iter; p->ClearUserParamValue(LP_DETECTOR_INVALID_ID); } } } bool CDetectorTask::Exec() { int64 start = cv::getTickCount(); //LARGE_INTEGER litmp; //LONGLONG startTime, endTime; //double dfMinus, dfFreq, dfTime; //QueryPerformanceFrequency(&litmp); //dfFreq = (double)litmp.QuadPart; //QueryPerformanceCounter(&litmp); //startTime = litmp.QuadPart; //quint64 msec = QDateTime::currentMSecsSinceEpoch(); m_bRunning = true; for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { if (NULL != *iter) { CDetectorAlgorithm *pAlgo = (CDetectorAlgorithm*)*iter; if (pAlgo->IsEnabled()) pAlgo->ExecOnce(); } } m_bRunning = false; for (QVector::iterator iter = m_vecAlgorithm.begin(); iter != m_vecAlgorithm.end(); ++iter) { if (NULL != *iter) { CDetectorAlgorithm *p = (CDetectorAlgorithm*)*iter; p->SetRunStateToken(false); } } //QueryPerformanceCounter(&litmp); //endTime = litmp.QuadPart; //dfMinus = (double)(endTime - startTime); //dfTime = dfMinus / dfFreq; m_dExecTime = (cv::getTickCount() - start) / cv::getTickFrequency() * 1000; qDebug("task name:%s, task exec time: %.3f", m_tTask.strName.toLocal8Bit().data(), m_dExecTime); return true; } bool CDetectorTask::SyncMapResult(const QVariantMap& vMap) { if (!vMap.contains(BE_KEY_TASK_IMAGE) || !vMap.contains(BE_KEY_ALGO_RLT)) return false; for (auto it = vMap.begin(); it != vMap.end(); ++it) { if (it.key() == BE_KEY_TASK_IMAGE) { } else if (it.key() == BE_KEY_TASK_EXEC_TIME) { m_dExecTime = it.value().toDouble(); } else if (it.key() == BE_KEY_ALGO_RLT) { QVariantMap algoMap = it.value().toMap(); for (auto itOut = algoMap.begin(); itOut != algoMap.end(); ++itOut) { int nId = itOut.key().toInt(); CDetectorAlgorithm* pAlgo = (CDetectorAlgorithm*)GetAlgorithm(nId); if (pAlgo) { QList listOut = itOut.value().value>(); for each (LP_DETECTOR_TASK_OUT_PARAM paramOut in listOut) { if (paramOut.name == BE_KEY_ALGO_EXEC_TIME) { pAlgo->m_dExecTime = paramOut.value.toDouble(); } else if (paramOut.name == "roi") { //这里roi有特殊处理 pAlgo->SetRoiInfo(paramOut.value.value()); } else { pAlgo->SetOutParamValue(paramOut.name, paramOut.value); } } } } } } return true; }