linux下调用python脚本,Linux下QT调用Python脚本的解决方案,Qt,python,一种,解决办法

最近在做一个深度学习对图片中对象识别效果的检测工具,其主要功能就是将自己标注的图片与识别结果图片进行对比然后计算识别的准确等参数,并提供原图与结果图片的显示功能。

脚本主要完成识别与计算功能,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();

}

//数据处理...

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值