update 极性用引擎调用
This commit is contained in:
+230
-199
@@ -1,4 +1,4 @@
|
||||
#include "Hd_AI_algo_jiXing_cam1.h"
|
||||
#include "Hd_AI_algo_jiXing_L.h"
|
||||
#include <QDateTime>
|
||||
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
@@ -7,141 +7,142 @@
|
||||
#include <filesystem>
|
||||
#include <myTools.h>
|
||||
#include <algo.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
//原图保存文件夹s
|
||||
QString oriImgSaveDir = "D:/601线/终焊CCD/极性检测CCD/相机2/原图/";
|
||||
QString oriImgSaveDir = "D:/601线/终焊CCD/极性检测CCD/相机2/原图/";
|
||||
//原图class图保存文件夹
|
||||
QString classImgSaveDir = "D:/601线/终焊CCD/极性检测CCD/相机2/class图/";
|
||||
QString classImgSaveDir = "D:/601线/终焊CCD/极性检测CCD/相机2/class图/";
|
||||
//结果保存文件夹
|
||||
QString resImgSaveDir = "D:/601线/终焊CCD/极性检测CCD/相机2/结果图/";
|
||||
int algoResult = 0;//0 OK 1 NG
|
||||
QString resImgSaveDir = "D:/601线/终焊CCD/极性检测CCD/相机2/结果图/";
|
||||
int algoResult = 0;//0 OK 1 NG
|
||||
//切图的class的width,根据项目实际设置!
|
||||
int sizeW = 150;
|
||||
int sizeW = 150;
|
||||
//切图的class的height,根据项目实际设置!
|
||||
int sizeH = 150;
|
||||
int sizeH = 150;
|
||||
//切图的class的width,根据项目实际设置!0为黑,255为白;
|
||||
int fillColour = 0;
|
||||
int fillColour = 0;
|
||||
|
||||
|
||||
//检测线程锁
|
||||
QMutex* detect_mutex;
|
||||
QMutex* detect_mutex;
|
||||
//保存线程锁
|
||||
QMutex* saveStruct_mutex;
|
||||
QMutex* saveStruct_mutex;
|
||||
//检测线程运行标志
|
||||
bool ifdetectThread = false;
|
||||
bool ifdetectThread = false;
|
||||
|
||||
QString cameraKey_index = "";
|
||||
QString cameraKey_index = "";
|
||||
|
||||
//初始化参数列表
|
||||
QByteArrayList initValueList;
|
||||
//初始化参数列表
|
||||
QByteArrayList initValueList;
|
||||
|
||||
static void createDirectoryIfNotExists(const QString& path) {
|
||||
QDir dir;
|
||||
dir.mkpath(path);
|
||||
}
|
||||
static std::string getCurrentTime() {
|
||||
static void createDirectoryIfNotExists(const QString& path) {
|
||||
QDir dir;
|
||||
dir.mkpath(path);
|
||||
}
|
||||
static std::string getCurrentTime() {
|
||||
|
||||
// 获取当前时间点
|
||||
auto now = std::chrono::system_clock::now();
|
||||
// 获取当前时间点
|
||||
auto now = std::chrono::system_clock::now();
|
||||
|
||||
// 转换为字符串
|
||||
auto currentTime = std::chrono::system_clock::to_time_t(now);
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 100;
|
||||
std::tm* localTime = std::localtime(¤tTime);
|
||||
std::stringstream ss;
|
||||
ss << std::put_time(localTime, "%H-%M-%S") << "-" << std::setfill('0') << std::setw(2) << ms.count();
|
||||
return ss.str();
|
||||
}
|
||||
static QString getCurrentDate()
|
||||
{
|
||||
QDateTime currentDateTime = QDateTime::currentDateTime();
|
||||
return currentDateTime.toString("yyyy-MM-dd");
|
||||
}
|
||||
static QString getCurrentHour()
|
||||
{
|
||||
QDateTime currentDateTime = QDateTime::currentDateTime();
|
||||
return QString::number(currentDateTime.time().hour())+"H";
|
||||
}
|
||||
QString byteArrayToUnicode(const QByteArray array) {
|
||||
// 转换为字符串
|
||||
auto currentTime = std::chrono::system_clock::to_time_t(now);
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 100;
|
||||
std::tm* localTime = std::localtime(¤tTime);
|
||||
std::stringstream ss;
|
||||
ss << std::put_time(localTime, "%H-%M-%S") << "-" << std::setfill('0') << std::setw(2) << ms.count();
|
||||
return ss.str();
|
||||
}
|
||||
static QString getCurrentDate()
|
||||
{
|
||||
QDateTime currentDateTime = QDateTime::currentDateTime();
|
||||
return currentDateTime.toString("yyyy-MM-dd");
|
||||
}
|
||||
static QString getCurrentHour()
|
||||
{
|
||||
QDateTime currentDateTime = QDateTime::currentDateTime();
|
||||
return QString::number(currentDateTime.time().hour()) + "H";
|
||||
}
|
||||
QString byteArrayToUnicode(const QByteArray array) {
|
||||
|
||||
// state用于保存转换状态,它的成员invalidChars,可用来判断是否转换成功
|
||||
// 如果转换成功,则值为0,如果值大于0,则说明转换失败
|
||||
QTextCodec::ConverterState state;
|
||||
// 先尝试使用utf-8的方式把QByteArray转换成QString
|
||||
QString text = QTextCodec::codecForName("UTF-8")->toUnicode(array.constData(), array.size(), &state);
|
||||
// 如果转换时无效字符数量大于0,说明编码格式不对
|
||||
if (state.invalidChars > 0)
|
||||
{
|
||||
// 再尝试使用GBK的方式进行转换,一般就能转换正确(当然也可能是其它格式,但比较少见了)
|
||||
text = QTextCodec::codecForName("GBK")->toUnicode(array);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
static cv::Rect findMaxAreaContour(cv::Mat src)
|
||||
{
|
||||
int maxIndex = 0, maxArea = 0;
|
||||
std::vector<std::vector<cv::Point>>contours;
|
||||
cv::findContours(src, contours, cv::noArray(), cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
|
||||
if (contours.size() == 0)
|
||||
return cv::Rect(0, 0, 0, 0);
|
||||
for (int i = 0; i < contours.size(); i++)
|
||||
{
|
||||
int Area = cv::contourArea(contours[i]);
|
||||
if (Area > maxArea)
|
||||
{
|
||||
maxArea = Area;
|
||||
maxIndex = i;
|
||||
}
|
||||
}
|
||||
if (contours.size() != 0)
|
||||
return cv::boundingRect(contours[maxIndex]);
|
||||
return cv::Rect(0, 0, 0, 0);
|
||||
}
|
||||
inline std::string utf8_to_gbk(const std::string& str)
|
||||
{
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
|
||||
std::wstring tmp_wstr = conv.from_bytes(str);
|
||||
//GBK locale name in windows
|
||||
const char* GBK_LOCALE_NAME = ".936";
|
||||
std::wstring_convert<std::codecvt_byname<wchar_t, char, mbstate_t>> convert(new std::codecvt_byname<wchar_t, char, mbstate_t>(GBK_LOCALE_NAME));
|
||||
return convert.to_bytes(tmp_wstr);
|
||||
}
|
||||
void getAllFiles(std::string path, std::vector<std::string>& files, std::string format)
|
||||
{
|
||||
intptr_t hFile = 0;//文件句柄
|
||||
struct _finddata_t fileinfo;//文件信息
|
||||
std::string p;
|
||||
if ((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1) //文件存在
|
||||
{
|
||||
do
|
||||
{
|
||||
if ((fileinfo.attrib & _A_SUBDIR))//判断是否为文件夹
|
||||
{
|
||||
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)//文件夹名中不含"."和".."
|
||||
{
|
||||
files.push_back(p.assign(path).append("\\").append(fileinfo.name)); //保存文件夹名
|
||||
getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files, format); //递归遍历文件夹
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
files.push_back(p.assign(path).append("\\").append(fileinfo.name));//如果不是文件夹,储存文件名
|
||||
}
|
||||
} while (_findnext(hFile, &fileinfo) == 0);
|
||||
_findclose(hFile);
|
||||
}
|
||||
}
|
||||
QString geteOriSaveFromat()
|
||||
{
|
||||
return oriImgSaveDir + getCurrentDate() + "//" + getCurrentHour() + "//"
|
||||
+ "ORI_" + cameraKey_index + "_" + QString::fromStdString(getCurrentTime()) + ".bmp";
|
||||
}
|
||||
QString geteResSaveFromat()
|
||||
{
|
||||
return resImgSaveDir + getCurrentDate() + "//" + getCurrentHour() + "//"
|
||||
+ "RES_" + cameraKey_index + "_" + QString::fromStdString(getCurrentTime()) + ".png";
|
||||
}
|
||||
// state用于保存转换状态,它的成员invalidChars,可用来判断是否转换成功
|
||||
// 如果转换成功,则值为0,如果值大于0,则说明转换失败
|
||||
QTextCodec::ConverterState state;
|
||||
// 先尝试使用utf-8的方式把QByteArray转换成QString
|
||||
QString text = QTextCodec::codecForName("UTF-8")->toUnicode(array.constData(), array.size(), &state);
|
||||
// 如果转换时无效字符数量大于0,说明编码格式不对
|
||||
if (state.invalidChars > 0)
|
||||
{
|
||||
// 再尝试使用GBK的方式进行转换,一般就能转换正确(当然也可能是其它格式,但比较少见了)
|
||||
text = QTextCodec::codecForName("GBK")->toUnicode(array);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
static cv::Rect findMaxAreaContour(cv::Mat src)
|
||||
{
|
||||
int maxIndex = 0, maxArea = 0;
|
||||
std::vector<std::vector<cv::Point>>contours;
|
||||
cv::findContours(src, contours, cv::noArray(), cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
|
||||
if (contours.size() == 0)
|
||||
return cv::Rect(0, 0, 0, 0);
|
||||
for (int i = 0; i < contours.size(); i++)
|
||||
{
|
||||
int Area = cv::contourArea(contours[i]);
|
||||
if (Area > maxArea)
|
||||
{
|
||||
maxArea = Area;
|
||||
maxIndex = i;
|
||||
}
|
||||
}
|
||||
if (contours.size() != 0)
|
||||
return cv::boundingRect(contours[maxIndex]);
|
||||
return cv::Rect(0, 0, 0, 0);
|
||||
}
|
||||
inline std::string utf8_to_gbk(const std::string& str)
|
||||
{
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
|
||||
std::wstring tmp_wstr = conv.from_bytes(str);
|
||||
//GBK locale name in windows
|
||||
const char* GBK_LOCALE_NAME = ".936";
|
||||
std::wstring_convert<std::codecvt_byname<wchar_t, char, mbstate_t>> convert(new std::codecvt_byname<wchar_t, char, mbstate_t>(GBK_LOCALE_NAME));
|
||||
return convert.to_bytes(tmp_wstr);
|
||||
}
|
||||
void getAllFiles(std::string path, std::vector<std::string>& files, std::string format)
|
||||
{
|
||||
intptr_t hFile = 0;//文件句柄
|
||||
struct _finddata_t fileinfo;//文件信息
|
||||
std::string p;
|
||||
if ((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1) //文件存在
|
||||
{
|
||||
do
|
||||
{
|
||||
if ((fileinfo.attrib & _A_SUBDIR))//判断是否为文件夹
|
||||
{
|
||||
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)//文件夹名中不含"."和".."
|
||||
{
|
||||
files.push_back(p.assign(path).append("\\").append(fileinfo.name)); //保存文件夹名
|
||||
getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files, format); //递归遍历文件夹
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
files.push_back(p.assign(path).append("\\").append(fileinfo.name));//如果不是文件夹,储存文件名
|
||||
}
|
||||
} while (_findnext(hFile, &fileinfo) == 0);
|
||||
_findclose(hFile);
|
||||
}
|
||||
}
|
||||
QString geteOriSaveFromat()
|
||||
{
|
||||
return oriImgSaveDir + getCurrentDate() + "//" + getCurrentHour() + "//"
|
||||
+ "ORI_" + cameraKey_index + "_" + QString::fromStdString(getCurrentTime()) + ".bmp";
|
||||
}
|
||||
QString getResSaveFromat()
|
||||
{
|
||||
return resImgSaveDir + getCurrentDate() + "//" + getCurrentHour() + "//"
|
||||
+ "RES_" + cameraKey_index + "_" + QString::fromStdString(getCurrentTime()) + ".png";
|
||||
}
|
||||
// 缺陷信息结构体
|
||||
typedef struct {
|
||||
QString defectName = "";//缺陷名称
|
||||
@@ -443,7 +444,7 @@ void drawDefects(cv::Mat& sourceMat, QQueue<defectInfo> defectInfoList)
|
||||
|
||||
|
||||
//判断文件夹是否存在,不存在则创建
|
||||
void Hd_AI_algo_jiXing_cam1::isDirExist(QString fullDirName)
|
||||
void Hd_AI_algo_jiXing_L::isDirExist(QString fullDirName)
|
||||
{
|
||||
QDir* photo = new QDir;
|
||||
bool exist = photo->exists(fullDirName);
|
||||
@@ -461,23 +462,23 @@ void Hd_AI_algo_jiXing_cam1::isDirExist(QString fullDirName)
|
||||
photo = nullptr;
|
||||
}
|
||||
|
||||
Hd_AI_algo_jiXing_cam1* create()
|
||||
Hd_AI_algo_jiXing_L* create()
|
||||
{
|
||||
qDebug() << "[INFO] " << " Hd_AI_algo_jiXing_cam1 create success!" ;
|
||||
return new Hd_AI_algo_jiXing_cam1;
|
||||
qDebug() << "[INFO] " << " Hd_AI_algo_jiXing_L create success!";
|
||||
return new Hd_AI_algo_jiXing_L;
|
||||
}
|
||||
|
||||
void destory(Hd_AI_algo_jiXing_cam1* ptr)
|
||||
void destory(Hd_AI_algo_jiXing_L* ptr)
|
||||
{
|
||||
qDebug() << "[INFO] " << "Hd_AI_algo_jiXing_cam1 destory success!";
|
||||
if(ptr)
|
||||
qDebug() << "[INFO] " << "Hd_AI_algo_jiXing_L destory success!";
|
||||
if (ptr)
|
||||
{
|
||||
delete ptr;
|
||||
ptr = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Hd_AI_algo_jiXing_cam1::Hd_AI_algo_jiXing_cam1()
|
||||
Hd_AI_algo_jiXing_L::Hd_AI_algo_jiXing_L()
|
||||
{
|
||||
detect_mutex = new QMutex;
|
||||
saveStruct_mutex = new QMutex;
|
||||
@@ -501,7 +502,7 @@ Hd_AI_algo_jiXing_cam1::Hd_AI_algo_jiXing_cam1()
|
||||
}
|
||||
}
|
||||
|
||||
Hd_AI_algo_jiXing_cam1::~Hd_AI_algo_jiXing_cam1()
|
||||
Hd_AI_algo_jiXing_L::~Hd_AI_algo_jiXing_L()
|
||||
{
|
||||
ifdetectThread = false;
|
||||
Sleep(10);
|
||||
@@ -520,25 +521,25 @@ Hd_AI_algo_jiXing_cam1::~Hd_AI_algo_jiXing_cam1()
|
||||
DestroyClsObj(pCls);*/
|
||||
}
|
||||
//从类中读数据到实例对象
|
||||
bool Hd_AI_algo_jiXing_cam1::readData(std::vector<cv::Mat>&vec_mat,QByteArray &data)
|
||||
bool Hd_AI_algo_jiXing_L::readData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
|
||||
{
|
||||
detect_mutex->lock();
|
||||
//qDebug() << __FUNCTION__ << " line:" <<__LINE__ << " data len:" << data.length();
|
||||
if (resMat.size()>0)
|
||||
if (resMat.size() > 0)
|
||||
{
|
||||
vec_mat.push_back(resMat[0]);
|
||||
resMat.clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
if(HikMat.size()>0)
|
||||
if (HikMat.size() > 0)
|
||||
vec_mat.push_back(HikMat[0]);
|
||||
//qDebug() << __FUNCTION__ << " line:" << __LINE__ << " resMat.data err:";
|
||||
}
|
||||
//if (allNum++ % 2 == 0)
|
||||
// data = "2*3*2";// resByte.dequeue();
|
||||
//else
|
||||
data = "1*&" + QByteArray::number(algoResult) + "&+" + QByteArray::number(algoResult);//1OK,2NG
|
||||
data = "1*" + QByteArray::number(algoResult) + "&+" + QByteArray::number(algoResult);//1OK,2NG
|
||||
resByte.clear();
|
||||
////之后根据结果类修改
|
||||
//if(resIfNg == true)
|
||||
@@ -546,40 +547,41 @@ bool Hd_AI_algo_jiXing_cam1::readData(std::vector<cv::Mat>&vec_mat,QByteArray &d
|
||||
//else data = "OK";
|
||||
detect_mutex->unlock();
|
||||
/*if(cameraKey_index.contains('_'))
|
||||
qDebug() << "[INFO] " << __FUNCTION__ << " line:" << __LINE__ << " checkImg getDetectData--CameraName:"
|
||||
qDebug() << "[INFO] " << __FUNCTION__ << " line:" << __LINE__ << " checkImg getDetectData--CameraName:"
|
||||
<< cameraKey_index.split("_")[0] << " imgIndex:" << cameraKey_index.split("_")[1];
|
||||
else
|
||||
qDebug() << "[INFO] " << __FUNCTION__ << " line:" << __LINE__ << " checkImg getDetectData--CameraName imgIndex:" << cameraKey_index;*/
|
||||
return true;
|
||||
}
|
||||
//从类中读数据到实例对象
|
||||
bool Hd_AI_algo_jiXing_cam1::encodeResultValue(QVector<std::string> append_value,QByteArray &resultByte)
|
||||
bool Hd_AI_algo_jiXing_L::encodeResultValue(QVector<std::string> append_value, QByteArray& resultByte)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string sendMsg;
|
||||
for(int i = 0 ; i < append_value.size();i++)
|
||||
for (int i = 0; i < append_value.size(); i++)
|
||||
{
|
||||
std::string value = append_value.at(1);
|
||||
//type
|
||||
int type = 3;
|
||||
std::string m_type;
|
||||
m_type.insert(0,4,'1');
|
||||
memcpy ( (void*)m_type.c_str(),(void*)&type,4 );
|
||||
m_type.insert(0, 4, '1');
|
||||
memcpy((void*)m_type.c_str(), (void*)&type, 4);
|
||||
sendMsg.append(m_type);
|
||||
|
||||
//len
|
||||
int len = value.length();
|
||||
std::string m_len;
|
||||
m_len.insert(0,4,'1');
|
||||
memcpy ( (void*)m_len.c_str(),(void*)&len,4 );
|
||||
m_len.insert(0, 4, '1');
|
||||
memcpy((void*)m_len.c_str(), (void*)&len, 4);
|
||||
sendMsg.append(m_len);
|
||||
|
||||
//data x
|
||||
sendMsg.append(value);
|
||||
}
|
||||
resultByte = QByteArray::fromStdString(sendMsg);
|
||||
} catch (QString ev)
|
||||
}
|
||||
catch (QString ev)
|
||||
{
|
||||
qCritical() << "[Error] " << " ev:" << ev;
|
||||
return false;
|
||||
@@ -587,13 +589,14 @@ bool Hd_AI_algo_jiXing_cam1::encodeResultValue(QVector<std::string> append_value
|
||||
return true;
|
||||
}
|
||||
|
||||
int myallNum[4] = {0};
|
||||
int myallNum[4] = { 0 };
|
||||
QMap<int, cv::Mat> lastImgMap;
|
||||
//接收数据
|
||||
bool Hd_AI_algo_jiXing_cam1::writeData(std::vector<cv::Mat>& vec_mat, QByteArray &data)
|
||||
bool Hd_AI_algo_jiXing_L::writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
|
||||
{
|
||||
//qDebug() << __FUNCTION__ << "line:"<< __LINE__ << " data len" << data.length() ;
|
||||
if(vec_mat.empty())
|
||||
|
||||
if (vec_mat.empty())
|
||||
{
|
||||
qCritical() << "[Error] " << " vec_mat is null !";
|
||||
return false;
|
||||
@@ -601,7 +604,7 @@ bool Hd_AI_algo_jiXing_cam1::writeData(std::vector<cv::Mat>& vec_mat, QByteArray
|
||||
detect_mutex->lock();
|
||||
HikMat.clear();
|
||||
for (int i = 0; i < vec_mat.size(); i++)
|
||||
HikMat.enqueue(vec_mat[i]);
|
||||
HikMat.enqueue(vec_mat[i]);
|
||||
|
||||
QByteArray source_savePath;
|
||||
QByteArray sData = data;
|
||||
@@ -612,7 +615,7 @@ bool Hd_AI_algo_jiXing_cam1::writeData(std::vector<cv::Mat>& vec_mat, QByteArray
|
||||
cameraKey_index = splitDataList[2];
|
||||
QByteArray saveDir = splitDataList[0] + "/" + splitDataList[1] + "/";
|
||||
isDirExist(byteArrayToUnicode(saveDir)); //不存在,创建
|
||||
QString sdataTime= QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss-zzz");
|
||||
QString sdataTime = QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss-zzz");
|
||||
source_savePath = splitDataList[0] + "/" + splitDataList[1] + "/" + sdataTime.toLocal8Bit() + "-" + splitDataList[2] + "-" + splitDataList[3];// +".bmp";
|
||||
}
|
||||
//int index = -1;
|
||||
@@ -690,7 +693,7 @@ bool Hd_AI_algo_jiXing_cam1::writeData(std::vector<cv::Mat>& vec_mat, QByteArray
|
||||
|
||||
/*saveKey.enqueue("datalist[0]");
|
||||
Dir2D.enqueue("datalist[1]");
|
||||
QStringList SN_dataTime;
|
||||
QStringList SN_dataTime;
|
||||
SN_dataTime = datalist[3].split('_');
|
||||
DateTime1 = "SN_dataTime[1]";
|
||||
SN.enqueue("SN_dataTime[0]");*/
|
||||
@@ -707,7 +710,7 @@ bool Hd_AI_algo_jiXing_cam1::writeData(std::vector<cv::Mat>& vec_mat, QByteArray
|
||||
|
||||
//传入initParas函数,格式为:相机key+参数名1#参数值1#参数值2+参数名2#参数值1...
|
||||
//初始化
|
||||
bool Hd_AI_algo_jiXing_cam1::initParas(QByteArray& byte)
|
||||
bool Hd_AI_algo_jiXing_L::initParas(QByteArray& byte)
|
||||
{
|
||||
initValueList = byte.split('+');
|
||||
QStringList valueiList;
|
||||
@@ -745,9 +748,9 @@ bool Hd_AI_algo_jiXing_cam1::initParas(QByteArray& byte)
|
||||
|
||||
//线程检测:结果类完成时,使用
|
||||
//ifThreadRunDetect = true;
|
||||
//QFuture<void> future1 = QtConcurrent::run(this, &Hd_AI_algo_jiXing_cam1::detectThread);
|
||||
//QFuture<void> future1 = QtConcurrent::run(this, &Hd_AI_algo_jiXing_L::detectThread);
|
||||
//运行存图线程
|
||||
QFuture<void> future2 = QtConcurrent::run(this, &Hd_AI_algo_jiXing_cam1::saveImgThread);
|
||||
QFuture<void> future2 = QtConcurrent::run(this, &Hd_AI_algo_jiXing_L::saveImgThread);
|
||||
//detectThread();
|
||||
|
||||
//原图总文件夹
|
||||
@@ -758,48 +761,51 @@ bool Hd_AI_algo_jiXing_cam1::initParas(QByteArray& byte)
|
||||
isDirExist(resImgSaveDir);
|
||||
|
||||
//qDebug() << "[INFO] " << " StationNo:" << StationNo << " img2D:" << img2D ;
|
||||
myEngine.SetProcedurePath("./halconFilesAlgo/JX_ColorInspection.hdpl"); //设置hdvp路径
|
||||
return true;
|
||||
}
|
||||
// 解码
|
||||
bool Hd_AI_algo_jiXing_cam1::decodeAiValue(QByteArray byte,QVector<QByteArray> &vec_ret)
|
||||
bool Hd_AI_algo_jiXing_L::decodeAiValue(QByteArray byte, QVector<QByteArray>& vec_ret)
|
||||
{
|
||||
if(byte.isEmpty())
|
||||
if (byte.isEmpty())
|
||||
{
|
||||
qCritical() << "[Error] " << " byte is null!";
|
||||
return false;
|
||||
}
|
||||
|
||||
/*******************************************/
|
||||
byte.remove(0,8);
|
||||
QByteArray mm_data = byte.mid(0,4);
|
||||
memcpy((void*)¶mNum,(void*)mm_data.data(), 4);
|
||||
byte.remove(0, 8);
|
||||
QByteArray mm_data = byte.mid(0, 4);
|
||||
memcpy((void*)¶mNum, (void*)mm_data.data(), 4);
|
||||
qDebug() << "[INFO] " << " paramNum:" << paramNum;
|
||||
byte.remove(0,4);
|
||||
byte.remove(0, 4);
|
||||
/*******************************************/
|
||||
|
||||
while (!byte.isEmpty())
|
||||
{
|
||||
int type = 0;
|
||||
QByteArray m_type = byte.mid(0,4);
|
||||
memcpy((void*)&type,(void*)m_type.data(), 4);
|
||||
byte.remove(0,4);
|
||||
QByteArray m_type = byte.mid(0, 4);
|
||||
memcpy((void*)&type, (void*)m_type.data(), 4);
|
||||
byte.remove(0, 4);
|
||||
|
||||
int len = 0;
|
||||
QByteArray m_len = byte.mid(0,4);
|
||||
memcpy((void*)&len,(void*)m_len.data(), 4);
|
||||
byte.remove(0,4);
|
||||
QByteArray m_len = byte.mid(0, 4);
|
||||
memcpy((void*)&len, (void*)m_len.data(), 4);
|
||||
byte.remove(0, 4);
|
||||
|
||||
QByteArray mm_data = byte.mid(0,len);
|
||||
QByteArray mm_data = byte.mid(0, len);
|
||||
vec_ret.append(mm_data);
|
||||
byte.remove(0,len);
|
||||
byte.remove(0, len);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//---------检测流程,根据实际项目修改---------
|
||||
bool Hd_AI_algo_jiXing_cam1::AIdetect(cv::Mat img2D, QString mysaveKey, QString myDir2D, QString mySN, QString myDataTime)
|
||||
bool Hd_AI_algo_jiXing_L::AIdetect(cv::Mat img2D, QString mysaveKey, QString myDir2D, QString mySN, QString myDataTime)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
//存图数据加保存队列,加锁,避免数据访问冲突
|
||||
saveStruct_mutex->lock();
|
||||
saveImgStruct mySave;
|
||||
@@ -827,35 +833,60 @@ bool Hd_AI_algo_jiXing_cam1::AIdetect(cv::Mat img2D, QString mysaveKey, QString
|
||||
// csvName.close();
|
||||
//}
|
||||
//sourceImg = cv::imread("D:\\mySN15-46-33-86.bmp",-1);
|
||||
//sourceImg = cv::imread("D:/1/2024-10-17-14-07-07-225-DA4203326_0-errSN.bmp", -1);
|
||||
//sourceImg = cv::imread("F:/CCD/TEST/20241024-091531.bmp", -1);
|
||||
cv::Mat Paint;
|
||||
sourceImg.copyTo(Paint);
|
||||
if(Paint.channels() == 1)
|
||||
if (Paint.channels() == 1)
|
||||
cvtColor(Paint, Paint, COLOR_GRAY2BGR);
|
||||
|
||||
|
||||
HObject ho_Image;
|
||||
HTuple hv_out_Value;
|
||||
HTuple hv_Make;
|
||||
MatToHObject(sourceImg, &ho_Image);
|
||||
HDevEngineCpp::HDevProcedure proc("JX_Inspection");
|
||||
HDevProcedureCall procedureCall(proc);
|
||||
|
||||
_628_2_Polarity_detection(ho_Image, &hv_out_Value, &hv_Make);
|
||||
if(hv_Make.D() == 1)
|
||||
HObject in_Image;
|
||||
HTuple out_Flag, out_ColorType;
|
||||
MatToHObject(sourceImg, &in_Image);
|
||||
|
||||
procedureCall.SetInputIconicParamObject("in_Image", in_Image);//
|
||||
procedureCall.Execute();//执行halcon程序
|
||||
|
||||
out_Flag = procedureCall.GetOutputCtrlParamTuple("out_Flag");//获取输出图像类变量
|
||||
out_ColorType = procedureCall.GetOutputCtrlParamTuple("out_ColorType");//获取输出控制类变量
|
||||
|
||||
if (out_Flag.D() == 1)
|
||||
{
|
||||
int res1 = hv_out_Value[0].D();
|
||||
int res2 = hv_out_Value[1].D();
|
||||
if(res1 == 1)
|
||||
putTextHusky(Paint, utf8_to_gbk("铝").c_str(), cv::Point(0, 0), cv::Scalar(0, 255, 0), 500);
|
||||
else
|
||||
int res1 = out_ColorType[0].D();
|
||||
int res2 = out_ColorType[1].D();
|
||||
//1铜 2铝+
|
||||
switch (res1)
|
||||
{
|
||||
case 0:
|
||||
putTextHusky(Paint, utf8_to_gbk("异常").c_str(), cv::Point(0, 0), cv::Scalar(0, 0, 255), 300);
|
||||
break;
|
||||
case 1:
|
||||
putTextHusky(Paint, utf8_to_gbk("铜").c_str(), cv::Point(0, 0), cv::Scalar(0, 255, 0), 500);
|
||||
break;
|
||||
case 2:
|
||||
putTextHusky(Paint, utf8_to_gbk("铝").c_str(), cv::Point(0, 0), cv::Scalar(0, 255, 0), 500);
|
||||
break;
|
||||
}
|
||||
|
||||
if (res2 == 1)
|
||||
putTextHusky(Paint, utf8_to_gbk("铝").c_str(), cv::Point(0, Paint.rows - 500), cv::Scalar(0, 255, 0), 500);
|
||||
else
|
||||
switch (res1)
|
||||
{
|
||||
case 0:
|
||||
putTextHusky(Paint, utf8_to_gbk("异常").c_str(), cv::Point(0, Paint.rows - 500), cv::Scalar(0, 0, 255), 300);
|
||||
break;
|
||||
case 1:
|
||||
putTextHusky(Paint, utf8_to_gbk("铜").c_str(), cv::Point(0, Paint.rows - 500), cv::Scalar(0, 255, 0), 500);
|
||||
break;
|
||||
case 2:
|
||||
putTextHusky(Paint, utf8_to_gbk("铝").c_str(), cv::Point(0, Paint.rows - 500), cv::Scalar(0, 255, 0), 500);
|
||||
break;
|
||||
|
||||
//CAM1 为铜OK,CAM2为铝OK
|
||||
if (res1 == 1 && res2 == 1)//铝
|
||||
}
|
||||
|
||||
//T 为铜OK,L为铝OK
|
||||
if (res1 == 2 && res2 == 2)//铝ok
|
||||
{
|
||||
putTextHusky(Paint, utf8_to_gbk("OK").c_str(), cv::Point(Paint.cols - 800, 0), cv::Scalar(0, 255, 0), 500);
|
||||
algoResult = 1;
|
||||
@@ -872,12 +903,12 @@ bool Hd_AI_algo_jiXing_cam1::AIdetect(cv::Mat img2D, QString mysaveKey, QString
|
||||
|
||||
QQueue<defectInfo> defectInfoList;
|
||||
|
||||
|
||||
|
||||
detect_mutex->lock();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
resMat.push_back(Paint);
|
||||
QJsonArray detaillists;
|
||||
@@ -901,10 +932,10 @@ bool Hd_AI_algo_jiXing_cam1::AIdetect(cv::Mat img2D, QString mysaveKey, QString
|
||||
detailObj.insert("defectRect.width", QString::number(mdefect.defectRect.width));
|
||||
detailObj.insert("defectRect.height", QString::number(mdefect.defectRect.height));
|
||||
detailObj.insert("defectScalar0", QString::number(mdefect.defectScalar[0]));
|
||||
detailObj.insert("defectScalar1", QString::number(mdefect.defectScalar[1]));
|
||||
detailObj.insert("defectScalar1", QString::number(mdefect.defectScalar[1]));
|
||||
detailObj.insert("defectScalar2", QString::number(mdefect.defectScalar[2]));
|
||||
detailObj.insert("defectArea", QString::number(mdefect.defectArea));
|
||||
detailObj.insert("defectLength", QString::number(sqrt(mdefect.defectRect.width* mdefect.defectRect.width+ mdefect.defectRect.height* mdefect.defectRect.height)));
|
||||
detailObj.insert("defectLength", QString::number(sqrt(mdefect.defectRect.width * mdefect.defectRect.width + mdefect.defectRect.height * mdefect.defectRect.height)));
|
||||
|
||||
detaillists.append(detailObj);
|
||||
}
|
||||
@@ -920,10 +951,10 @@ bool Hd_AI_algo_jiXing_cam1::AIdetect(cv::Mat img2D, QString mysaveKey, QString
|
||||
//isDirExist(resImgSaveDir + mysaveKey1 + "/");
|
||||
QString qimgsavePath = resImgSaveDir + mysaveKey1 + "/" + mySN + myDataTime + ".png";
|
||||
//存图数据入队,按需
|
||||
if (resMat.size() > 0 )
|
||||
if (resMat.size() > 0)
|
||||
{
|
||||
createDirectoryIfNotExists(resImgSaveDir + getCurrentDate() + "//" + getCurrentHour() + "//");
|
||||
addImgToSaveStruct(resMat[0], "0", geteResSaveFromat());//保存原图
|
||||
addImgToSaveStruct(resMat[0], "0", getResSaveFromat());//保存原图
|
||||
}
|
||||
detect_mutex->unlock();
|
||||
|
||||
@@ -967,7 +998,7 @@ bool Hd_AI_algo_jiXing_cam1::AIdetect(cv::Mat img2D, QString mysaveKey, QString
|
||||
}
|
||||
|
||||
//存图加入队列
|
||||
void Hd_AI_algo_jiXing_cam1::addImgToSaveStruct(cv::Mat img2D, QString mysaveKey, QString savePath)
|
||||
void Hd_AI_algo_jiXing_L::addImgToSaveStruct(cv::Mat img2D, QString mysaveKey, QString savePath)
|
||||
{
|
||||
//存图数据加保存队列,加锁,避免数据访问冲突
|
||||
saveStruct_mutex->lock();
|
||||
@@ -979,7 +1010,7 @@ void Hd_AI_algo_jiXing_cam1::addImgToSaveStruct(cv::Mat img2D, QString mysaveKey
|
||||
saveStruct.enqueue(mySave);
|
||||
saveStruct_mutex->unlock();
|
||||
}
|
||||
void Hd_AI_algo_jiXing_cam1::saveImgThread()
|
||||
void Hd_AI_algo_jiXing_L::saveImgThread()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
@@ -995,7 +1026,7 @@ void Hd_AI_algo_jiXing_cam1::saveImgThread()
|
||||
saveImgStruct mysaveStruct = saveStruct.dequeue();
|
||||
saveStruct_mutex->unlock();
|
||||
stop = clock();
|
||||
if(stop-start >1000)
|
||||
if (stop - start > 1000)
|
||||
std::cout << "lock stop-start >1000::************************" << stop - start << std::endl;
|
||||
start = clock();
|
||||
if (mysaveStruct.out_Mat.data)
|
||||
@@ -1003,17 +1034,17 @@ void Hd_AI_algo_jiXing_cam1::saveImgThread()
|
||||
//std::cout << "savePath::" << mysaveStruct.savePath << std::endl;
|
||||
stop = clock();
|
||||
if (stop - start > 1000)
|
||||
std::cout << "imwrite stop-start >1000::--------------------------"<< stop - start << std::endl;
|
||||
std::cout << "imwrite stop-start >1000::--------------------------" << stop - start << std::endl;
|
||||
_sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
//检测线程,应当只运行一次
|
||||
void Hd_AI_algo_jiXing_cam1::detectThread()
|
||||
void Hd_AI_algo_jiXing_L::detectThread()
|
||||
{
|
||||
do
|
||||
{
|
||||
if (!ifdetectThread)return;
|
||||
if (!ifdetectThread)return;
|
||||
|
||||
//此两行代码,避免程序卡死;_sleep的时间根据实际项目调整
|
||||
_sleep(2);
|
||||
@@ -1043,7 +1074,7 @@ void Hd_AI_algo_jiXing_cam1::detectThread()
|
||||
isDirExist(resImgSaveDir + mysaveKey);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//测试相机的,
|
||||
// while (sourceImg1.cols > 2000)cv::resize(sourceImg1, sourceImg1, cv::Size(), 0.5, 0.5);
|
||||
|
||||
@@ -1058,14 +1089,14 @@ void Hd_AI_algo_jiXing_cam1::detectThread()
|
||||
myDir2D.clear();
|
||||
mySN.clear();
|
||||
myDataTime.clear();
|
||||
qApp->processEvents();
|
||||
qApp->processEvents();
|
||||
|
||||
} while (ifThreadRunDetect);
|
||||
|
||||
|
||||
}
|
||||
|
||||
static int runNum = 0;
|
||||
bool Hd_AI_algo_jiXing_cam1::run()
|
||||
bool Hd_AI_algo_jiXing_L::run()
|
||||
{
|
||||
clock_t start, stop;
|
||||
start = clock();
|
||||
@@ -1073,14 +1104,14 @@ bool Hd_AI_algo_jiXing_cam1::run()
|
||||
//Sleep(1000);
|
||||
detectThread();
|
||||
stop = clock();
|
||||
qDebug() << __FUNCTION__ << "line:" << __LINE__ << " run start ::runNum::" << runNum++<<" cutTime::"<<stop-start;
|
||||
qDebug() << __FUNCTION__ << "line:" << __LINE__ << " run start ::runNum::" << runNum++ << " cutTime::" << stop - start;
|
||||
|
||||
|
||||
|
||||
//qDebug() << __FUNCTION__ << "line:" << __LINE__ << "[INFO] " << "Mat_name: " << Mat_name << " run end";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Hd_AI_algo_jiXing_cam1::checkStatus()
|
||||
bool Hd_AI_algo_jiXing_L::checkStatus()
|
||||
{
|
||||
status = moduleStatus;
|
||||
return status;
|
||||
+21
-18
@@ -1,5 +1,5 @@
|
||||
#ifndef Hd_AI_algo_jiXing_cam1_H
|
||||
#define Hd_AI_algo_jiXing_cam1_H
|
||||
#ifndef Hd_AI_algo_jiXing_L_H
|
||||
#define Hd_AI_algo_jiXing_L_H
|
||||
|
||||
#include <assert.h>
|
||||
#include <atlstr.h>
|
||||
@@ -21,7 +21,9 @@
|
||||
#include "base.h"
|
||||
#include "handleAI.h"
|
||||
#pragma execution_character_set("utf-8")
|
||||
#include <HDevEngineCpp.h>
|
||||
|
||||
using namespace HDevEngineCpp;
|
||||
|
||||
|
||||
//保存图片的结构体
|
||||
@@ -32,16 +34,16 @@ struct saveImgStruct;
|
||||
// std::string savePath; //保存路径
|
||||
//};
|
||||
|
||||
//初始化流程(一次):Hd_AI_algo_jiXing_cam1,initParas
|
||||
//初始化流程(一次):Hd_AI_algo_jiXing_L,initParas
|
||||
//运行流程:writeData主线程把数据传过来,run,readData此线程把数据传主线程
|
||||
class Hd_AI_algo_jiXing_cam1 :public Base
|
||||
class Hd_AI_algo_jiXing_L :public Base
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Hd_AI_algo_jiXing_cam1();
|
||||
virtual ~Hd_AI_algo_jiXing_cam1();
|
||||
Hd_AI_algo_jiXing_L();
|
||||
virtual ~Hd_AI_algo_jiXing_L();
|
||||
//从类中读数据到实例对象
|
||||
bool readData(std::vector<cv::Mat>&,QByteArray&);
|
||||
bool readData(std::vector<cv::Mat>&, QByteArray&);
|
||||
//实例对象把数据写入到类
|
||||
bool writeData(std::vector<cv::Mat>&, QByteArray&);
|
||||
bool initParas(QByteArray&);
|
||||
@@ -50,14 +52,15 @@ public:
|
||||
//线程状态
|
||||
bool checkStatus();// { return true; };
|
||||
|
||||
bool encodeResultValue(QVector<std::string> append_value,QByteArray &resultByte);
|
||||
bool decodeAiValue(QByteArray byte,QVector<QByteArray> &vec_ret);
|
||||
bool encodeResultValue(QVector<std::string> append_value, QByteArray& resultByte);
|
||||
bool decodeAiValue(QByteArray byte, QVector<QByteArray>& vec_ret);
|
||||
//---------检测流程,根据实际项目修改---------
|
||||
bool AIdetect(cv::Mat img2D, QString saveKey, QString Dir2D, QString SN, QString DataTime);
|
||||
|
||||
|
||||
private:
|
||||
HDevEngine myEngine;
|
||||
int allNum = 0;
|
||||
int StationNo=0;
|
||||
int StationNo = 0;
|
||||
int img2D = 2;
|
||||
|
||||
bool moduleStatus = true;
|
||||
@@ -65,13 +68,13 @@ private:
|
||||
//输入图片队列
|
||||
QQueue<cv::Mat> HikMat;
|
||||
//输入关键字队列
|
||||
QString saveKey="saveKey";
|
||||
QString saveKey = "saveKey";
|
||||
//结果路径文件夹队列
|
||||
QString Dir2D="Dir2D";
|
||||
QString Dir2D = "Dir2D";
|
||||
//产品SN队列
|
||||
QString SN="SN";
|
||||
QString SN = "SN";
|
||||
//产品时间戳队列
|
||||
QString DateTime1="DateTime1";
|
||||
QString DateTime1 = "DateTime1";
|
||||
|
||||
int paramNum;
|
||||
|
||||
@@ -88,7 +91,7 @@ private:
|
||||
void detectThread();
|
||||
//存图线程
|
||||
void saveImgThread();
|
||||
|
||||
|
||||
//保存图片队列
|
||||
QQueue<saveImgStruct> saveStruct;
|
||||
|
||||
@@ -101,8 +104,8 @@ private:
|
||||
|
||||
extern "C"
|
||||
{
|
||||
Q_DECL_EXPORT Hd_AI_algo_jiXing_cam1 * create();
|
||||
Q_DECL_EXPORT void destory(Hd_AI_algo_jiXing_cam1 * ptr);
|
||||
Q_DECL_EXPORT Hd_AI_algo_jiXing_L* create();
|
||||
Q_DECL_EXPORT void destory(Hd_AI_algo_jiXing_L* ptr);
|
||||
}
|
||||
//保存图片的结构体
|
||||
struct saveImgStruct
|
||||
+77
-47
@@ -1,4 +1,4 @@
|
||||
#include "Hd_AI_algo_jiXing_cam1.h"
|
||||
#include "Hd_AI_algo_jiXing_T.h"
|
||||
#include <QDateTime>
|
||||
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <filesystem>
|
||||
#include <myTools.h>
|
||||
#include <algo.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
//原图保存文件夹s
|
||||
@@ -443,7 +444,7 @@ void drawDefects(cv::Mat& sourceMat, QQueue<defectInfo> defectInfoList)
|
||||
|
||||
|
||||
//判断文件夹是否存在,不存在则创建
|
||||
void Hd_AI_algo_jiXing_cam1::isDirExist(QString fullDirName)
|
||||
void Hd_AI_algo_jiXing_T::isDirExist(QString fullDirName)
|
||||
{
|
||||
QDir* photo = new QDir;
|
||||
bool exist = photo->exists(fullDirName);
|
||||
@@ -461,15 +462,15 @@ void Hd_AI_algo_jiXing_cam1::isDirExist(QString fullDirName)
|
||||
photo = nullptr;
|
||||
}
|
||||
|
||||
Hd_AI_algo_jiXing_cam1* create()
|
||||
Hd_AI_algo_jiXing_T* create()
|
||||
{
|
||||
qDebug() << "[INFO] " << " Hd_AI_algo_jiXing_cam1 create success!" ;
|
||||
return new Hd_AI_algo_jiXing_cam1;
|
||||
qDebug() << "[INFO] " << " Hd_AI_algo_jiXing_T create success!" ;
|
||||
return new Hd_AI_algo_jiXing_T;
|
||||
}
|
||||
|
||||
void destory(Hd_AI_algo_jiXing_cam1* ptr)
|
||||
void destory(Hd_AI_algo_jiXing_T* ptr)
|
||||
{
|
||||
qDebug() << "[INFO] " << "Hd_AI_algo_jiXing_cam1 destory success!";
|
||||
qDebug() << "[INFO] " << "Hd_AI_algo_jiXing_T destory success!";
|
||||
if(ptr)
|
||||
{
|
||||
delete ptr;
|
||||
@@ -477,7 +478,7 @@ void destory(Hd_AI_algo_jiXing_cam1* ptr)
|
||||
}
|
||||
}
|
||||
|
||||
Hd_AI_algo_jiXing_cam1::Hd_AI_algo_jiXing_cam1()
|
||||
Hd_AI_algo_jiXing_T::Hd_AI_algo_jiXing_T()
|
||||
{
|
||||
detect_mutex = new QMutex;
|
||||
saveStruct_mutex = new QMutex;
|
||||
@@ -501,7 +502,7 @@ Hd_AI_algo_jiXing_cam1::Hd_AI_algo_jiXing_cam1()
|
||||
}
|
||||
}
|
||||
|
||||
Hd_AI_algo_jiXing_cam1::~Hd_AI_algo_jiXing_cam1()
|
||||
Hd_AI_algo_jiXing_T::~Hd_AI_algo_jiXing_T()
|
||||
{
|
||||
ifdetectThread = false;
|
||||
Sleep(10);
|
||||
@@ -520,7 +521,7 @@ Hd_AI_algo_jiXing_cam1::~Hd_AI_algo_jiXing_cam1()
|
||||
DestroyClsObj(pCls);*/
|
||||
}
|
||||
//从类中读数据到实例对象
|
||||
bool Hd_AI_algo_jiXing_cam1::readData(std::vector<cv::Mat>&vec_mat,QByteArray &data)
|
||||
bool Hd_AI_algo_jiXing_T::readData(std::vector<cv::Mat>&vec_mat,QByteArray &data)
|
||||
{
|
||||
detect_mutex->lock();
|
||||
//qDebug() << __FUNCTION__ << " line:" <<__LINE__ << " data len:" << data.length();
|
||||
@@ -553,7 +554,7 @@ bool Hd_AI_algo_jiXing_cam1::readData(std::vector<cv::Mat>&vec_mat,QByteArray &d
|
||||
return true;
|
||||
}
|
||||
//从类中读数据到实例对象
|
||||
bool Hd_AI_algo_jiXing_cam1::encodeResultValue(QVector<std::string> append_value,QByteArray &resultByte)
|
||||
bool Hd_AI_algo_jiXing_T::encodeResultValue(QVector<std::string> append_value,QByteArray &resultByte)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -590,9 +591,10 @@ bool Hd_AI_algo_jiXing_cam1::encodeResultValue(QVector<std::string> append_value
|
||||
int myallNum[4] = {0};
|
||||
QMap<int, cv::Mat> lastImgMap;
|
||||
//接收数据
|
||||
bool Hd_AI_algo_jiXing_cam1::writeData(std::vector<cv::Mat>& vec_mat, QByteArray &data)
|
||||
bool Hd_AI_algo_jiXing_T::writeData(std::vector<cv::Mat>& vec_mat, QByteArray &data)
|
||||
{
|
||||
//qDebug() << __FUNCTION__ << "line:"<< __LINE__ << " data len" << data.length() ;
|
||||
|
||||
if(vec_mat.empty())
|
||||
{
|
||||
qCritical() << "[Error] " << " vec_mat is null !";
|
||||
@@ -603,7 +605,7 @@ bool Hd_AI_algo_jiXing_cam1::writeData(std::vector<cv::Mat>& vec_mat, QByteArray
|
||||
for (int i = 0; i < vec_mat.size(); i++)
|
||||
HikMat.enqueue(vec_mat[i]);
|
||||
|
||||
QByteArray source_savePath;
|
||||
QByteArray source_savePath;
|
||||
QByteArray sData = data;
|
||||
QByteArrayList splitDataList = sData.split(',');
|
||||
if (splitDataList.size() >= 4)
|
||||
@@ -707,7 +709,7 @@ bool Hd_AI_algo_jiXing_cam1::writeData(std::vector<cv::Mat>& vec_mat, QByteArray
|
||||
|
||||
//传入initParas函数,格式为:相机key+参数名1#参数值1#参数值2+参数名2#参数值1...
|
||||
//初始化
|
||||
bool Hd_AI_algo_jiXing_cam1::initParas(QByteArray& byte)
|
||||
bool Hd_AI_algo_jiXing_T::initParas(QByteArray& byte)
|
||||
{
|
||||
initValueList = byte.split('+');
|
||||
QStringList valueiList;
|
||||
@@ -745,9 +747,9 @@ bool Hd_AI_algo_jiXing_cam1::initParas(QByteArray& byte)
|
||||
|
||||
//线程检测:结果类完成时,使用
|
||||
//ifThreadRunDetect = true;
|
||||
//QFuture<void> future1 = QtConcurrent::run(this, &Hd_AI_algo_jiXing_cam1::detectThread);
|
||||
//QFuture<void> future1 = QtConcurrent::run(this, &Hd_AI_algo_jiXing_T::detectThread);
|
||||
//运行存图线程
|
||||
QFuture<void> future2 = QtConcurrent::run(this, &Hd_AI_algo_jiXing_cam1::saveImgThread);
|
||||
QFuture<void> future2 = QtConcurrent::run(this, &Hd_AI_algo_jiXing_T::saveImgThread);
|
||||
//detectThread();
|
||||
|
||||
//原图总文件夹
|
||||
@@ -758,10 +760,11 @@ bool Hd_AI_algo_jiXing_cam1::initParas(QByteArray& byte)
|
||||
isDirExist(resImgSaveDir);
|
||||
|
||||
//qDebug() << "[INFO] " << " StationNo:" << StationNo << " img2D:" << img2D ;
|
||||
myEngine.SetProcedurePath("./halconFilesAlgo/JX_ColorInspection.hdpl"); //设置hdvp路径
|
||||
return true;
|
||||
}
|
||||
// 解码
|
||||
bool Hd_AI_algo_jiXing_cam1::decodeAiValue(QByteArray byte,QVector<QByteArray> &vec_ret)
|
||||
bool Hd_AI_algo_jiXing_T::decodeAiValue(QByteArray byte,QVector<QByteArray> &vec_ret)
|
||||
{
|
||||
if(byte.isEmpty())
|
||||
{
|
||||
@@ -796,10 +799,12 @@ bool Hd_AI_algo_jiXing_cam1::decodeAiValue(QByteArray byte,QVector<QByteArray> &
|
||||
return true;
|
||||
}
|
||||
//---------检测流程,根据实际项目修改---------
|
||||
bool Hd_AI_algo_jiXing_cam1::AIdetect(cv::Mat img2D, QString mysaveKey, QString myDir2D, QString mySN, QString myDataTime)
|
||||
bool Hd_AI_algo_jiXing_T::AIdetect(cv::Mat img2D, QString mysaveKey, QString myDir2D, QString mySN, QString myDataTime)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
//存图数据加保存队列,加锁,避免数据访问冲突
|
||||
saveStruct_mutex->lock();
|
||||
saveImgStruct mySave;
|
||||
@@ -827,44 +832,69 @@ bool Hd_AI_algo_jiXing_cam1::AIdetect(cv::Mat img2D, QString mysaveKey, QString
|
||||
// csvName.close();
|
||||
//}
|
||||
//sourceImg = cv::imread("D:\\mySN15-46-33-86.bmp",-1);
|
||||
//sourceImg = cv::imread("D:/1/2024-10-17-14-07-07-225-DA4203326_0-errSN.bmp", -1);
|
||||
//sourceImg = cv::imread("F:/CCD/TEST/20241024-091531.bmp", -1);
|
||||
cv::Mat Paint;
|
||||
sourceImg.copyTo(Paint);
|
||||
if(Paint.channels() == 1)
|
||||
cvtColor(Paint, Paint, COLOR_GRAY2BGR);
|
||||
|
||||
|
||||
HObject ho_Image;
|
||||
HTuple hv_out_Value;
|
||||
HTuple hv_Make;
|
||||
MatToHObject(sourceImg, &ho_Image);
|
||||
HDevEngineCpp::HDevProcedure proc("JX_Inspection");
|
||||
HDevProcedureCall procedureCall(proc);
|
||||
|
||||
_628_2_Polarity_detection(ho_Image, &hv_out_Value, &hv_Make);
|
||||
if(hv_Make.D() == 1)
|
||||
HObject in_Image;
|
||||
HTuple out_Flag, out_ColorType;
|
||||
MatToHObject(sourceImg, &in_Image);
|
||||
|
||||
procedureCall.SetInputIconicParamObject("in_Image", in_Image);//
|
||||
procedureCall.Execute();//执行halcon程序
|
||||
|
||||
out_Flag = procedureCall.GetOutputCtrlParamTuple("out_Flag");//获取输出图像类变量
|
||||
out_ColorType = procedureCall.GetOutputCtrlParamTuple("out_ColorType");//获取输出控制类变量
|
||||
|
||||
if(out_Flag.D() == 1)
|
||||
{
|
||||
int res1 = hv_out_Value[0].D();
|
||||
int res2 = hv_out_Value[1].D();
|
||||
if(res1 == 1)
|
||||
putTextHusky(Paint, utf8_to_gbk("铝").c_str(), cv::Point(0, 0), cv::Scalar(0, 255, 0), 500);
|
||||
else
|
||||
putTextHusky(Paint, utf8_to_gbk("铜").c_str(), cv::Point(0, 0), cv::Scalar(0, 255, 0), 500);
|
||||
|
||||
if (res2 == 1)
|
||||
putTextHusky(Paint, utf8_to_gbk("铝").c_str(), cv::Point(0, Paint.rows - 500), cv::Scalar(0, 255, 0), 500);
|
||||
else
|
||||
putTextHusky(Paint, utf8_to_gbk("铜").c_str(), cv::Point(0, Paint.rows - 500), cv::Scalar(0, 255, 0), 500);
|
||||
|
||||
//CAM1 为铜OK,CAM2为铝OK
|
||||
if (res1 == 1 && res2 == 1)//铝
|
||||
int res1 = out_ColorType[0].D();
|
||||
int res2 = out_ColorType[1].D();
|
||||
//1铜 2铝+
|
||||
switch (res1)
|
||||
{
|
||||
putTextHusky(Paint, utf8_to_gbk("NG").c_str(), cv::Point(Paint.cols - 800, 0), cv::Scalar(0, 0, 255), 500);
|
||||
algoResult = 0;
|
||||
case 0:
|
||||
putTextHusky(Paint, utf8_to_gbk("异常").c_str(), cv::Point(0, 0), cv::Scalar(0, 0, 255), 300);
|
||||
break;
|
||||
case 1:
|
||||
putTextHusky(Paint, utf8_to_gbk("铜").c_str(), cv::Point(0, 0), cv::Scalar(0, 255, 0), 500);
|
||||
break;
|
||||
case 2:
|
||||
putTextHusky(Paint, utf8_to_gbk("铝").c_str(), cv::Point(0, 0), cv::Scalar(0, 255, 0), 500);
|
||||
break;
|
||||
}
|
||||
else
|
||||
|
||||
switch (res1)
|
||||
{
|
||||
case 0:
|
||||
putTextHusky(Paint, utf8_to_gbk("异常").c_str(), cv::Point(0, Paint.rows - 500), cv::Scalar(0, 0, 255), 300);
|
||||
break;
|
||||
case 1:
|
||||
putTextHusky(Paint, utf8_to_gbk("铜").c_str(), cv::Point(0, Paint.rows - 500), cv::Scalar(0, 255, 0), 500);
|
||||
break;
|
||||
case 2:
|
||||
putTextHusky(Paint, utf8_to_gbk("铝").c_str(), cv::Point(0, Paint.rows - 500), cv::Scalar(0, 255, 0), 500);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
//T 为铜OK,L为铝OK
|
||||
if (res1 == 1 && res2 == 1)//铜ok
|
||||
{
|
||||
putTextHusky(Paint, utf8_to_gbk("OK").c_str(), cv::Point(Paint.cols - 800, 0), cv::Scalar(0, 255, 0), 500);
|
||||
algoResult = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
putTextHusky(Paint, utf8_to_gbk("NG").c_str(), cv::Point(Paint.cols - 800, 0), cv::Scalar(0, 0, 255), 500);
|
||||
algoResult = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
putTextHusky(Paint, utf8_to_gbk("算法错误").c_str(), cv::Point(0, 50), cv::Scalar(0, 255, 0), 500);
|
||||
@@ -967,7 +997,7 @@ bool Hd_AI_algo_jiXing_cam1::AIdetect(cv::Mat img2D, QString mysaveKey, QString
|
||||
}
|
||||
|
||||
//存图加入队列
|
||||
void Hd_AI_algo_jiXing_cam1::addImgToSaveStruct(cv::Mat img2D, QString mysaveKey, QString savePath)
|
||||
void Hd_AI_algo_jiXing_T::addImgToSaveStruct(cv::Mat img2D, QString mysaveKey, QString savePath)
|
||||
{
|
||||
//存图数据加保存队列,加锁,避免数据访问冲突
|
||||
saveStruct_mutex->lock();
|
||||
@@ -979,7 +1009,7 @@ void Hd_AI_algo_jiXing_cam1::addImgToSaveStruct(cv::Mat img2D, QString mysaveKey
|
||||
saveStruct.enqueue(mySave);
|
||||
saveStruct_mutex->unlock();
|
||||
}
|
||||
void Hd_AI_algo_jiXing_cam1::saveImgThread()
|
||||
void Hd_AI_algo_jiXing_T::saveImgThread()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
@@ -1009,7 +1039,7 @@ void Hd_AI_algo_jiXing_cam1::saveImgThread()
|
||||
}
|
||||
}
|
||||
//检测线程,应当只运行一次
|
||||
void Hd_AI_algo_jiXing_cam1::detectThread()
|
||||
void Hd_AI_algo_jiXing_T::detectThread()
|
||||
{
|
||||
do
|
||||
{
|
||||
@@ -1065,7 +1095,7 @@ void Hd_AI_algo_jiXing_cam1::detectThread()
|
||||
}
|
||||
|
||||
static int runNum = 0;
|
||||
bool Hd_AI_algo_jiXing_cam1::run()
|
||||
bool Hd_AI_algo_jiXing_T::run()
|
||||
{
|
||||
clock_t start, stop;
|
||||
start = clock();
|
||||
@@ -1080,7 +1110,7 @@ bool Hd_AI_algo_jiXing_cam1::run()
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Hd_AI_algo_jiXing_cam1::checkStatus()
|
||||
bool Hd_AI_algo_jiXing_T::checkStatus()
|
||||
{
|
||||
status = moduleStatus;
|
||||
return status;
|
||||
+11
-8
@@ -1,5 +1,5 @@
|
||||
#ifndef Hd_AI_algo_jiXing_cam1_H
|
||||
#define Hd_AI_algo_jiXing_cam1_H
|
||||
#ifndef Hd_AI_algo_jiXing_T_H
|
||||
#define Hd_AI_algo_jiXing_T_H
|
||||
|
||||
#include <assert.h>
|
||||
#include <atlstr.h>
|
||||
@@ -21,7 +21,9 @@
|
||||
#include "base.h"
|
||||
#include "handleAI.h"
|
||||
#pragma execution_character_set("utf-8")
|
||||
#include <HDevEngineCpp.h>
|
||||
|
||||
using namespace HDevEngineCpp;
|
||||
|
||||
|
||||
//保存图片的结构体
|
||||
@@ -32,14 +34,14 @@ struct saveImgStruct;
|
||||
// std::string savePath; //保存路径
|
||||
//};
|
||||
|
||||
//初始化流程(一次):Hd_AI_algo_jiXing_cam1,initParas
|
||||
//初始化流程(一次):Hd_AI_algo_jiXing_T,initParas
|
||||
//运行流程:writeData主线程把数据传过来,run,readData此线程把数据传主线程
|
||||
class Hd_AI_algo_jiXing_cam1 :public Base
|
||||
class Hd_AI_algo_jiXing_T :public Base
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Hd_AI_algo_jiXing_cam1();
|
||||
virtual ~Hd_AI_algo_jiXing_cam1();
|
||||
Hd_AI_algo_jiXing_T();
|
||||
virtual ~Hd_AI_algo_jiXing_T();
|
||||
//从类中读数据到实例对象
|
||||
bool readData(std::vector<cv::Mat>&,QByteArray&);
|
||||
//实例对象把数据写入到类
|
||||
@@ -56,6 +58,7 @@ public:
|
||||
bool AIdetect(cv::Mat img2D, QString saveKey, QString Dir2D, QString SN, QString DataTime);
|
||||
|
||||
private:
|
||||
HDevEngine myEngine;
|
||||
int allNum = 0;
|
||||
int StationNo=0;
|
||||
int img2D = 2;
|
||||
@@ -101,8 +104,8 @@ private:
|
||||
|
||||
extern "C"
|
||||
{
|
||||
Q_DECL_EXPORT Hd_AI_algo_jiXing_cam1 * create();
|
||||
Q_DECL_EXPORT void destory(Hd_AI_algo_jiXing_cam1 * ptr);
|
||||
Q_DECL_EXPORT Hd_AI_algo_jiXing_T * create();
|
||||
Q_DECL_EXPORT void destory(Hd_AI_algo_jiXing_T * ptr);
|
||||
}
|
||||
//保存图片的结构体
|
||||
struct saveImgStruct
|
||||
Reference in New Issue
Block a user