【问题标题】:How can I get darkent detected object names using qt(QProcess)?如何使用 qt(QProcess) 获取深色检测到的对象名称?
【发布时间】:2019-10-05 09:14:11
【问题描述】:

我想使用 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. 还有其他方法可以实现吗?

【问题讨论】:

    标签: c++ qt ubuntu yolo


    【解决方案1】:

    使用 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"
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用QProcess::readyReadStandardOutput() 信号而不是进程完成信号。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-05
        • 2016-09-03
        • 1970-01-01
        • 2011-05-13
        • 1970-01-01
        相关资源
        最近更新 更多