代码拉取完成,页面将自动刷新
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>
#include <Python.h>
#include <QPainter>
#include <QPen>
#include <QSemaphore>
bool is_connected = false;
PyObject* pFun = NULL;
extern QSemaphore emptybuff;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
init_python_moudle();
setAttribute(Qt::WA_DeleteOnClose);
init_window();
connect_signals();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init_window()
{
//showFullScreen();
m_correct = new Correct;
ui->tabWidget->findChildren<QTabBar*>().at(0)->hide();
ui->tabWidget->setCurrentIndex(0);
ui->showLabel->setAutoFillBackground(true);
ui->debugLabel->setAutoFillBackground(true);
ui->explosureSpinBox->setRange(15.00, 9999500.00);
ui->explosureSpinBox->setDecimals(2);
ui->explosureSpinBox->setSingleStep(0.01);
ui->gainSpinBox->setRange(0.00, 16.98);
ui->gainSpinBox->setDecimals(2);
ui->gainSpinBox->setSingleStep(0.01);
ui->whiteSpinBox->setRange(1, 4095);
ui->delaySpinBox->setRange(1, 10000);
ui->sendIntervalSpinBox->setRange(1, 100);
ui->tiggerIntervalSpinBox->setRange(1, 1000);
ui->stopButton->setEnabled(false);
ui->CorrectButton->setEnabled(false);
#if 1
m_camera = new Camera;
m_camera->init_camera();
//m_modbus = new ModbusTCP;
//m_modbus->modbus_tcp_init();
//is_connected = true;
server = new QTcpServer();
server->listen(QHostAddress::Any, 8000);
#endif
#if 0
m_camera = new Camera;
while(1)
{
if(!m_camera->init_camera())
{
if(QMessageBox::Yes == QMessageBox::warning(NULL, "Title", QString::fromUtf8("检查相机连接"),QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes))
{
;
}
else
{
close();
}
}
else
{
is_connected = true;
break;
}
}
m_modbus = new ModbusTCP;
while(1)
{
if(!m_modbus->modbus_tcp_init())
{
if(QMessageBox::Yes == QMessageBox::warning(NULL, "Title", QString::fromUtf8("检查PLC连接"),QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes))
{
}
else
{
close();
}
}
break;
}
#endif
}
void MainWindow::connect_signals()
{
connect(ui->startButton, SIGNAL(clicked()), this, SLOT(On_startButton_clicked()));
connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(On_stopButton_clicked()));
connect(ui->debugButton, SIGNAL(clicked()), this, SLOT(On_debugButton_clicked()));
connect(ui->exitButton, SIGNAL(clicked()), this, SLOT(On_exitButton_clicked()));
connect(ui->setParamButton, SIGNAL(clicked()), this, SLOT(On_setParamButton_clicked()));
connect(ui->saveParamButton, SIGNAL(clicked()), this, SLOT(On_saveParamButton_clicked()));
connect(ui->backButton, SIGNAL(clicked()), this, SLOT(On_backButton_clicked()));
connect(ui->getCorrectImageButton, SIGNAL(clicked()), this, SLOT(On_getCorrectImageButton_clicked()));
connect(ui->valvetestButton, SIGNAL(clicked()), this, SLOT(On_valvetestButton_clicked()));
connect(ui->CorrectButton, SIGNAL(clicked()), this, SLOT(On_correctBUtton_clicked()));
connect(ui->setDelayButtton, SIGNAL(clicked()), this, SLOT(On_setDelayButton_clicked()));
connect(ui->confitmDelayButton, SIGNAL(clicked()), this, SLOT(On_confirmDelayButton_clicked()));
connect(ui->setsendIntervalButton, SIGNAL(clicked()), this, SLOT(On_setsendIntervalButton_clicked()));
connect(ui->settiggerIntervaButton, SIGNAL(clicked()), this, SLOT(On_settiggerIntervaButton_clicked()));
connect(server, SIGNAL(newConnection()), this, SLOT(ServerNewConnection()));
}
void MainWindow::On_startButton_clicked()
{
qDebug()<<"thread run";
deal_thread = new QThreadDeal(this);
connect(deal_thread, SIGNAL(send_image(cv::Mat)), this, SLOT(showimage(cv::Mat)),Qt::BlockingQueuedConnection);
connect(deal_thread, SIGNAL(send_res(QVector<bad_candy_box>)), this, SLOT(drawbox(QVector<bad_candy_box>)), Qt::BlockingQueuedConnection);
int n = emptybuff.available();
if(n < 2)
{
emptybuff.release(2 - n);
}
m_camera->set_acquisition_mode();
m_camera->start_capture();
modbus_thread = new QThreadModbus(this);
//connect(modbus_thread, SIGNAL(stop_all_thread()), this, SLOT(deal_camera_offline()));
deal_thread->start();
//modbus_thread->start();
socket->write("startwork",sizeof("startwork"));
ui->debugButton->setEnabled(false);
ui->startButton->setEnabled(false);
ui->stopButton->setEnabled(true);
}
void MainWindow::On_stopButton_clicked()
{
qDebug()<<"thread stop";
disconnect(deal_thread, SIGNAL(send_image(cv::Mat)), this, SLOT(showimage(cv::Mat)));
disconnect(deal_thread, SIGNAL(send_res(QVector<bad_candy_box>)), this, SLOT(drawbox(QVector<bad_candy_box>)));
//disconnect(modbus_thread, SIGNAL(stop_all_thread()), this, SLOT(deal_camera_offline()));
m_camera->stop_capture();
deal_thread->exitThread();
//modbus_thread->exitThread();
ui->debugButton->setEnabled(true);
ui->stopButton->setEnabled(false);
ui->startButton->setEnabled(true);
}
void MainWindow::On_debugButton_clicked()
{
ui->tabWidget->setCurrentIndex(1);
debug_thread = new QThreadDebug;
connect(debug_thread, SIGNAL(send_image_debug(cv::Mat)), this, SLOT(showimage_debug(cv::Mat)),Qt::BlockingQueuedConnection);
connect(this, SIGNAL(correct_signal()), debug_thread, SLOT(get_correct_siganl()));
int n = emptybuff.available();
if(n < 2)
{
emptybuff.release(2 - n);
}
Camera_param param = m_camera->get_param();
ui->explosureSpinBox->setValue(param.exposure_time);
ui->whiteSpinBox->setValue(param.white_balance_ratio);
ui->gainSpinBox->setValue(param.gain);
//m_camera->set_ROI(0, 0, 2448, 2048);
m_camera->set_acquisition_mode_for_debug();
m_camera->start_capture();
debug_thread->start();
}
void MainWindow::On_exitButton_clicked()
{
m_camera->fini_camera();
close();
}
void MainWindow::On_saveParamButton_clicked()
{
m_camera->stop_capture();
usleep(1000);
m_camera->get_param();
if(!m_camera->save_config_file())
{
qDebug()<<"SAVE FAILED";
}
m_camera->start_capture();
}
void MainWindow::On_setParamButton_clicked()
{
Camera_param sets;
sets.exposure_time = ui->explosureSpinBox->text().toFloat();
sets.gain = ui->gainSpinBox->text().toFloat();
sets.white_balance_ratio = ui->whiteSpinBox->text().toInt();
qDebug()<<sets.exposure_time;
m_camera->set_param(sets);
}
void MainWindow::On_backButton_clicked()
{
ui->tabWidget->setCurrentIndex(0);
disconnect(debug_thread, SIGNAL(send_image_debug(cv::Mat)), this, SLOT(showimage_debug(cv::Mat)));
m_camera->stop_capture();
debug_thread->exitThread();
}
void MainWindow::closeEvent (QCloseEvent* e)
{
if(QMessageBox::question(this, "Quit", "Are you sure to quit this application?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
e->accept();
qDebug()<<"ok";
}
else
{
e->ignore();
}
}
bool MainWindow::init_python_moudle()
{
Py_SetPythonHome(L"/home/dk/miniconda3/envs/yolo/");
Py_Initialize();
if(!Py_IsInitialized())
{
qDebug()<<"python init failed";
return false;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('/home/dk/qtProjects/candydetect/yolov5-master-3label/')");
PyObject *pModule = PyImport_ImportModule("detect_candy");
if(!pModule)
{
qDebug()<<"import module failed";
return false;
}
PyObject *pDict = PyModule_GetDict(pModule);
if(!pDict)
{
qDebug()<<"get function failed";
return false;
}
PyObject *pClass = PyDict_GetItemString(pDict, "SugarDetect");
if(!pClass)
{
qDebug()<<"get class failed";
return false;
}
PyObject *pIns = PyInstanceMethod_New(pClass);
if(!pIns)
{
qDebug()<<"get init function failed";
return false;
}
pFun = PyObject_CallObject(pIns, NULL);
if(!pFun)
{
qDebug()<<"instance failed";
return false;
}
PyEval_InitThreads();
PyEval_ReleaseThread(PyThreadState_Get());
qDebug()<<"success";
return true;
}
void MainWindow::showimage(cv::Mat img)
{
cv::cvtColor(img, img, CV_BGRA2RGB);
const unsigned char *pSrc = (const unsigned char*)img.data;
QImage image(pSrc, img.cols, img.rows, img.step, QImage::Format_RGB888);
pix = QPixmap::fromImage(image.scaled(ui->showLabel->width(),ui->showLabel->height(),Qt::KeepAspectRatio));
ui->showLabel->setPixmap(pix);
ui->showLabel->show();
}
void MainWindow::drawbox(QVector<bad_candy_box> bounding_box)
{
QPainter painter(&pix);
QPen pen(QColor(255, 0, 0), 1);
painter.setPen(pen);
QVector<QRect> rects;
int size = bounding_box.size();
for(int i = 0; i < size; i++)
{
int width = (bounding_box[i].pointB_x - bounding_box[i].pointA_x) * (ui->showLabel->width() / 2448.0f);
int height = (bounding_box[i].pointB_y - bounding_box[i].pointA_y) * (ui->showLabel->width() / 2448.0f);
QRect rect(bounding_box[i].pointA_x* (pix.width() / 2448.0f), bounding_box[i].pointA_y* (pix.height() / 1000.0f), width, height);
rects << rect;
}
painter.drawRects(rects);
// char buf[1024];
// static int i = 1;
// sprintf(buf, "./result3/%d.png", i);
// i++;
// pix.save(buf);
ui->showLabel->setPixmap(pix);
}
void MainWindow::showimage_debug(cv::Mat img)
{
cv::cvtColor(img, img, CV_BGRA2RGB);
const unsigned char *pSrc = (const unsigned char*)img.data;
QImage image(pSrc, img.cols, img.rows, img.step, QImage::Format_RGB888);
ui->debugLabel->setPixmap(QPixmap::fromImage(image.scaled(ui->debugLabel->width(),ui->debugLabel->height(),Qt::KeepAspectRatio)));
ui->debugLabel->show();
}
void MainWindow::deal_camera_offline()
{
On_stopButton_clicked();
m_camera->fini_camera();
while(1)
{
if(!m_camera->init_camera())
{
if(QMessageBox::Yes == QMessageBox::warning(NULL, "Title", QString::fromUtf8("检查相机连接"),QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes))
{
;
}
else
{
close();
}
}
else
{
is_connected = true;
break;
}
}
}
void MainWindow::On_getCorrectImageButton_clicked()
{
emit correct_signal();
ui->CorrectButton->setEnabled(true);
}
void MainWindow::On_valvetestButton_clicked()
{
valve_test_thread = new QThreadValveTest;
valve_test_thread->start();
}
void MainWindow::On_correctBUtton_clicked()
{
cv::Mat correct_image = cv::imread("./correct.bmp", 1);
m_correct->get_rgb(correct_image);
m_correct->cal_correction_ratio();
m_correct->is_corrected=true;
}
void MainWindow::ServerNewConnection()
{
socket = server->nextPendingConnection();
qDebug()<<"new connection";
}
void MainWindow::On_setDelayButton_clicked()
{
ui->tabWidget->setCurrentIndex(2);
}
void MainWindow::On_confirmDelayButton_clicked()
{
int delay_time = ui->delaySpinBox->text().toInt();
uint16_t x = (uint16_t)delay_time;
uint8_t high = (x>>8)&0xFF;
uint8_t low = x&0xFF;
uint8_t send_tab[6]={0xAA, 0xBB, high, low, 0xCC, 0xDD};
socket->write((const char*)send_tab, 6);
}
void MainWindow::On_setsendIntervalButton_clicked()
{
int interval = ui->sendIntervalSpinBox->text().toInt();
uint16_t x = (uint16_t)interval;
uint8_t high = (x>>8)&0xFF;
uint8_t low = x&0xFF;
uint8_t send_tab[6]={0xBB, 0xCC, high, low, 0xDD, 0xEE};
socket->write((const char*)send_tab, 6);
}
void MainWindow::On_settiggerIntervaButton_clicked()
{
int interval = ui->tiggerIntervalSpinBox->text().toInt();
uint16_t x = (uint16_t)interval;
uint8_t high = (x>>8)&0xFF;
uint8_t low = x&0xFF;
uint8_t send_tab[6]={0xCC, 0xDD, high, low, 0xEE, 0xFF};
socket->write((const char*)send_tab, 6);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。