#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include class threadManager; class algoThread; class subThread; class detDataManager; //class detData; struct saveStruct { std::string filePath; std::string fileName; cv::Mat file; }; struct MatchResult { cv::Rect boundingRect; double confidence; }; class detData { public: const unsigned long long timeCode = getTimeCode();//根据时间和随机数生成的值,唯一 QMutex imgMutex; std::string SNcode; QString cameraKeyName; QString algoThreadName; std::string cameraNameStr; correctionStruct* correctionData=nullptr; double camera1Width = 0, camera1Height = 0; int algoThreadIndex = -1;//进入第几个算法线程 int cameraIndex = -1; //是第几次拍照 int imgAmountMin = 1; std::arrayskip; std::vector> sqlVec; std::vector imgs; std::vector resultImgs; int oriShowIndex = -1;//原图在第几个框展示 int resShowIndex = -1;//结果图在第几个框展示 std::string snErrorsnError = std::string("snErrNoSn"); detDataManager* dManager=nullptr; static unsigned long long getTimeCode() { auto now = std::chrono::high_resolution_clock::now(); auto timestamp = std::chrono::duration_cast(now.time_since_epoch()).count(); std::random_device rd; std::mt19937_64 gen(rd()); std::uniform_int_distribution dis(0, std::numeric_limits::max()); unsigned long long randomValue = dis(gen); unsigned long long uniqueNumber = (timestamp << 32) | randomValue; return uniqueNumber; } void Init(QString 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; } static std::shared_ptr CreateDetData(QString IN_cameraKeyName, QString IN_algoThreadName, std::string IN_SN, int IN_oriShowIndex) { std::shared_ptr data(new detData(IN_cameraKeyName, IN_algoThreadName, IN_SN, IN_oriShowIndex)); return data; } detData(QString IN_cameraKeyName, QString IN_algoThreadName, std::string IN_SN, int IN_oriShowIndex) { cameraKeyName = IN_cameraKeyName; algoThreadName = IN_algoThreadName; oriShowIndex = IN_oriShowIndex; SNcode = IN_SN; //imgMutex = new QMutex(); } ~detData() { for (auto img : imgs) img.release(); for (auto img : resultImgs) img.release(); //delete cameraImgPtr; } void clear() { imgs.clear(); //timeCode = -1; resShowIndex = -1; oriShowIndex = -1; algoThreadIndex = -1; cameraIndex = -1; //snCountIndex = -1; std::string SNcode = ""; } bool isEmpty() { if (imgs.size() == 0) return true; else return false; } }; class detDataManager { public: std::unordered_map>map; //QLinkedList detDatas; QMutex dataMutex; //std::shared_ptr insertDetData(QString IN_cameraKeyName, QString IN_algoThreadName, // std::string IN_SN, int IN_oriShowIndex) //{ // dataMutex.lock(); // std::shared_ptr data = new detData(IN_cameraKeyName, IN_algoThreadName, IN_SN, IN_oriShowIndex); // data->dManager = this; // //detDatas.push_back(data); // map.insert({ data->timeCode,data }); // dataMutex.unlock(); // std::shared_ptr dataPtr = getDetData(data->timeCode); // // if (dataPtr == nullptr) // qDebug() << "创建新容器失败"; // return dataPtr; //} std::shared_ptr insertDetData(QString IN_cameraKeyName, QString IN_algoThreadName, std::string IN_SN, int IN_oriShowIndex) { dataMutex.lock(); std::shared_ptr data(new detData(IN_cameraKeyName, IN_algoThreadName, IN_SN, IN_oriShowIndex)); data->dManager = this; //detDatas.push_back(data); map.insert({ data->timeCode,data }); dataMutex.unlock(); std::shared_ptr dataPtr = getDetData(data->timeCode); if (dataPtr == nullptr) qDebug() << "创建新容器失败"; return dataPtr; } std::shared_ptr 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->imgMutex.lock(); //delete it->second; map.erase(it); dataMutex.unlock(); return true; } dataMutex.unlock(); return false; } }; class sncodeCls { public: struct snPack { unsigned long long timecode; std::string sn; int startpos; }; QListsnWaittingVec; QListsnResList; std::string snErrorsnError = "snErrNoSn"; int tcpStartPos=0; bool clearSameSnRequestData(); void checkSnFromWaittingVec(QByteArray byte); std::string readSnFromQByteArray(QByteArray byte, int dmPos, bool highLowConversion, bool clearAfterRead, long long timeCode, int Size = -1); QString snHex(QString sn); std::string getData(unsigned long long timeCode); std::string waitForSn(unsigned long long timeCode, int timeOut=1000); sncodeCls(); }; /// /// 线程池管理 /// class threadManager : public QThread { Q_OBJECT public: explicit threadManager(QObject* parent = nullptr); QMutex queueMutex; QQueue> detDataQueue; sqlManager* sql; //handleAiModel models;//初始化模型 autoDelete* autoDeleteThread; QMutex insertQueueMutex; QQueue>insertQueue; std::vectoralgoThreadList; void run(); void stop() { runFlag = false; this->terminate(); } void addTask(std::shared_ptr ); bool addTaskFreeThread(std::shared_ptr ); sncodeCls snDatas; ~threadManager(); private: bool runFlag = true; QList>imgWaitList; std::vector>deleteVec; }; #pragma once