我有3个帖子。 主线程(对于我的GUI) - >工人线程A - >工人线程B
在工作线程B(在我的代码中称为recognThread)中,我试图启动一个进程并将数据写入该进程。该过程应该启动我的python虚拟环境(这部分工作)并在该环境中执行一些命令(这不起作用)。
当我尝试将数据写入该过程时,我得到: 无法从另一个线程启用或禁用套接字通知程序
我认为我有点理解问题是什么,但并不完全,也不知道如何解决我的问题。
这些是我的代码的相关部分:
recognitionClass.cpp:
QProcess recognitionClass::p;
recognitionClass::recognitionClass(QObject *parent) : QObject(parent)
{
startPythonVirtualEnv();
}
void recognitionClass::startPythonVirtualEnv()
{
qRegisterMetaType<QProcess::ProcessError>("QProcess::ProcessError");
params<<"-f"<<"-c"<<"python2"<< "/home/John/Desktop/python.log";
qDebug()<<"parameters: "<<params;
if(p.state()==2)
{qDebug()<<"python virtualenv already running";}
else
{
p.start("script", params);
qDebug()<<"started process";
while(!p.waitForStarted())
{qDebug()<<"waiting for virtualenv to be ready";}
}
}
recognitionClass::person recognitionClass::runRecognitionScript()
{
person persStrObj;
qRegisterMetaType<QProcess::ProcessError>("QProcess::ProcessError");
connect(&p, &QProcess::errorOccurred, qApp, &QApplication::aboutQt );
qint64 successFailWrite;
delay();
qDebug()<<"before p.write()";
successFailWrite = p.write("import imp; foo = imp.load_source('myTest', '/home/John/openface/demos/recognitionClass.py'); from myTest import recognitionClass; myClassObj = recognitionClass(); print myClassObj.recognize('/home/John/Desktop/camera.jpg');");
qDebug()<<"after p.write()";
delay();
qDebug()<<"started process, result write op: "<< successFailWrite;
}
recognitionClass.h:
class recognitionClass : public QObject
{
Q_OBJECT
private:
struct person{
QString persName;
double persCertitude;
};
static QProcess p;
QStringList params;
public:
explicit recognitionClass(QObject *parent = nullptr);
struct person runRecognitionScript();
void startPythonVirtualEnv();
void stopPythonVirtualEnv();
void delay();
signals:
public slots:
double recognizePerson();
};
webcamclass.cpp:
webcamClass::webcamClass(QObject *parent) : QObject(parent)
{
recognizePerson=false;
//setup recognition thread
recognitionThread = new QThread(this);
recognitionClObj = new recognitionClass();
connect( recognitionThread, SIGNAL(started()), recognitionClObj, SLOT(recognizePerson()) );
recognitionClObj->moveToThread(recognitionThread);
}
void webcamClass:: getVideoFrame()
{
static cv::VideoCapture cap(CV_CAP_ANY);
cv::Mat imgFrame;
if( !cap.isOpened() )
{
qDebug()<< "Could not initialize capturing...\n";
}
while(1)
{
cap >> imgFrame;
cv::cvtColor(imgFrame, imgFrame, CV_BGR2RGB);
QImage img;
img = QImage((uchar*)imgFrame.data, imgFrame.cols, imgFrame.rows, QImage::Format_RGB888);
QPixmap pixmap = QPixmap::fromImage(img);
emit gottenVideoFrame(pixmap);
if(recognizePerson==true)
{
//write image to recognition buffer
recognitionThread->start();
if(myGlobalVariable==0)
{
frameForRecognition = imgFrame;
myGlobalVariable=55;
}
}
cv::waitKey(100);
}
}
webcamclass(AKA worker thread A)是一个工作线程,它创建一个实现识别类的新工作线程。 (recognitionThread,AKA工作者线程B) 在RecognitionThread中,我尝试创建一个进程来在我的构造函数中启动我的python虚拟环境。这有效。 错误发生在运行时包含successFailWrite = p.write的行(“import imp ....”);在第37行的recognitionClass.cpp文件中。
我的猜测是我试图编程这是一个非常糟糕的方式,这就是为什么我从一个问题碰到另一个问题。我已经阅读了与类似问题相关的其他帖子,但我无法找到出路。
请有人请提供一些解释吗?
谢谢!