2025-03-21 18:25:54 +08:00
|
|
|
#include <qthread.h>
|
|
|
|
|
#include <opencv.hpp>
|
|
|
|
|
#include <qqueue.h>
|
|
|
|
|
#include <qdatetime.h>
|
|
|
|
|
#include <backStruct.h>
|
2025-08-15 16:36:54 +08:00
|
|
|
#include <detData.h>
|
2025-08-20 16:57:03 +08:00
|
|
|
|
2025-03-21 18:25:54 +08:00
|
|
|
enum imgSaveThreadType
|
|
|
|
|
{
|
|
|
|
|
oriImg,
|
|
|
|
|
resImg,
|
|
|
|
|
segImg,
|
|
|
|
|
clsImg,
|
|
|
|
|
tiffOriImg
|
|
|
|
|
};
|
|
|
|
|
class imgSaveThread : public QThread
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
enum type{
|
|
|
|
|
normal,
|
|
|
|
|
priority
|
|
|
|
|
};
|
|
|
|
|
QMutex mutex;
|
|
|
|
|
std::vector<int> compressionParams{ cv::IMWRITE_PNG_COMPRESSION ,9};
|
|
|
|
|
QQueue<std::pair<std::string, cv::Mat>>priorityQueue;
|
|
|
|
|
QQueue<std::pair<std::string, cv::Mat>>imgQueue;
|
|
|
|
|
void run()
|
|
|
|
|
{
|
2025-07-11 02:02:09 +08:00
|
|
|
while (true)
|
2025-03-21 18:25:54 +08:00
|
|
|
{
|
2025-07-11 02:02:09 +08:00
|
|
|
QQueue<std::pair<std::string, cv::Mat>> localQueue;
|
|
|
|
|
|
|
|
|
|
// 优先处理 priorityQueue
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
|
if (!priorityQueue.isEmpty()) {
|
|
|
|
|
localQueue.swap(priorityQueue);
|
|
|
|
|
}
|
|
|
|
|
else if (!imgQueue.isEmpty()) {
|
|
|
|
|
localQueue.swap(imgQueue);
|
|
|
|
|
}
|
2025-03-21 18:25:54 +08:00
|
|
|
}
|
2025-07-11 02:02:09 +08:00
|
|
|
|
|
|
|
|
// 没有任务就跳出
|
|
|
|
|
if (localQueue.isEmpty()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 批量保存
|
|
|
|
|
while (!localQueue.isEmpty()) {
|
|
|
|
|
auto data = std::move(localQueue.dequeue());
|
2025-03-21 18:25:54 +08:00
|
|
|
qDebug() << "saving img " << QString::fromStdString(data.first);
|
2025-07-16 16:43:20 +08:00
|
|
|
#if saveImgEnable
|
2025-07-11 02:02:09 +08:00
|
|
|
cv::imwrite(data.first, data.second);
|
2025-07-16 16:43:20 +08:00
|
|
|
#endif
|
2025-08-31 16:51:18 +08:00
|
|
|
|
2025-03-21 18:25:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-11 02:02:09 +08:00
|
|
|
|
2025-03-21 18:25:54 +08:00
|
|
|
void addData(std::string str, cv::Mat& mat,type type_ = normal)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (mat.data == nullptr || &mat == nullptr)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "在存图线程中发现无效图像";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch(...)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "在存图线程中发现无效图像";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mutex.lock();
|
|
|
|
|
cv::Mat img;
|
|
|
|
|
mat.copyTo(img);
|
|
|
|
|
switch (type_) {
|
|
|
|
|
case normal:
|
|
|
|
|
imgQueue.append(std::make_pair(str, img));
|
|
|
|
|
break;
|
|
|
|
|
case priority:
|
|
|
|
|
priorityQueue.append(std::make_pair(str, img));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
qDebug() << "img enqueue" << QString::fromStdString(str)<<"current size"<< priorityQueue.size()<<","<< imgQueue.size();
|
|
|
|
|
mutex.unlock();
|
|
|
|
|
this->start();
|
|
|
|
|
}
|
|
|
|
|
int queueSize()
|
|
|
|
|
{
|
|
|
|
|
mutex.lock();
|
|
|
|
|
int size = imgQueue.size();
|
|
|
|
|
mutex.unlock();
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
class imgSaveThreadManager
|
|
|
|
|
{
|
|
|
|
|
public:
|
2025-06-13 01:55:47 +08:00
|
|
|
imgSaveThreadManager(std::string pathIn) {
|
2025-07-16 16:43:20 +08:00
|
|
|
path = QString::fromStdString(pathIn);
|
2025-06-13 01:55:47 +08:00
|
|
|
}
|
|
|
|
|
~imgSaveThreadManager() {};
|
2025-07-16 16:43:20 +08:00
|
|
|
void setPath(QString pathIn){
|
2025-06-11 10:00:09 +08:00
|
|
|
path = pathIn;
|
|
|
|
|
}
|
2025-03-21 18:25:54 +08:00
|
|
|
imgSaveThread saveThread;
|
2025-08-20 16:57:03 +08:00
|
|
|
QString genDirName(std::shared_ptr<detData> data, imgSaveThreadType type, saveMode mode){
|
|
|
|
|
// 一级目录(根路径)
|
|
|
|
|
const QString basePath = path;
|
|
|
|
|
|
|
|
|
|
// 二级目录(图片类型)
|
|
|
|
|
static const QMap<imgSaveThreadType, QString> typeMap{
|
|
|
|
|
{ oriImg, "原图" },
|
|
|
|
|
{ resImg, "结果图" }
|
|
|
|
|
};
|
|
|
|
|
QString imgTypeDir = typeMap.value(type, "error");
|
|
|
|
|
// 日期与小时
|
|
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
|
QString dateDir = now.toString("yyyy-MM-dd");
|
|
|
|
|
QString hourDir = QString("%1H").arg(now.time().hour());
|
|
|
|
|
|
|
|
|
|
// 检测结果
|
|
|
|
|
QString resultDir;
|
|
|
|
|
if (!data->aiDetOK && !data->halconOK) resultDir = "NG";
|
|
|
|
|
else if (!data->aiDetOK) resultDir = "aiNG";
|
|
|
|
|
else if (!data->halconOK) resultDir = "halconNG";
|
|
|
|
|
else resultDir = "OK";
|
|
|
|
|
|
|
|
|
|
// SN 或相机名
|
|
|
|
|
QString detailName = (data->SNcode.size() > 0 && data->SNcode.size() < 50)
|
|
|
|
|
? QString::fromStdString(data->SNcode)
|
|
|
|
|
: QString::fromStdString(data->cameraKeyName);
|
|
|
|
|
|
|
|
|
|
// 拼接路径
|
|
|
|
|
QDir dir(basePath);
|
|
|
|
|
dir = QDir(dir.filePath(imgTypeDir));
|
2025-08-31 16:51:18 +08:00
|
|
|
if (data->saveObjectName != "")
|
|
|
|
|
dir = QDir(dir.filePath(data->saveObjectName));
|
2025-08-20 16:57:03 +08:00
|
|
|
dir = QDir(dir.filePath(dateDir));
|
|
|
|
|
dir = QDir(dir.filePath(hourDir));
|
|
|
|
|
dir = QDir(dir.filePath(resultDir));
|
2025-08-31 16:51:18 +08:00
|
|
|
if (mode != oneFile)
|
|
|
|
|
dir = QDir(dir.filePath(detailName));
|
2025-08-20 16:57:03 +08:00
|
|
|
return dir.path() + "/";
|
2025-03-21 18:25:54 +08:00
|
|
|
}
|
2025-08-20 16:57:03 +08:00
|
|
|
|
2025-06-15 03:06:01 +08:00
|
|
|
std::vector<std::string> genFileNames(std::shared_ptr <detData> data, imgSaveThreadType type, std::string format = "")
|
2025-03-21 18:25:54 +08:00
|
|
|
{
|
|
|
|
|
std::vector<std::string> resultFileNames;
|
|
|
|
|
std::string fileName;
|
2025-07-25 00:29:52 +08:00
|
|
|
if (data->SNcode.length() != 0 && data->SNcode.length() < 50)
|
|
|
|
|
fileName += data->SNcode;
|
|
|
|
|
else
|
|
|
|
|
fileName += data->cameraKeyName;
|
2025-06-15 03:06:01 +08:00
|
|
|
for (auto str : data->imgSaveNameParameter)
|
2025-03-21 18:25:54 +08:00
|
|
|
fileName += str + "_";
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case oriImg:
|
|
|
|
|
for (int i = 0; i < data->oriImgs.size(); i++)
|
|
|
|
|
{
|
2025-06-15 03:06:01 +08:00
|
|
|
if (i < data->oriFormat.size() )
|
|
|
|
|
resultFileNames.push_back(fileName + getCurrentDateTime() + "_Count" + std::to_string(i) + "." + data->oriFormat[i]);
|
2025-03-21 18:25:54 +08:00
|
|
|
else
|
|
|
|
|
resultFileNames.push_back(fileName + getCurrentDateTime() + "_Count" + std::to_string(i) + ".bmp");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case resImg:
|
|
|
|
|
for (int i = 0; i < data->resultImgs.size(); i++)
|
|
|
|
|
resultFileNames.push_back(fileName + getCurrentDateTime() + "_Count" + std::to_string(i) + ".jpg");
|
|
|
|
|
break;
|
|
|
|
|
case segImg:
|
|
|
|
|
for (int i = 0; i < data->segOriginImgs.size(); i++)
|
|
|
|
|
resultFileNames.push_back(fileName + "SEG_" + getCurrentDateTime() + "_" + std::to_string(i) + ".png");
|
|
|
|
|
break;
|
|
|
|
|
case clsImg:
|
|
|
|
|
for (int i = 0; i < data->clsOriginImgs.size(); i++)
|
|
|
|
|
resultFileNames.push_back(fileName + "CLS_RES" + getCurrentDateTime() + "_" + data->clsOriginImgs[i].first + "_Count" + std::to_string(i) + ".bmp");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return resultFileNames;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 16:57:03 +08:00
|
|
|
void saveOri(std::shared_ptr <detData> data) {
|
|
|
|
|
QString fileSavePath = genDirName(data, oriImg, data->_saveMode);//获取文件夹名
|
2025-03-21 18:25:54 +08:00
|
|
|
createDirectoryIfNotExists(fileSavePath);//不存在此文件夹则创建
|
2025-06-15 03:06:01 +08:00
|
|
|
std::vector<std::string>fileNames = genFileNames(data, oriImg);
|
2025-07-16 16:43:20 +08:00
|
|
|
|
2025-03-21 18:25:54 +08:00
|
|
|
for (int index = 0; index < fileNames.size(); index++)
|
2025-07-16 16:43:20 +08:00
|
|
|
saveThread.addData(fileSavePath.toLocal8Bit().toStdString() + fileNames[index], data->oriImgs[index]);
|
2025-03-21 18:25:54 +08:00
|
|
|
int queueSize = saveThread.queueSize();
|
|
|
|
|
if (queueSize > 10)
|
|
|
|
|
qDebug() << "queueSize " << queueSize;
|
|
|
|
|
}
|
2025-08-20 16:57:03 +08:00
|
|
|
void saveRes(std::shared_ptr <detData> data){
|
|
|
|
|
QString fileSavePath = genDirName(data, resImg,data->_saveMode);//获取文件夹名
|
2025-03-21 18:25:54 +08:00
|
|
|
createDirectoryIfNotExists(fileSavePath);//不存在此文件夹则创建
|
|
|
|
|
//结果图保存
|
2025-06-15 03:06:01 +08:00
|
|
|
std::vector<std::string>resFileNames = genFileNames(data, resImg);
|
2025-03-21 18:25:54 +08:00
|
|
|
for (int index = 0; index < resFileNames.size(); index++)
|
2025-07-16 16:43:20 +08:00
|
|
|
saveThread.addData(fileSavePath.toLocal8Bit().toStdString() + resFileNames[index], data->resultImgs[index], imgSaveThread::priority);
|
2025-03-21 18:25:54 +08:00
|
|
|
|
|
|
|
|
//seg
|
2025-06-15 03:06:01 +08:00
|
|
|
std::vector<std::string>segFileNames = genFileNames(data, segImg);
|
2025-03-21 18:25:54 +08:00
|
|
|
for (int index = 0; index < segFileNames.size(); index++)
|
2025-07-16 16:43:20 +08:00
|
|
|
saveThread.addData(fileSavePath.toLocal8Bit().toStdString() + segFileNames[index], data->segOriginImgs[index]);
|
2025-03-21 18:25:54 +08:00
|
|
|
|
|
|
|
|
//cls
|
2025-06-15 03:06:01 +08:00
|
|
|
std::vector<std::string>clsFileNames = genFileNames(data, clsImg);
|
2025-03-21 18:25:54 +08:00
|
|
|
for (int index = 0; index < clsFileNames.size(); index++)
|
2025-07-16 16:43:20 +08:00
|
|
|
saveThread.addData(fileSavePath.toLocal8Bit().toStdString() + clsFileNames[index], data->clsOriginImgs[index].second);
|
2025-03-21 18:25:54 +08:00
|
|
|
|
|
|
|
|
//额外储存NG文件夹
|
2025-07-11 02:02:09 +08:00
|
|
|
//std::string additionalSaveNgImgPath = genDirName(data, additionalSaveNgImg);
|
|
|
|
|
//std::vector<std::string>additionalSaveNgImgNames = genFileNames(data, additionalSaveNgImg);
|
|
|
|
|
//if (additionalSaveNgImgNames.size() > 0)
|
|
|
|
|
// createDirectoryIfNotExists(additionalSaveNgImgPath);//不存在此文件夹则创建
|
|
|
|
|
//int additionalSaveNgImgNamesIndex = 0;
|
|
|
|
|
//for (int index = 0; index < data->segOriginImgs.size()&& additionalSaveNgImgNames.size() > additionalSaveNgImgNamesIndex; index++)
|
|
|
|
|
//{
|
|
|
|
|
// saveThread.addData(additionalSaveNgImgPath + additionalSaveNgImgNames[additionalSaveNgImgNamesIndex], data->segOriginImgs[index]);
|
|
|
|
|
// additionalSaveNgImgNamesIndex++;
|
|
|
|
|
//}
|
|
|
|
|
//for (int index = 0; index < data->oriImgs.size() && additionalSaveNgImgNames.size() > additionalSaveNgImgNamesIndex; index++)
|
|
|
|
|
//{
|
|
|
|
|
// saveThread.addData(additionalSaveNgImgPath + additionalSaveNgImgNames[additionalSaveNgImgNamesIndex], data->oriImgs[index]);
|
|
|
|
|
// additionalSaveNgImgNamesIndex++;
|
|
|
|
|
//}
|
|
|
|
|
//for (int index = 0; index < data->resultImgs.size() && additionalSaveNgImgNames.size() > additionalSaveNgImgNamesIndex; index++)
|
|
|
|
|
//{
|
|
|
|
|
// saveThread.addData(additionalSaveNgImgPath + additionalSaveNgImgNames[additionalSaveNgImgNamesIndex], data->resultImgs[index]);
|
|
|
|
|
// additionalSaveNgImgNamesIndex++;
|
|
|
|
|
//}
|
2025-03-21 18:25:54 +08:00
|
|
|
}
|
|
|
|
|
private:
|
2025-07-16 16:43:20 +08:00
|
|
|
QString path = "";
|
2025-03-21 18:25:54 +08:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#pragma once
|