添加检测数据导出为XML,读取XML文件统计平均值的功能输出到产量统计的功能

main
Flamingo 1 year ago
parent aa9e765f26
commit fc3e8de896

@ -6,6 +6,7 @@
#include <PLCDevice.h>
#include <QtCore\qcryptographichash.h>
#include <QtCore\qprocess.h>
#include "exportData.h"
// 主界面基本参数配置文件
#define CONFIGURE_FILE "D:/conf/conf.txt"
@ -66,6 +67,8 @@ ASyncQueue<bool> *g_result_queue[NumberOfSupportedCameras]; //
bool g_debug_mode; //相机调试模式,调试模式必须暂停状态才能打开
SyncQueue<cv::Mat> *g_debug_queue[NumberOfSupportedCameras]; //相机调试模式图像队列
SyncQueue<_UDPSendInfo> *UDP_Info_queue;
SyncQueue<_ExportDataInfo> *export_Data_Info_queue;
ExportDataThread exportDataThread;
bool g_admin_mode; //是否管理员模式
@ -132,6 +135,8 @@ Cigarette::Cigarette(QWidget *parent)
g_save_queue->name = "save queue";
UDP_Info_queue = new SyncQueue<_UDPSendInfo>(Queue_Size);
UDP_Info_queue->name = "UDP Info queue";
export_Data_Info_queue = new SyncQueue<_ExportDataInfo>(Queue_Size);
export_Data_Info_queue->name = "Export Data Info queue";
last_shift = 256;
for(int i=0;i<NumberOfSupportedCameras;i++)
@ -160,7 +165,7 @@ Cigarette::Cigarette(QWidget *parent)
}
g_debug_mode = false;
g_admin_mode = false;
g_admin_mode = true;
g_admin_time = ADMIN_TIME;
g_admin_pswd = read_pswd();
@ -248,6 +253,11 @@ Cigarette::Cigarette(QWidget *parent)
#endif
#ifdef __ExportData
exportDataThread.init();
exportDataThread.start_work();
#endif
m_PLCDevice = new PLCDevice;
PLCDevice::init_plc(m_PLCDevice);
@ -435,6 +445,8 @@ Cigarette::Cigarette(QWidget *parent)
Cigarette::~Cigarette()
{
emit sengMsgToClog("Application exited.");
Exit();
exit(-1);
}
void Cigarette::start_work()
@ -572,6 +584,11 @@ void Cigarette::Exit()
rThread->stop();
delete rThread;
#endif
#ifdef __ExportData
exportDataThread.stop();
// delete export_Data_Info_queue;
#endif
qDebug() << "Close thread ";
if(m_PLCDevice) {

@ -20,6 +20,7 @@
//#define DOUBLE_FEED_BACK //一次ng两次反馈ng信号
#endif
//#define identify_Hik_YSXID//识别海康相机YSXID
#define __ExportData // 输出检测数据到XML文档
#define Queue_Size 15
#define Unit_Queue_Size Queue_Size*3

@ -0,0 +1,178 @@
#include "exportData.h"
#include "QtCore\qfile.h"
#include "QtCore\qtextstream.h"
#include<string>
extern SyncQueue<_ExportDataInfo>* export_Data_Info_queue;
ExportDataThread::ExportDataThread(QObject* parent) : QThread(parent)
{
char xmlPath[256];
for (int index = 0; index < NumberOfSupportedCameras; index++)
{
XMLError error;
pDocument[index] = new XMLDocument();
memset(xmlPath, 0, 256);
sprintf(xmlPath, EXPORTDATA_FILE, index);
error = pDocument[index]->LoadFile(xmlPath);
if (error != XML_SUCCESS)
{
XMLDeclaration* declaration = pDocument[index]->NewDeclaration();
pDocument[index]->InsertFirstChild(declaration);
XMLElement* root = pDocument[0]->NewElement("Root");
pDocument[0]->InsertEndChild(root);
pDocument[index]->SaveFile(xmlPath);
}
/*
_ExportDataInfo data;
data.cameraId = index;
data.cameraTotal = 3;
data.timeCost = "3ms";
insertXMLNode(xmlPath, data);
*/
}
}
void ExportDataThread::init()
{
b_quit = false;
}
void ExportDataThread::start_work()
{
start(HighestPriority);
}
void ExportDataThread::stop()
{
b_quit = true;
_ExportDataInfo data;
export_Data_Info_queue->put(data);
}
bool _ExportDataInfo::getAverageData(map<string, float> &averageData, int index)
{
XMLDocument doc;
char xmlPath[256];
XMLError error;
map<string, float> data;
memset(xmlPath, 0, 256);
sprintf(xmlPath, EXPORTDATA_FILE, index);
error = doc.LoadFile(xmlPath);
if (error != XML_SUCCESS)
if (doc.LoadFile(xmlPath) != 0)
{
cout << "load xml file failed" << endl;
return false;
}
XMLElement* root = doc.RootElement();
XMLElement* userNode = root->FirstChildElement("Camera");
data["CameraId"] = index;
data["IsNG"] = 0;
data["IsJdExist"] = 0;
data["Total"] = 0;
while (userNode != NULL)
{
if (atoi(userNode->Attribute("Id")) == index) {
data["IsNG"]++;
data["IsJdExist"]++;
data["Total"]++;
XMLElement* IsNgNode = userNode->FirstChildElement("IsNG");
data["IsNg"] = strcmp(IsNgNode->GetText(), "TRUE");
XMLElement* IsJdExistNode = userNode->FirstChildElement("IsJdExist");
if (strcmp(IsJdExistNode->GetText(), "TRUE") == 0)
data["IsJdExist"]++;
XMLElement* TimeCostNode = userNode->FirstChildElement("TimeCost");
data["TimeCost"] += stof(TimeCostNode->GetText());
}
userNode = userNode->NextSiblingElement();//ÏÂÒ»¸öÐֵܽڵã
}
if (data["Total"] == 0)
return false;
data["TimeCost"] = data["TimeCost"] / data["Total"];
averageData = data;
return true;
}
int ExportDataThread::insertXMLNode(const char* xmlPath, _ExportDataInfo& data)
{
XMLElement* root = pDocument[data.cameraId]->RootElement();
if (root == NULL) {
root = pDocument[data.cameraId]->NewElement("Root");
pDocument[data.cameraId]->InsertEndChild(root);
pDocument[data.cameraId]->SaveFile(xmlPath);
}
XMLElement* CameraNode = pDocument[data.cameraId]->NewElement("Camera");
CameraNode->SetAttribute("Id", data.cameraId);
CameraNode->SetAttribute("Count ", data.cameraTotal);
root->InsertEndChild(CameraNode);
XMLElement* IsNG = pDocument[data.cameraId]->NewElement("IsNG");
XMLText* IsNGText = pDocument[data.cameraId]->NewText(data.isNg ? "TRUE" : "FALSE");
IsNG->InsertEndChild(IsNGText);
CameraNode->InsertEndChild(IsNG);
XMLElement* timeCost = pDocument[data.cameraId]->NewElement("TimeCost");
timeCost->InsertEndChild(pDocument[data.cameraId]->NewText(data.timeCost.c_str()));
CameraNode->InsertEndChild(timeCost);
XMLElement* isJdExist = pDocument[data.cameraId]->NewElement("IsJdExist");
isJdExist->InsertEndChild(pDocument[data.cameraId]->NewText(data.isJdExist[0] ? "TRUE" : "FALSE"));
CameraNode->InsertEndChild(isJdExist);
// XMLElement* jdInterval = pDocument[data.cameraId]->NewElement("JdInterval");
// jdInterval->InsertEndChild(pDocument[data.cameraId]->NewText((const char*)&data.jdInterval));
// CameraNode->InsertEndChild(jdInterval);
// XMLElement* jdInterval = pDocument[data.cameraId]->NewElement("jdPointsLocation");
// jdInterval->InsertEndChild(pDocument[data.cameraId]->NewText(data.getPoint(0).c_str()));
// CameraNode->InsertEndChild(jdInterval);
return pDocument[data.cameraId]->SaveFile(xmlPath);
}
void ExportDataThread::run()
{
while (!b_quit) {
_ExportDataInfo element;
export_Data_Info_queue->take(element);
if (element.cameraId != -1) {
char buf[256];
memset(buf, 0, 256);
sprintf(buf, EXPORTDATA_FILE, element.cameraId);
insertXMLNode(buf, element);
}
}
for (int index = 0; index < NumberOfSupportedCameras; index++) {
pDocument[index]->~XMLDocument();
}
}
void ExportDataThread::check_save_dir(std::string dir_path)
{
bool b_find = false;
for (int i = 0; i < vec_save_dirs.size(); i++)
{
if (dir_path == vec_save_dirs[i])
{
b_find = true;
}
}
if (!b_find)
{
QString dir_str = QString::fromStdString(dir_path);
QDir dir;
if (!dir.exists(dir_str))
{
dir.mkpath(dir_str);
}
vec_save_dirs.push_back(dir_path);
}
}

@ -0,0 +1,86 @@
#include <QDialog>
#include "ui_output_statistic.h"
#include <vector>
#include <qdir.h>
#include <iostream>
#include <SyncQueue.h>
#include <qthread.h>
#include "basecamera.h"
#include "tinyxml2.h"
#include <map>
#define EXPORTDATA_FILE "D:/conf/camera%d_data_info.xml"
using namespace tinyxml2;
class _ExportDataInfo
{
struct JdPoint //´´½¨µãµÄÊý¾Ý
{
string x;
string y;
};
public:
int cameraId;
int shotCounts;
int jd[20];
string timeCost;
float jdInterval;
bool isNg;
bool isJdExist[20];
long long cameraTotal;
std::vector<JdPoint> jdPointsLocation;
float jdSize[20];
bool getAverageData(map<string, float>& averageData, int index);
string getPoint(int index) {
string point = "(";
point += jdPointsLocation.at(index).x + ',' + jdPointsLocation.at(index).y + ')';
return point;
}
_ExportDataInfo()
{
cameraId = -1;
jdInterval = 0.0;
isNg = false;
for (int i = 0; i < 20; i++)isJdExist[i] = false;
for (int i = 0; i < 20; i++)jd[i] = 0;
timeCost = "0ms";
for (int i = 0; i < 20; i++)jdSize[i] = 0.0;
shotCounts = 0;
}
};
extern SyncQueue<_ExportDataInfo>* export_Data_Info_queue;
class ExportDataThread : public QThread
{
Q_OBJECT
signals:
public:
ExportDataThread(QObject* parent = 0);
~ExportDataThread()
{
stop();
//export_Data_Info_queue->put(data);
quit();
wait();
}
void init();
void start_work();
void stop();
int insertXMLNode(const char* xmlPath, _ExportDataInfo& data);
protected:
void run();
void check_save_dir(std::string dir_path);
public:
bool b_quit;
std::vector<std::string> vec_save_dirs;
tinyxml2::XMLDocument *pDocument[NumberOfSupportedCameras];
};

@ -1,6 +1,8 @@
#include "output_statistic.h"
#include "QtCore\qfile.h"
#include "QtCore\qtextstream.h"
#include "exportData.h"
#include "common.h"
output_statistic::output_statistic(QDialog *parent)
: QDialog(parent)
@ -15,6 +17,36 @@ output_statistic::~output_statistic()
void output_statistic::recMsgFromDialogSetup()
{
#ifdef __ExportData
_ExportDataInfo data;
map<string, float> averageData[NumberOfSupportedCameras];
ui.tabWidget->setTabVisible(1, false);
ui.tabWidget->setTabVisible(2, false);
ui.tabWidget->setTabVisible(3, false);
ui.tabWidget->setTabVisible(4, false);
ui.tabWidget->setTabVisible(5, false);
ui.tabWidget->setTabVisible(6, false);
ui.tabWidget->setTabVisible(7, false);
ui.tabWidget->setTabText(0, "Statistical data");
for (int index = 0; index < NumberOfSupportedCameras; index++) {
data.getAverageData(averageData[index], index);
QString text;
text.append(" CameraId: ");
text.append(std::to_string((int)averageData[index]["CameraId"]).c_str());
text.append("\n IsNG: ");
text.append(std::to_string((int)averageData[index]["IsNG"]).c_str());
text.append("\n IsJdExist: ");
text.append(std::to_string((int)averageData[index]["IsJdExist"]).c_str());
text.append("\n TimeCost: ");
text.append(std::to_string(averageData[index]["TimeCost"]).c_str());
text.append("\n*************************************************\n");
ui.textBrowser_1->insertPlainText(text);
}
#else
QFile file("../conf/camera0_statistic.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
@ -71,5 +103,6 @@ void output_statistic::recMsgFromDialogSetup()
ui.textBrowser_8->setText(in.readAll());
}
file.close();
#endif
this->show();
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -5,6 +5,8 @@
#include "baslercamera.h"
#include "threadSend.h"
#include <PLCDevice.h>
#include "exportData.h"
extern AlgJd alg_jd[NumberOfSupportedCameras]; //检测胶点的AI算法
extern SysConf g_sys_conf; //系统配置参数
@ -16,6 +18,7 @@ extern bool isNeddRotate[NumberOfSupportedCameras];
extern SyncQueue<std::pair<std::string, cv::Mat> >* g_save_queue; //图片保存队列
extern SyncQueue<std::pair<int, cv::Mat> >* g_image_queue[NumberOfSupportedCameras]; //int表示一个目标拍了几张
extern SyncQueue<_UDPSendInfo> *UDP_Info_queue;
extern SyncQueue<_ExportDataInfo>* export_Data_Info_queue;
extern PLCDevice* m_PLCDevice;
WorkThread::~WorkThread()
{
@ -74,6 +77,11 @@ void WorkThread::run()
_UDPSendInfo UDPSendInfo;
UDPSendInfo.FrameID = info_frame;
UDPSendInfo.index = local_camera_number;
_ExportDataInfo exportDataInfo;
exportDataInfo.cameraId = local_camera_number;
if (!image.data)
{
continue; //图像为空,跳过
@ -84,6 +92,7 @@ void WorkThread::run()
}
if (local_SysConf.shoot[local_camera_number] == unit_count)
{
exportDataInfo.shotCounts = unit_count;
std::vector<cv::Mat> vec_in;
int w = image.cols;
int h = image.rows / unit_count;
@ -117,6 +126,7 @@ void WorkThread::run()
int time_process = ts_start.msecsTo(ts_jd);
emit display_timecost(local_camera_number, time_process);
UDPSendInfo.timecost = QString::number(time_process);
exportDataInfo.timeCost = QString::number(time_process).toStdString();
cv::Mat image1;
cv::Mat image2;
@ -125,6 +135,8 @@ void WorkThread::run()
for(int index=0;index<unit_count;index++)
{
jd_no += QString::number(vec_results[index].size())+",";
exportDataInfo.jd[index] = vec_results[index].size();
exportDataInfo.isJdExist[index] = vec_results[index].size() > 0;
}
jd_no.chop(1);
emit display_jd_no(local_camera_number, jd_no);
@ -164,6 +176,7 @@ void WorkThread::run()
IsNG|=CheckSelectRects(image1,vec_results,2,local_DisplayLabelConf,1);
}
result_index++;
exportDataInfo.isNg = IsNG;
if (!IsNG)
{
@ -198,6 +211,7 @@ void WorkThread::run()
}
emit display_check_total(local_camera_number, ++frame_total);
exportDataInfo.cameraTotal = frame_total;
emit notify(local_camera_number, 0,image1);
if(unit_count>=3)
@ -214,6 +228,9 @@ void WorkThread::run()
}
#ifdef __UDPSend
UDP_Info_queue->put(UDPSendInfo);
#endif
#ifdef __ExportData
export_Data_Info_queue->put(exportDataInfo);
#endif
}
}

Loading…
Cancel
Save