最近在做一个深度学习对图片中对象识别效果的检测工具,其主要功能就是将自己标注的图片与识别结果图片进行对比然后计算识别的准确等参数,并提供原图与结果图片的显示功能。
脚本主要完成识别与计算功能,QT完成数据的整理显示与图片的显示。
我们可以直接用
qt的QProcess调用bash来执行运行脚本的命令
,获取结果数据则有两种方式:
一是在
python脚本中将结果写到文本中,脚本结束后由qt读取
。
二是在
脚本中用print按一定格式将结果输出,在Qt中可用QProcess的信号槽机制获取输出结果
。
void TagDetection::startDetect_slot()
{
p = new QProcess(this);
//当脚本中的结果全部(注意是全部)print完成后进入到OnReadData_slot
connect(p, SIGNAL(readyReadStandardOutput()), this, SLOT(OnReadData_slot()));
//组合命令,带入参
QString labelsStr = QString::fromStdString(m_Vecmodels.at(m_CurrentLabel).types).split(" ").join("/");
QStringList cmdList;
cmdList<
<<:fromstdstring>
<<:fromstdstring>
<
<<:fromstdstring>
<<:fromstdstring>
<<:fromstdstring>
<<:fromstdstring>
<<:fromstdstring>
<<:fromstdstring>
<<:fromstdstring>
<<:fromstdstring>
<<:fromstdstring>
QString cmd = cmdList.join(" ");
qDebug()<
//开始执行
p->start(cmd);
//等待提示框
QApplication::setOverrideCursor(Qt::WaitCursor);//设置鼠标为等待状态
QProgressDialog progress;
progress.setWindowTitle(codecMain->toUnicode("提示"));
progress.setLabelText(codecMain->toUnicode("正在检测中..."));
progress.setCancelButton(false);
progress.setRange(0, 60000);//设置范围
progress.setModal(true);//设置为模态对话框
progress.show();
for (int i = 0; i < 3000 ; i++)
{
QThread::msleep(100);
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
progress.setValue(i);
//进入槽函数则停止等待
if(g_IsDetectedDone)
{
g_IsDetectedDone = false;
break;
}
//用户取消的话则中止
if (progress.wasCanceled())
{
break;
}
}
QApplication::restoreOverrideCursor();
progress.close();
QMessageBox::warning(this, codecMain->toUnicode("提示"), codecMain->toUnicode("检测结束!"));
}
槽函数如下:
//识别完成
void TagDetection::OnReadData_slot()
{
//改变标志位停止等待
g_IsDetectedDone = true;
//获取结果
QString Pyresult = codecMain->toUnicode(p->readAllStandardOutput());
qDebug()<
ui.textEdit->setText(Pyresult);
//关闭bash
p->kill();
p->close();
delete p;
//数据处理...
//或者读取识别结果文本
QFile file("result.txt");
QStringList result;
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
while (!file.atEnd())
{
//按行读取
QByteArray line = file.readLine();
QString str(line);
result.append(str);
qDebug() << str;
}
file.close();
}
//数据处理...
}