完善检测逻辑完善存图逻辑

This commit is contained in:
2024-06-03 18:27:54 +08:00
parent d4a690d75f
commit d97ed362fb
24 changed files with 86 additions and 38 deletions
+3 -1
View File
@@ -110,7 +110,9 @@ else()
${ALGO_SRC_FILES}
${UI_FILES} "ff.rc" "icon.qrc" "uiModule/uiModule_switchProduct.h")
else()
add_executable( ${CURRENT_FOLDER} WIN32
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG")
add_executable( ${CURRENT_FOLDER}
${HEAD_FILES}
${SRC_FILES}
${BACK_HEAD_FILES}
+49 -11
View File
@@ -323,6 +323,41 @@ void pinHalconEvaluate(std::vector<cv::Rect>& Result,cv::Mat& src, cv::Mat& Pain
}
}
cv::Mat pinDetEvaluate(cv::Mat& src, handleAiModelManager* _handleAiModelManager)
{
cv::Mat resultImg;
std::vector<handdleAI::Bbox> result;
_handleAiModelManager->smalDet->Evaluate(src, result);
cv::Point centerPos(src.rows/2, src.cols/2);
std::vector<handdleAI::Bbox> targets;
cv::Rect targetRect(src.cols / 4, src.rows / 4, src.cols / 2, src.rows / 2);
for (auto detResult : result)
{
//cv::Rect rect(detResult.x - detResult.w / 2, detResult.y - detResult.h / 2, detResult.w, detResult.h);
if ((detResult.x< targetRect.x || detResult.x > targetRect.x + targetRect.width ||
detResult.y < targetRect.y || detResult.y > targetRect.y + targetRect.height)==false)
{
targets.push_back(detResult);
}
}
cv::Rect resultRect(INT_MAX,INT_MAX,INT_MIN,INT_MIN);
int targetSize = targets.size();
std::cout << targetSize;
for (auto detResult : targets)
{
resultRect.x = min(resultRect.x, detResult.x - detResult.w / 2);
resultRect.y = min(resultRect.y, detResult.y - detResult.h / 2);
}
for (auto detResult : targets)
{
resultRect.width = max(resultRect.width, detResult.w/2 + detResult.x - resultRect.x);
resultRect.height = max(resultRect.height, detResult.h/2 + detResult.y - resultRect.y);
}
return src(resultRect);
}
void ALGO_Inspection(std::shared_ptr <detData> data, handleAiModelManager* _handleAiModelManager)
{
if (data->oriImgs.size() < 1)
@@ -331,48 +366,51 @@ void ALGO_Inspection(std::shared_ptr <detData> data, handleAiModelManager* _hand
return;
}
cv::Mat src = data->oriImgs[0];
//DET
cv::Mat detResult = pinDetEvaluate(src, _handleAiModelManager);
cv::Mat Paint;
cvtColor(src, Paint, cv::COLOR_GRAY2BGR);
cvtColor(detResult, Paint, cv::COLOR_GRAY2BGR);
std::vector<cv::Rect>halconResult;
//hv_in_Parameter
//[0]-产品种类; 1A正面 2A反面 3B正面 4C正面
int productType = 3;
//[1]-图像序号第一位;
int pinRow = data->armPos / 2;
int pinRow = 1;
//[2]-图像序号第二位;
int pinCol = data->armPos % 2;
int pinCol = 0;
//[3]-3d相机精度(mm);
//[4]-ping针偏移判NG阈值(mm);
//[5]-切图存图开关
//HALCON
pinHalconEvaluate(halconResult,src, Paint, productType, pinRow, pinCol);
pinHalconEvaluate(halconResult, detResult, Paint, productType, pinRow, pinCol);
//HANDLEAI
switch(productType)
{
case 0:
pinHandleAiEvaluate(data, src(halconResult[0]), Paint(halconResult[0]),
pinHandleAiEvaluate(data, detResult, Paint,
_handleAiModelManager->largeSeg, _handleAiModelManager->largeCls);
break;
case 1://3 p
pinHandleAiEvaluate(data, src(halconResult[0]), Paint(halconResult[0]),
pinHandleAiEvaluate(data, detResult, Paint,
_handleAiModelManager->largeSeg, _handleAiModelManager->largeCls);
break;
case 2://2
pinHandleAiEvaluate(data, src(halconResult[0]), Paint(halconResult[0]),
pinHandleAiEvaluate(data, detResult, Paint,
_handleAiModelManager->midSeg, _handleAiModelManager->midCls);
break;
case 3://1
pinHandleAiEvaluate(data, src(halconResult[0]), Paint(halconResult[0]),
pinHandleAiEvaluate(data, detResult, Paint,
_handleAiModelManager->smallSeg, _handleAiModelManager->smallCls);
break;
}
//rectangle(Paint, halconResult[0], cv::Scalar(0, 0, 255), 4);
//set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
data->resultImgs.push_back(src(halconResult[0]).clone());//切下来的原图
data->resultImgs.push_back(Paint(halconResult[0]).clone());//AI检测结果
// data->resultImgs.push_back(detResult.clone());//切下来的原图
data->resultImgs.push_back(Paint.clone());//AI检测结果
_sleep(100);
}
@@ -77,10 +77,13 @@ backEndManager::~backEndManager()
}
void backEndManager::startManualDetSlot(cv::Mat mats, int algoIndex)
void backEndManager::startManualDetSlot(cv::Mat* mats, int algoIndex)
{
std::shared_ptr <detData> data = _detDataManager->insertDetData("Manual", "Manual", 0, 1);
data->oriImgs.push_back(mats);
data->oriImgMutex.lock();
data->oriImgs.push_back(mats->clone());
data->oriImgMutex.unlock();
delete mats;
_threadManager->addTask(data);//Ìí¼ÓÈÎÎñ
}
@@ -40,7 +40,7 @@ public slots:
this->start();
//std::async(std::launch::async, &backEndManager::run, this);
}
void startManualDetSlot(cv::Mat mats, int algoIndex);
void startManualDetSlot(cv::Mat* mats, int algoIndex);
private:
void run();
signals:
@@ -73,35 +73,34 @@ public:
std::string fileName;
for (auto str : data->imgSaveFileParameter)
fileName += str + "_";
std::string segClsDirName;
switch (type)
{
case oriImgSaveThread:
for (int i = 0; i < data->oriImgs.size(); i++)
{
fileName += getCurrentDateTime() + "_Count" + std::to_string(i) + ".bmp";
cv::imwrite(fileSavePath + fileName, data->oriImgs[i]);
cv::imwrite(fileSavePath + fileName + getCurrentDateTime() + "_Count" + std::to_string(i) + ".bmp", data->oriImgs[i]);
qInfo() << QString::fromStdString(data->cameraKeyName + " ori:" + fileSavePath + fileName);
}
break;
case resImgSaveThread:
for (int i = 0; i < data->resultImgs.size(); i++)
{
fileName += getCurrentDateTime() + "_Count" + std::to_string(i) + ".jpg";
cv::imwrite(fileSavePath + fileName, data->resultImgs[i]);
cv::imwrite(fileSavePath + fileName + getCurrentDateTime() + "_Count" + std::to_string(i) + ".jpg", data->resultImgs[i]);
qInfo() << QString::fromStdString(data->cameraKeyName + " ori:" + fileSavePath + fileName);
}
//保存seg原图和cls原图
createDirectoryIfNotExists(fileSavePath+"detImgs//");//文件夹处理
segClsDirName = fileSavePath + "detImgs//"+ getCurrentDateTime()+"//";
createDirectoryIfNotExists(segClsDirName);//文件夹处理
for (int i = 0; i < data->segOriginImgs.size(); i++)
{
fileName += getCurrentDateTime() + "_SEG_Count" + std::to_string(i) + ".bmp";
cv::imwrite(fileSavePath + "detImgs//" + fileName, data->segOriginImgs[i]);
;
cv::imwrite(segClsDirName + fileName + "SEG_Count_" + getCurrentDateTime() + "_" + std::to_string(i) + ".bmp", data->segOriginImgs[i]);
//qInfo() << QString::fromStdString(data->cameraKeyName + " ori:" + fileSavePath + fileName);
}
for (int i = 0; i < data->clsOriginImgs.size(); i++)
{
fileName += getCurrentDateTime() + "_CLS_RES_"+ std::to_string(data->clsOriginImgs[i].second) + "_Count" + std::to_string(i) + ".bmp";
cv::imwrite(fileSavePath + "detImgs//" + fileName, data->clsOriginImgs[i].first);
cv::imwrite(segClsDirName + fileName + "CLS_RES" + getCurrentDateTime() + "_" + std::to_string(data->clsOriginImgs[i].second) + "_Count" + std::to_string(i) + ".bmp", data->clsOriginImgs[i].first);
//qInfo() << QString::fromStdString(data->cameraKeyName + " ori:" + fileSavePath + fileName);
}
break;
@@ -10,7 +10,7 @@ void manualDetThread::run()
fileManager::getAllFiles(imgPathStr, files, "jpg");
for (auto file : files)
{
emit startManualDetSignal(cv::imread(file), algoIndex);
emit startManualDetSignal(new cv::Mat(cv::imread(file,-1)), algoIndex);
//mats.push_back(std::shared_ptr<cv::Mat>(std::make_shared<cv::Mat>(cv::imread(file))));
logWidget->append(u8"¼ì²âµ½Í¼Æ¬:" + QString::fromStdString(file));
}
@@ -27,7 +27,7 @@ public slots:
void manualDetSlot();
signals:
void finishImgReadSignal();
void startManualDetSignal(cv::Mat,int);
void startManualDetSignal(cv::Mat*,int);
};
class manualDetWidget : public QWidget
{
@@ -130,6 +130,10 @@ public:
connect(startEvaluateBtn, &QPushButton::clicked, this, [=]() {startEvaluateBtn->setDisabled(true); });
connect(_manualDetThread, &manualDetThread::finishImgReadSignal,this, &manualDetWidget::finishImgReadSlot);
}
~manualDetWidget()
{
delete _manualDetThread;
}
public slots:
void finishImgReadSlot()
{
+2 -2
View File
@@ -17,7 +17,7 @@
#define sColor_Black "22, 22, 22"
#define sColor_BlackWithWhite "45, 45, 45"
#define enableHandleAiModel 0
#define enableHandleAiModel 1
#define disableBackEndCheck 1
//窗口设置
@@ -105,5 +105,5 @@ static std::string getFileName(std::string path)
return path.substr(path.find_last_of("\\") + 1, path.find_last_of(".") - path.find_last_of("\\") - 1);
}
Q_DECLARE_METATYPE(imgWidgetShowType)
Q_DECLARE_METATYPE(cv::Mat)
Q_DECLARE_METATYPE(cv::Mat*)
#pragma once
@@ -86,7 +86,7 @@ public:
};
void init()
{
detPtr = handdleAI::CreateDetObjS((char*)path.c_str(), gpuIndex, 1);
detPtr = handdleAI::CreateDetObjS((char*)path.c_str(), 1, gpuIndex);
}
bool destroy()
{
@@ -124,23 +124,24 @@ public:
gpuMutex = std::vector<std::mutex*>(gpuCnt, new std::mutex());
#if enableHandleAiModel
int gpu1 = 0;
largeSeg = new handleAiModelSeg("./HandleAiModels/large-seg.BHD", gpu1, gpuMutex[gpu1]);
midSeg = new handleAiModelSeg("./HandleAiModels/mid-seg.BHD", gpu1, gpuMutex[gpu1]);
smallSeg = new handleAiModelSeg("./HandleAiModels/small-seg.BHD", gpu1, gpuMutex[gpu1]);
largeSeg = new handleAiModelSeg("./HandleAiModels/large_seg.BHD", gpu1, gpuMutex[gpu1]);
midSeg = new handleAiModelSeg("./HandleAiModels/mid_seg.BHD", gpu1, gpuMutex[gpu1]);
smallSeg = new handleAiModelSeg("./HandleAiModels/small_seg.BHD", gpu1, gpuMutex[gpu1]);
segModels.push_back(largeSeg);
segModels.push_back(midSeg);
segModels.push_back(smallSeg);
largeCls = new handleAiModelCls("./HandleAiModels/large-cls.BHD", gpu1, gpuMutex[gpu1]);
midCls = new handleAiModelCls("./HandleAiModels/mid-cls.BHD", gpu1, gpuMutex[gpu1]);
largeCls = new handleAiModelCls("./HandleAiModels/large_cls.BHD", gpu1, gpuMutex[gpu1]);
midCls = new handleAiModelCls("./HandleAiModels/mid_cls.BHD", gpu1, gpuMutex[gpu1]);
smallCls = new handleAiModelCls("./HandleAiModels/small_cls.BHD", gpu1, gpuMutex[gpu1]);
clsModels.push_back(largeCls);
clsModels.push_back(midCls);
clsModels.push_back(smallCls);
smalDet = new handleAiModelDet("./HandleAiModels/small_det.BHD", gpu1, gpuMutex[gpu1]);
detModels.push_back(smalDet);
#endif
}
///////////END/////////
@@ -151,6 +152,7 @@ public:
#if enableHandleAiModel
std::vector<std::future<void>> futuresSeg;
std::vector<std::future<void>> futuresCls;
std::vector<std::future<void>> futuresDet;
for(auto model: segModels)
{
futuresSeg.push_back(std::async(std::launch::async, &handleAiModelSeg::init, model));
@@ -163,7 +165,7 @@ public:
}
for (auto model : detModels)
{
futuresCls.push_back(std::async(std::launch::async, &handleAiModelDet::init, model));
futuresDet.push_back(std::async(std::launch::async, &handleAiModelDet::init, model));
emit _backEndManagerIn->sendInitString(QString::fromStdString(getFileName(model->path) + "初始化中..."));
}
@@ -184,7 +186,7 @@ public:
}
int detCls = 0;
for (auto& func : detModels)
for (auto& func : futuresDet)
{
func.wait();
emit _backEndManagerIn->sendInitString(QString::fromStdString(getFileName(detModels[detCls]->path) + "成功"));
+1 -1
View File
@@ -45,7 +45,7 @@ int main(int argc, char* argv[])
//////////
qRegisterMetaType<imgWidgetShowType>("imgWidgetShowType");
qRegisterMetaType<showImgPack>("showImgPack");
qRegisterMetaType<cv::Mat>("mat");
qRegisterMetaType<cv::Mat*>("matPtr");
MainWindow w;
w.show();
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.