Files
handleaiuinew/HanddleAI/HandleAiUi-new/back/detData.h
T
2024-08-07 09:16:35 +00:00

161 lines
5.1 KiB
C++

#include <unordered_map>
#include <string>
#include <filesystem>
#include <back/backStruct.h>
#include <random>
#include <chrono>
#include <qmutex.h>
#include <appConfig/appConfig.h>
//所有检测需要的数据均放在这里
class detDataManager;
class detData
{
public:
const unsigned long long timeCode = getTimeCode();//根据时间和随机数生成的值,唯一
std::chrono::steady_clock::time_point startTime = std::chrono::high_resolution_clock::now();
std::chrono::steady_clock::time_point getImageTime;
std::chrono::steady_clock::time_point algoStartTime;
std::chrono::steady_clock::time_point halconEndTime;
std::chrono::steady_clock::time_point handleAiEndTime;
std::chrono::steady_clock::time_point algoEndTime;
//参数
std::string SNcode;
std::string cameraKeyName;
QString algoThreadName;
std::string cameraNameStr;
double camera1Width = 0, camera1Height = 0;
int cameraIndex = -1; //是第几次拍照
int imgAmountMin = 1;
int algoThreadIndex = -1;
//检测参数
correctionStruct laserData;
int armPos = -1;
int productType = -1;
int oriShowIndex = -1;//原图在第几个框展示
int resShowIndex = -1;//结果图在第几个框展示
std::vector <std::shared_ptr<std::string>> resImgSavePath;
std::string snErrorsnError = std::string("snErrNoSn");
std::vector<QString> writeBackData;
void writeBack(QString type, int pos, QString value)
{
writeBackData.push_back(QString("write_%1_%2_%3").arg(type).arg(QString::number(pos)).arg(value));
}
detDataManager* dManager = nullptr;
static unsigned long long getTimeCode() {
auto now = std::chrono::high_resolution_clock::now();
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<unsigned long long> dis(0, std::numeric_limits<unsigned long long>::max());
unsigned long long randomValue = dis(gen);
unsigned long long uniqueNumber = (timestamp << 32) | randomValue;
return uniqueNumber;
}
void Init(std::string IN_cameraKeyName, QString IN_algoThreadName,
std::string IN_SN, int IN_oriShowIndex)
{
cameraKeyName = IN_cameraKeyName;
algoThreadName = IN_algoThreadName;
oriShowIndex = IN_oriShowIndex;
SNcode = IN_SN;
//algoThreadCls::datastruct _struct = bridge.back.json.algoThreads.getData(IN_algoThreadName);
//algoThreadIndex = _struct.threadIndex;
//resShowIndex = _struct.showIndex;
}
productDataStruct _productDataStruct;
//结果
double maxPinDetValue = 0;
bool aiDetOK = true;
bool halconOK = true;
int aiNgCount = 0, aiOkCount = 0, halconOkCount = 0, halconNgCount = 0;
//存图相关
QMutex resultMutex;//结果图锁
std::vector<cv::Mat> resultImgs;//结果图
std::vector<cv::Mat> segOriginImgs;//seg检测原图,共用结果图锁
std::vector<std::pair<std::string,cv::Mat>> clsOriginImgs;//cls检测原图,共用结果图锁
QMutex oriImgMutex;//原图锁
std::vector<cv::Mat> oriImgs;//原图
std::vector<std::string>imgSaveFileParameter;//命名规则 str1_str2... 第一个参数已默认为相机名,在构造函数
//需要存入的SQL数据
std::vector<std::pair<std::string, double>> sqlVec;
detData(std::string IN_cameraKeyName)
{
cameraKeyName = IN_cameraKeyName;
imgSaveFileParameter.push_back(cameraKeyName);
}
~detData()
{
for (auto& img : oriImgs)
img.release();
for (auto& img : resultImgs)
img.release();
for (auto& img : segOriginImgs)
img.release();
for (auto& img : clsOriginImgs)
img.second.release();
}
};
class detDataManager
{
public:
std::unordered_map<unsigned long long, std::shared_ptr<detData>>map;
QMutex dataMutex;
std::shared_ptr <detData> insertDetData(
QString IN_cameraKeyName,
std::string IN_SN,
int IN_oriShowIndex,
int IN_algoThreadIndex)
{
dataMutex.lock();
std::shared_ptr <detData>dataPtr(new detData(IN_cameraKeyName.toStdString()));
dataPtr->SNcode = IN_SN;
dataPtr->oriShowIndex = IN_oriShowIndex;
dataPtr->algoThreadIndex = IN_algoThreadIndex;
dataPtr->dManager = this;
map.insert({ dataPtr->timeCode,dataPtr });
dataMutex.unlock();
if (dataPtr == nullptr)
qDebug() << "创建新容器失败";
return dataPtr;
}
std::shared_ptr <detData> getDetData(unsigned long long timeCode)
{
dataMutex.lock();
auto it = map.find(timeCode);
if (it != map.end())
{
dataMutex.unlock();
return it->second;
}
dataMutex.unlock();
return nullptr;
}
bool deleteDetData(unsigned long long timeCode)
{
dataMutex.lock();
auto it = map.find(timeCode);
if (it != map.end())
{
it->second->oriImgMutex.lock();
//delete it->second;
map.erase(it);
dataMutex.unlock();
return true;
}
dataMutex.unlock();
return false;
}
};
#pragma once