From d36012cc9c2e687b09b3896c27b988aa31c153d6 Mon Sep 17 00:00:00 2001
From: zhenye
Date: Mon, 16 Dec 2024 20:50:44 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86debug=E7=9A=84?=
=?UTF-8?q?=E6=89=93=E5=8D=B0=E6=96=B9=E5=BC=8F=EF=BC=8C=E4=BF=AE=E5=A4=8D?=
=?UTF-8?q?=E4=BA=86resize=E5=AF=BC=E8=87=B4=E7=9A=84mask=E9=97=AE?=
=?UTF-8?q?=E9=A2=98=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
camera.cpp | 177 ++++++++++++++++++-----------------------------------
1 file changed, 61 insertions(+), 116 deletions(-)
diff --git a/camera.cpp b/camera.cpp
index 1c3dcaa..48889b3 100644
--- a/camera.cpp
+++ b/camera.cpp
@@ -3,9 +3,10 @@
#include
// Debug Options
-#define GlobalDebug 1
-#define DebugDetection 1
-#define DebugLowerMacCOM 0
+#define GlobalDebug 1 // 全局是否允许打印Debug信息(打印会拖慢处理时间)
+#define DebugDetection 0 // 注意开启这个编译选项会导致图片存储, 处理时间会很慢
+#define DebugDetectionTime 1 // 是否打印处理时间
+#define DebugLowerMacCOM 1 // 是否打印和下位机通讯的相关信息
camera::camera() {}
@@ -13,7 +14,6 @@ QTcpServer* server_to_lowermachine = nullptr;
QTcpSocket* lower_machine = nullptr;
bool volatile is_running = false;
-//编号0 RGB相机 编号1 BV相机
static MIL_ID MilApplication;
static MIL_ID MilSystem;
@@ -46,15 +46,15 @@ ONNXRunner runner;
std::map params;
-int widthBlocks = 20;
-int heightBlocks = 512;
-int sizeThreshold = 20;
-int padLeft = 1;
-int padRight = 1;
-int rowRange = 0;
-int ignoreSide=2;
-int skipLeftCols=10;
-int skipRightCols=10;
+int widthBlocks = 20; // 输出的喷阀通道数
+int heightBlocks = 512; // 输出的Mask高度
+int sizeThreshold = 20; // 转化为喷阀的每块要求像素个数
+int padLeft = 1; // 左侧在结果Mask上补0
+int padRight = 1; // 右侧在结果Mask上补0
+int rowRange = 1; // 结果维持行数(至少为1)
+int ignoreSide = 0; // 左右两侧的忽略的喷阀通道数量
+int skipLeftCols = 0; // 生成mask时跳过左侧的像素数量
+int skipRightCols = 0; // 生成mask时跳过右侧的像素数量
static std::vector> tail_0(0);
static std::vector> tail_1(0);
@@ -117,16 +117,15 @@ bool iniCamera()
break;
}
}
-
-
-
+#if(GlobalDebug)
qDebug()<<"ready";
+#endif
return 1;
}
#if(GlobalDebug && DebugDetection)
-#define SAVE_PATH_resize MIL_TEXT ("C:\\Users\\Pc\\Desktop\\cotton_double2//resize.png")
+// #define SAVE_PATH_resize MIL_TEXT ("C:\\Users\\Pc\\Desktop\\cotton_double2//resize.png")
#define SAVE_PATH_flip MIL_TEXT ("C:\\Users\\Pc\\Desktop\\cotton_double2//flip.png")
#define SAVE_PATH_raw MIL_TEXT ("C:\\Users\\Pc\\Desktop\\cotton_double2//raw.png")
#define SAVE_PATH_result MIL_TEXT ("C:\\Users\\Pc\\Desktop\\cotton_double2//result.png")
@@ -134,6 +133,10 @@ bool iniCamera()
MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
{
+#if(GlobalDebug && DebugDetectionTime)
+ Timer timer_detection_time;
+ timer_detection_time.restart();
+#endif
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER + M_BUFFER_ID, &ModifiedBufferId0);
if(SaveImg_Flag)
@@ -147,35 +150,35 @@ MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
qDebug()<<"回调1成功存储照片:"<> mask_Onnx1 = generateMaskFromImage2(Img_Onnx, widthBlocks, heightBlocks, sizeThreshold);
// timer1.printElapsedTime("onnx sat finished");
- // timer1.restart();
-
auto [mask_1, newTail] = generateMaskWithTail(detection_result0, tail_1, widthBlocks, heightBlocks, sizeThreshold, rowRange, skipLeftCols, skipRightCols);
tail_1 = newTail;
- // timer1.printElapsedTime("tail finished");
-
- // timer1.restart();
-
- // std::vector> mask_Color1 = generateMaskFromImage(detection_result1, widthBlocks, heightBlocks, thresholds);
-
PadColumns(mask_1,padLeft,padRight,0);
std::vector> mask_Total = expandArray(mask_1,64);
- // timer1.printElapsedTime("expand finished");
-
-
- // for(int m = 0 ; m < 512 ; m ++)
- // {
- // for(int n = 0; n < 22 ; n++)
- // {
- // if(mask_Total[m][n] == 1)
- // {
- // for(int k = 0; k < 50; k++)
- // {
- // mask_Total[m + k][n] = 1;
- // }
- // }
- // }
- // }
- // save masks
- vector> mask = generateMask(detection_result0, widthBlocks, heightBlocks, sizeThreshold,skipLeftCols, skipRightCols);
+// save masks
#if(GlobalDebug && DebugDetection)
- VectorToImg(mask,"C:/Users/Pc/Desktop/img/mask" + std::to_string(FuncCount1) + ".bmp");
+ VectorToImg(mask_1,"C:/Users/Pc/Desktop/img/mask" + std::to_string(FuncCount1) + ".bmp");
VectorToImg(mask_1,"C:/Users/Pc/Desktop/img/mask_ignored" + std::to_string(FuncCount1) + ".bmp");
VectorToImg(mask_Total,"C:/Users/Pc/Desktop/img/mask_expended" + std::to_string(FuncCount1) + ".bmp");
#endif
bool result_Low = get_valve_data(mask_Total);
if(!result_Low)
{
- qDebug()<<"下位机发送失败";
+ qWarning()<<"下位机发送失败";
}
+
+#if(GlobalDebug && DebugDetectionTime)
+ timer_detection_time.printElapsedTime("Time of Processing From Get into CallBack to Sent to Lower Mac");
+#endif
return 0;
}
@@ -242,9 +224,6 @@ Timer timer2;
MIL_INT ProcessingFunction1(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
{
FuncCount1++;
-
-
-
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER + M_BUFFER_ID, &ModifiedBufferId1);
if(SaveImg_Flag)
@@ -848,73 +827,38 @@ bool iniLowMac()
{
/*--- server socket to faban ---*/
server_to_lowermachine = new QTcpServer();
- qDebug()<<"1";
+#if(GlobalDebug && DebugLowerMacCOM)
+ qDebug()<<"1/4. Build Tcp Server";
+#endif
server_to_lowermachine->listen(QHostAddress::Any, 13452);
- qDebug()<<"2";
+#if(GlobalDebug && DebugLowerMacCOM)
+ qDebug()<<"2/4. Bind to Port 13452";
+#endif
bool is_timeout;
server_to_lowermachine->waitForNewConnection(5000,&is_timeout);
- qDebug()<<"3";
+#if(GlobalDebug && DebugLowerMacCOM)
+ qDebug()<<"3/4. Try to Establish connect";
+#endif
if(is_timeout)
{
return 0;
}
- qDebug()<<"4";
+#if(GlobalDebug && DebugLowerMacCOM)
+ qDebug()<<"4/4. Connection Established no timeout";
+#endif
lower_machine = server_to_lowermachine->nextPendingConnection();
- qDebug()<<"连接成功";
+#if(GlobalDebug && DebugLowerMacCOM)
+ qDebug()<<"Lower Machine Connection Suecced";
+#endif
if (lower_machine == nullptr || !lower_machine->isWritable()) {
cout << "Error: Lower machine is not available or writable." << endl;
return 0 ;
}
- // // 创建 TCP 服务器对象
- // server_to_lowermachine = new QTcpServer();
-
- // // 启动监听
- // if (!server_to_lowermachine->listen(QHostAddress::Any, 13452)) {
- // qDebug() << "Error: Server failed to start.";
- // return false;
- // }
- // qDebug() << "Server is listening on port 13452.";
-
- // // 等待新连接
- // if (!server_to_lowermachine->waitForNewConnection(5000)) {
- // qDebug() << "Timeout or no connection.";
- // return false;
- // }
-
- // // 获取连接
- // lower_machine = server_to_lowermachine->nextPendingConnection();
- // if (lower_machine) {
- // qDebug() << "New client connected.";
-
- // // 连接数据接收信号
- // QObject::connect(lower_machine, &QTcpSocket::readyRead, []() {
- // QByteArray data = lower_machine->readAll();
- // qDebug() << "Received data:" << data;
-
- // // 向下位机发送响应
- // QByteArray response = "Acknowledged.";
- // lower_machine->write(response);
- // });
-
- // // 连接断开信号
- // QObject::connect(lower_machine, &QTcpSocket::disconnected, []() {
- // qDebug() << "Client disconnected.";
- // lower_machine->deleteLater();
- // });
-
- // return true;
- // } else {
- // qDebug() << "Error: No client connected.";
- // return false;
- // }
-
-
-
// 硬编码参数值
int file_delay = 1530; // 延迟时间(毫秒)
int file_encoder = 12000; // 编码器值
@@ -925,11 +869,6 @@ bool iniLowMac()
int divide_valve = (file_valve != 0) ? 100000000 / file_valve : 0; // 防止除以零的情况
-
-
-
-
-
// 将参数转换为长度为8的字符串,前面补0
QString delay_time = QString("%1").arg(file_delay, 8, 10, QChar('0'));
QString divide_parameter = QString("%1").arg(divide_camera, 8, 10, QChar('0'));
@@ -1232,7 +1171,13 @@ pair>, vector>> applyRowRangeDelay(
const vector>& mask,
const vector>& tail,
int rowRange
- ) {
+ )
+{
+ if(rowRange <= 0)
+ {
+ rowRange = 1;
+ qDebug() << "Maintain Row Range Error, forced to 1!!!!!!!!";
+ }
int outputHeight = (int)mask.size();
int outputWidth = (int)mask[0].size();