gpt4 book ai didi

c++ - 如何使用 qt(QProcess) 获取暗检测对象名称?

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:10 25 4
gpt4 key购买 nike

我想使用 QProcess 类从 qt 为单个图像调用暗网检测命令。这是我的主窗口代码:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QProcess * process = new QProcess();
QString temp = "./darknet detect cfg/yolov3.cfg yolov3.weights
iii.jpg";
connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(ReadOut(int, QProcess::ExitStatus)));
process->start(temp);
}

void MainWindow::ReadOut(int i, QProcess::ExitStatus statur)
{
QProcess *p = dynamic_cast<QProcess *>( sender() );
if (p)
ui->textBrowser->append( p->readAllStandardOutput() );
p->close();
delete p;
}

但是因为在检测到 darkent 等待另一个输入后,我无法获得终端的输出,因为进程仍在继续。或者可能存在另一个问题。

我的问题是:

  1. 如何更正我的代码以从进程中获取检测到的对象名称。

  2. 还有其他方法可以实现吗?

最佳答案

使用 QProcess 时最常见的错误是:

  • 程序的相对路径是相对于working Directory也就是可执行文件夹,但很多时候程序不在可执行文件夹中。

  • 程序可以使用与程序相关的资源,因此在这些情况下,工作目录必须是程序所在的目录。

综合以上,解决方案是:

#include <QtWidgets>


class DarknetWidget: public QWidget{
Q_OBJECT
public:
DarknetWidget(QWidget *parent=nullptr): QWidget(parent)
{
QGridLayout *lay = new QGridLayout(this);

lay->addWidget(new QLabel("Darket directory:"), 0, 0);
directory_le = new QLineEdit;
lay->addWidget(directory_le, 1, 0);
QPushButton *directory_button = new QPushButton("Select");
connect(directory_button, &QPushButton::clicked, this, &DarknetWidget::onSelectDirectory);
lay->addWidget(directory_button, 1, 1);

lay->addWidget(new QLabel("Command:"), 2, 0);
command_le = new QLineEdit;
command_le->setText("darknet detect cfg/yolov3.cfg");
lay->addWidget(command_le, 3, 0, 1, 2);

lay->addWidget(new QLabel("weights:"), 4, 0);
weight_le = new QLineEdit;
lay->addWidget(weight_le, 5, 0);
QPushButton *weight_button = new QPushButton("Select");
connect(weight_button, &QPushButton::clicked, this, &DarknetWidget::onSelectWeight);
lay->addWidget(weight_button, 5, 1);


lay->addWidget(new QLabel("Input image:"), 6, 0);
image_le = new QLineEdit;
lay->addWidget(image_le, 7, 0);
QPushButton *image_button = new QPushButton("Select");
connect(image_button, &QPushButton::clicked, this, &DarknetWidget::onSelectImage);
lay->addWidget(image_button, 7, 1);

run_btn = new QPushButton;
run_btn->setText("Run");
connect(run_btn, &QPushButton::clicked, this, &DarknetWidget::runDarket);
lay->addWidget(run_btn, 8, 0, 1, 2);

log_te = new QPlainTextEdit;
log_te->setReadOnly(true);
lay->addWidget(log_te, 9, 0, 1, 2);

resize(640, 480);

process = new QProcess(this);
process->setProcessChannelMode(QProcess::MergedChannels);
connect(process, &QProcess::stateChanged, this, &DarknetWidget::onStateChanged);
connect(process, &QProcess::readyReadStandardOutput, this, &DarknetWidget::onReadyReadStandardOutput);
}
protected:
void closeEvent(QCloseEvent *event){
process->kill();
QWidget::closeEvent(event);
}
private Q_SLOTS:
void onSelectDirectory(){
QString filename = QFileDialog::getExistingDirectory(this, tr("Open Darket directory"));
if(!filename.isEmpty()){
directory_le->setText(filename);
}
}
void onSelectWeight(){
QString filename = QFileDialog::getOpenFileName(this, tr("Open weights path"));
if(!filename.isEmpty()){
weight_le->setText(filename);
}
}
void onSelectImage(){
QString filename = QFileDialog::getOpenFileName(this,
tr("Open image path"),
QDir::currentPath(),
tr("Image Files (*.jpg)"));
if(!filename.isEmpty()){
image_le->setText(filename);
}
}

void runDarket(){
log_te->clear();
process->setWorkingDirectory(directory_le->text());
QString command = QString("%1 %2 %3")
.arg(command_le->text())
.arg(weight_le->text())
.arg(image_le->text());
process->start(command);
}
void onReadyReadStandardOutput(){
log_te->insertPlainText(process->readAllStandardOutput());
log_te->verticalScrollBar()->setValue(log_te->verticalScrollBar()->maximum());
}
void onStateChanged(QProcess::ProcessState state){
if(state == QProcess::Running){
run_btn->setEnabled(false);
}
else if(state == QProcess::NotRunning){
run_btn->setEnabled(true);
}
}
private:
QLineEdit *directory_le;
QLineEdit *command_le;
QLineEdit *weight_le;
QLineEdit *image_le;
QPushButton *run_btn;
QPlainTextEdit *log_te;

QProcess *process;
};

int main(int argc, char* argv[]) {
QApplication app(argc, argv);
DarknetWidget w;
w.show();
return app.exec();
}

#include "main.moc"

enter image description here

关于c++ - 如何使用 qt(QProcess) 获取暗检测对象名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58246917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com