263 lines
7.4 KiB
C++
263 lines
7.4 KiB
C++
#include <iostream>
|
|
#include <qthread.h>
|
|
#include <qqueue.h>
|
|
#include <qmutex.h>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <filesystem>
|
|
#include <opencv.hpp>
|
|
#include <string>
|
|
#include <handleAi/handleAiModel.h>
|
|
#include <sqliteManager/sqlManager.h>
|
|
#include <QtConcurrent/qtconcurrent>
|
|
#include <QMetaType>
|
|
#include <fileManager.h>
|
|
#include <qtcpserver.h>
|
|
#include <qtcpsocket.h>
|
|
#include <mutex>
|
|
#include <backStruct.h>
|
|
#include <memory>
|
|
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::array<bool, 20>skip;
|
|
std::vector<std::pair<std::string, double>> sqlVec;
|
|
|
|
std::vector<cv::Mat> imgs;
|
|
std::vector<cv::Mat> 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<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(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 <detData> CreateDetData(QString IN_cameraKeyName, QString IN_algoThreadName,
|
|
std::string IN_SN, int IN_oriShowIndex)
|
|
{
|
|
std::shared_ptr <detData> 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<unsigned long long, std::shared_ptr<detData>>map;
|
|
|
|
//QLinkedList<detData> detDatas;
|
|
QMutex dataMutex;
|
|
//std::shared_ptr <detData> insertDetData(QString IN_cameraKeyName, QString IN_algoThreadName,
|
|
// std::string IN_SN, int IN_oriShowIndex)
|
|
//{
|
|
// dataMutex.lock();
|
|
// std::shared_ptr <detData> 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 <detData> dataPtr = getDetData(data->timeCode);
|
|
//
|
|
// if (dataPtr == nullptr)
|
|
// qDebug() << "创建新容器失败";
|
|
// return dataPtr;
|
|
//}
|
|
std::shared_ptr <detData> insertDetData(QString IN_cameraKeyName, QString IN_algoThreadName,
|
|
std::string IN_SN, int IN_oriShowIndex)
|
|
{
|
|
dataMutex.lock();
|
|
std::shared_ptr <detData>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 <detData> dataPtr = getDetData(data->timeCode);
|
|
|
|
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->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;
|
|
};
|
|
QList<snPack>snWaittingVec;
|
|
QList<snPack>snResList;
|
|
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);
|
|
QQueue<std::shared_ptr <detData>> detDataQueue;
|
|
sqlManager* sql;
|
|
//handleAiModel models;//初始化模型
|
|
autoDelete* autoDeleteThread;
|
|
|
|
|
|
|
|
void run();
|
|
void stop()
|
|
{
|
|
runFlag = false;
|
|
this->terminate();
|
|
}
|
|
void resultHandle(std::shared_ptr <detData>);
|
|
|
|
void addTask(std::shared_ptr <detData>);
|
|
sncodeCls snDatas;
|
|
~threadManager();
|
|
|
|
QMutex queueMutex;
|
|
|
|
QMutex insertQueueMutex;
|
|
QQueue<std::shared_ptr <detData>>insertQueue;
|
|
void process();
|
|
private:
|
|
std::list<std::shared_ptr <detData>> waitImgData;
|
|
std::list<std::shared_ptr <detData>> processData;
|
|
std::unordered_map<unsigned long long, std::future<void>> futures;
|
|
std::unordered_map<unsigned long long, std::future<void>>::iterator futrueIte;
|
|
|
|
bool runFlag = true;
|
|
QList<std::shared_ptr <detData>>imgWaitList;
|
|
std::vector<std::shared_ptr <detData>>deleteVec;
|
|
};
|
|
|
|
#pragma once
|