增加系统资源监控并输出为表格,相机补黑图修改为下次触发时补黑图。
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include <QDebug>
|
||||
QQueue<std::vector<cv::Mat>> queuePic;
|
||||
QMutex* m_mutex;
|
||||
bool triggerIng = false;
|
||||
bool recivingImage = false,frameStart = false;
|
||||
int lastFrameId = -1 ;
|
||||
long long frameStampCountStart = 0, frameStampCountEnd = 0;
|
||||
@@ -300,12 +301,19 @@ Hd_3DCameraLMI_module::Hd_3DCameraLMI_module()
|
||||
{
|
||||
m_mutex = new QMutex();
|
||||
}
|
||||
|
||||
Hd_3DCameraLMI_module::~Hd_3DCameraLMI_module()
|
||||
{
|
||||
ExceptionCB(-1, NULL);
|
||||
}
|
||||
|
||||
void Hd_3DCameraLMI_module::genBlackImg(std::vector<cv::Mat>& vecPic)
|
||||
{
|
||||
cv::Mat blank_image1(1000, 1000, CV_8UC1, cv::Scalar(0, 0, 0));
|
||||
cv::Mat blank_image2(1000, 1000, CV_8UC1, cv::Scalar(0, 0, 0));
|
||||
vector<cv::Mat> dstvec;
|
||||
dstvec.push_back(blank_image1);
|
||||
dstvec.push_back(blank_image2);
|
||||
vecPic.assign(dstvec.begin(), dstvec.end());
|
||||
}
|
||||
bool Hd_3DCameraLMI_module::sendData(std::vector<cv::Mat>& vecPic, QByteArray &data )
|
||||
{
|
||||
|
||||
@@ -320,12 +328,7 @@ bool Hd_3DCameraLMI_module::sendData(std::vector<cv::Mat>& vecPic, QByteArray &d
|
||||
if (queuePic.empty())
|
||||
{
|
||||
//qDebug() << " [Error] " << " srcImage is null";
|
||||
cv::Mat blank_image1(100, 100, CV_8UC1, cv::Scalar(0, 0, 0));
|
||||
cv::Mat blank_image2(100, 100, CV_8UC1, cv::Scalar(0, 0, 0));
|
||||
vector<cv::Mat> dstvec;
|
||||
dstvec.push_back(blank_image1);
|
||||
dstvec.push_back(blank_image2);
|
||||
vecPic.assign(dstvec.begin(), dstvec.end());
|
||||
genBlackImg(vecPic);
|
||||
m_mutex->unlock();
|
||||
return false;
|
||||
}
|
||||
@@ -348,7 +351,6 @@ bool Hd_3DCameraLMI_module::reciveData(std::vector<cv::Mat> &, QByteArray &qbyte
|
||||
|
||||
bool Hd_3DCameraLMI_module::initParas(QByteArray &)
|
||||
{
|
||||
//return true;
|
||||
bool flag = InitLmi(id); //3D初始化
|
||||
m_status = flag ? OK : UnInit;
|
||||
if (!flag)
|
||||
@@ -378,20 +380,58 @@ bool Hd_3DCameraLMI_module::run()
|
||||
//qDebug() << "[info]" << __FUNCTION__ << " line:" << __LINE__ << " run start" << id;
|
||||
try
|
||||
{
|
||||
int timeOut = 0;
|
||||
queuePic.clear();
|
||||
while (timeOut < 500)
|
||||
//方法1 超时机制
|
||||
// queuePic.clear();
|
||||
//int timeOut = 0;
|
||||
//while (timeOut < 500)//ms
|
||||
//{
|
||||
// m_mutex->lock();
|
||||
// if (queuePic.size() > 0)
|
||||
// {
|
||||
// result = true;
|
||||
// m_mutex->unlock();
|
||||
// return true;
|
||||
// }
|
||||
// m_mutex->unlock();
|
||||
// timeOut ++;
|
||||
// QThread::msleep(1);//修改了睡眠1ms
|
||||
//}
|
||||
|
||||
//方法2 在下一次触发时强制给上一次触发补黑图
|
||||
m_mutex->lock();
|
||||
if (triggerIng == true)//仍然在处理上一次触发
|
||||
{
|
||||
std::vector<cv::Mat>imgs;
|
||||
genBlackImg(imgs);
|
||||
queuePic.enqueue(imgs);
|
||||
m_mutex->unlock();
|
||||
int cnt = 0;
|
||||
while (queuePic.size() > 0 || cnt < 100)
|
||||
{
|
||||
QThread::msleep(1);
|
||||
cnt++;
|
||||
if (cnt > 10)
|
||||
qDebug() << "insert black img 2 long";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
triggerIng = true;
|
||||
m_mutex->unlock();
|
||||
}
|
||||
//强制给上一次触发补黑图 结束
|
||||
while (true)
|
||||
{
|
||||
m_mutex->lock();
|
||||
if (queuePic.size() > 0)
|
||||
{
|
||||
result = true;
|
||||
result = true;
|
||||
triggerIng = false;
|
||||
m_mutex->unlock();
|
||||
return true;
|
||||
}
|
||||
m_mutex->unlock();
|
||||
timeOut += 10;
|
||||
QThread::msleep(10);
|
||||
QThread::msleep(1);
|
||||
}
|
||||
}
|
||||
catch (QString ev)
|
||||
|
||||
@@ -75,6 +75,7 @@ class Hd_3DCameraLMI_module:public Base
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
void genBlackImg(std::vector<cv::Mat>& vecPic);
|
||||
Hd_3DCameraLMI_module();
|
||||
virtual ~Hd_3DCameraLMI_module();
|
||||
bool sendData(std::vector<cv::Mat>& vecPic,QByteArray&data);
|
||||
|
||||
@@ -47,7 +47,6 @@ void camera::run()
|
||||
{
|
||||
mutex.lock();
|
||||
std::shared_ptr <detData>data = requestQueue.dequeue();
|
||||
trigger();
|
||||
data->oriImgMutex.lock();
|
||||
const unsigned long long timeCode = data->timeCode;
|
||||
while(1)
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <qdatetime.h>
|
||||
#include <back/threadPoolManager.h>
|
||||
#include <serviceModule.hpp>
|
||||
#include <future>
|
||||
//硬触发要求在触发相机前给出触发信号
|
||||
class backEndManager;
|
||||
class camera : public QThread
|
||||
@@ -38,6 +39,8 @@ public:
|
||||
void captureImage(std::shared_ptr <detData> dataIn)
|
||||
{
|
||||
requestQueue.enqueue(dataIn);
|
||||
std::thread([this]() {trigger();}).detach();
|
||||
|
||||
}
|
||||
|
||||
//相机操作
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
#include <string>
|
||||
#include <Windows.h>
|
||||
#include <ios>
|
||||
#include <qthread.h>
|
||||
#include <fileManager.h>
|
||||
class hardwareMonitor :public QThread
|
||||
{
|
||||
public:
|
||||
int outputCnt =10;
|
||||
struct CPUUsage {
|
||||
DWORD_PTR coreId;
|
||||
double usage;
|
||||
};
|
||||
struct oneTimeData
|
||||
{
|
||||
QString time;
|
||||
std::vector<CPUUsage>cpuState;
|
||||
std::vector<double>memState;
|
||||
};
|
||||
std::vector<CPUUsage> getCPUUsagePerCore() {
|
||||
std::vector<CPUUsage> coreUsages;
|
||||
int numCores = sysInfo.dwNumberOfProcessors;
|
||||
std::vector<FILETIME> idleTimes(numCores), kernelTimes(numCores), userTimes(numCores);
|
||||
std::vector<FILETIME> prevIdleTimes(numCores), prevKernelTimes(numCores), prevUserTimes(numCores);
|
||||
|
||||
// 初始获取
|
||||
GetSystemTimes(&prevIdleTimes[0], &prevKernelTimes[0], &prevUserTimes[0]);
|
||||
|
||||
Sleep(100);
|
||||
|
||||
// 获取再次的 CPU 时间
|
||||
GetSystemTimes(&idleTimes[0], &kernelTimes[0], &userTimes[0]);
|
||||
|
||||
for (int i = 0; i < numCores; ++i) {
|
||||
ULARGE_INTEGER idleTime, kernelTime, userTime;
|
||||
ULARGE_INTEGER prevIdleTime, prevKernelTime, prevUserTime;
|
||||
|
||||
idleTime.LowPart = idleTimes[i].dwLowDateTime;
|
||||
idleTime.HighPart = idleTimes[i].dwHighDateTime;
|
||||
kernelTime.LowPart = kernelTimes[i].dwLowDateTime;
|
||||
kernelTime.HighPart = kernelTimes[i].dwHighDateTime;
|
||||
userTime.LowPart = userTimes[i].dwLowDateTime;
|
||||
userTime.HighPart = userTimes[i].dwHighDateTime;
|
||||
|
||||
prevIdleTime.LowPart = prevIdleTimes[i].dwLowDateTime;
|
||||
prevIdleTime.HighPart = prevIdleTimes[i].dwHighDateTime;
|
||||
prevKernelTime.LowPart = prevKernelTimes[i].dwLowDateTime;
|
||||
prevKernelTime.HighPart = prevKernelTimes[i].dwHighDateTime;
|
||||
prevUserTime.LowPart = prevUserTimes[i].dwLowDateTime;
|
||||
prevUserTime.HighPart = prevUserTimes[i].dwHighDateTime;
|
||||
|
||||
ULONGLONG totalIdle = idleTime.QuadPart - prevIdleTime.QuadPart;
|
||||
ULONGLONG totalKernel = kernelTime.QuadPart - prevKernelTime.QuadPart;
|
||||
ULONGLONG totalUser = userTime.QuadPart - prevUserTime.QuadPart;
|
||||
|
||||
ULONGLONG total = totalKernel + totalUser;
|
||||
|
||||
CPUUsage usage;
|
||||
usage.coreId = i;
|
||||
usage.usage = (total > 0) ? (static_cast<double>(total - totalIdle) / total) * 100.0 : 0.0;
|
||||
coreUsages.push_back(usage);
|
||||
}
|
||||
|
||||
return coreUsages;
|
||||
}
|
||||
std::vector<double> getMemState()
|
||||
{
|
||||
MEMORYSTATUSEX memInfo;
|
||||
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
|
||||
if (GlobalMemoryStatusEx(&memInfo)) {
|
||||
// 获取总物理内存和已用内存
|
||||
DWORDLONG totalPhysical = memInfo.ullTotalPhys;
|
||||
DWORDLONG usedPhysical = memInfo.ullTotalPhys - memInfo.ullAvailPhys;
|
||||
|
||||
// 获取总虚拟内存和已用虚拟内存
|
||||
DWORDLONG totalVirtual = memInfo.ullTotalVirtual;
|
||||
DWORDLONG usedVirtual = memInfo.ullTotalVirtual - memInfo.ullAvailVirtual;
|
||||
|
||||
// 计算内存占用率
|
||||
double physicalMemoryUsage = (static_cast<double>(usedPhysical) / totalPhysical) * 100.0;
|
||||
double virtualMemoryUsage = (static_cast<double>(usedVirtual) / totalVirtual) * 100.0;
|
||||
return { physicalMemoryUsage,virtualMemoryUsage };
|
||||
}
|
||||
else
|
||||
return { 0,0 };
|
||||
}
|
||||
hardwareMonitor()
|
||||
{
|
||||
this->start();
|
||||
}
|
||||
~hardwareMonitor()
|
||||
{
|
||||
runFlag = false;
|
||||
}
|
||||
void writeCsv(std::vector<oneTimeData>&State) {
|
||||
if (State.size() == 0)
|
||||
{
|
||||
std::cerr << "hw数据为空! " << std::endl;
|
||||
return;
|
||||
}
|
||||
fileManager::createDirectoryIfNotExists("./Log");
|
||||
QString fileName("./Log/" + QDate::currentDate().toString("yyyy-MM-dd") + "hwMonitor.csv");
|
||||
QFile file(fileName);
|
||||
bool isEmpty = !file.exists() || file.size() == 0;
|
||||
// 打开文件进行写入
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
|
||||
std::cerr << "无法打开文件进行写入: " << file.errorString().toStdString() << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
QTextStream out(&file);
|
||||
|
||||
// 表头
|
||||
if(isEmpty == true)
|
||||
{
|
||||
QStringList headers;
|
||||
headers.append("getTime");
|
||||
for (int i = 0; i < State[0].cpuState.size(); i++)
|
||||
headers.append("CORE" + QString::number(i));
|
||||
headers.append("pMem,vMem");
|
||||
out << headers.join(",") << "\n";
|
||||
}
|
||||
|
||||
// 循环写入数据
|
||||
for(auto& data: State)
|
||||
{
|
||||
QStringList csvData;
|
||||
csvData.append(data.time);
|
||||
for (auto& cpuData : data.cpuState)
|
||||
csvData.append(QString::number(cpuData.usage));
|
||||
for (auto& memuData : data.memState)
|
||||
csvData.append(QString::number(memuData));
|
||||
out << csvData.join(",") << "\n";
|
||||
}
|
||||
|
||||
// 关闭文件
|
||||
file.close();
|
||||
State.clear();//成功写入后清除
|
||||
}
|
||||
void run()
|
||||
{
|
||||
//初始化
|
||||
GetSystemInfo(&sysInfo);
|
||||
//循环读取
|
||||
std::vector< oneTimeData> datas;
|
||||
while (runFlag)
|
||||
{
|
||||
oneTimeData _oneTimeData;
|
||||
//CPU占用
|
||||
_oneTimeData.cpuState = getCPUUsagePerCore();
|
||||
//内存占用
|
||||
_oneTimeData.memState= getMemState();
|
||||
//记录时间
|
||||
_oneTimeData.time = QTime::currentTime().toString("HH:mm:ss");
|
||||
|
||||
QThread::msleep(10);
|
||||
datas.push_back(_oneTimeData);
|
||||
if (datas.size() > outputCnt)
|
||||
{
|
||||
writeCsv(datas);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
private:
|
||||
SYSTEM_INFO sysInfo;
|
||||
bool runFlag = true;
|
||||
};
|
||||
#pragma once
|
||||
Reference in New Issue
Block a user