多处理:在GUI线程之外使用pixmaps是不安全的pyside

问题描述:

我正在构建一个简单的GUI并尝试学习多处理。在我的GUI中,捕获按钮捕获一个框架并将其放入框架或声明的空间(Qlabel)中的GUI中。我确实阅读了其他文章,但并没有明白如何在我的程序中使用它。多处理:在GUI线程之外使用pixmaps是不安全的pyside

当错误不说,安全GUI使用外,我想

print(multiprocessing.current_process()) 

这表明我的QPixmap在主线程中运行。这是我的计划

import examplegui 
from PySide.QtGui import * 
from PySide.QtCore import * 
import sys 
import multiprocessing 
import numpy as np 
import cv2 
from multiprocessing import Queue 

def image_capture(): 
    print(multiprocessing.current_process()) 
    print("starting img proc") 
    cap = cv2.VideoCapture(0) 
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 641) 
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 351) 
    ret, frame = cap.read() 
    cap.release() 

    if ret == True: 
     cv2.imwrite('frame.png', frame) 
     return frame 

class Maindialog(QMainWindow,examplegui.Ui_MainWindow): 
    pass_arguments = Signal(list) 

    def __init__(self,parent = None): 
     super(Maindialog,self).__init__(parent) 
     self.setupUi(self) 
     self.pool = multiprocessing.Pool(processes=4) 
     self.connect(self.excel_file,SIGNAL("clicked()"),self.apply_connection) 
    # self.pass_arguments.connect 


    def apply_connection(self): 
     print(multiprocessing.current_process()) 
     result = self.pool.apply_async(image_capture,callback=self.show_img) 

    def show_img(self,result): 
     print(multiprocessing.current_process()) 
     # print(result.type) 
     cv2.imshow("img",result) 
     image = QImage(result, result.shape[1], result.shape[0], result.strides[0], QImage.Format_RGB888) 
     self.vidimg.setPixmap(QPixmap.fromImage(image))    
     cv2.waitKey(0) 

    if __name__ == '__main__': 
     app = QApplication(sys.argv) 
     window = Maindialog() 
     window.show() 
     sys.exit(app.exec_()) 

我做了一个cv2.imshow,以确保图像正在运行,但我需要的GUI.When里面我运行PROG我得到的错误图像

QPixmap: It is not safe to use pixmaps outside the GUI thread 
QPixmap: It is not safe to use pixmaps outside the GUI thread 
QPixmap: It is not safe to use pixmaps outside the GUI thread 

有人可以告诉我我要去哪里吗?

enter image description here

+0

答案是否解决了您的问题Raghavendra? – ZF007

+0

@ ZF007不是真的!但我使用线程绕过这个错误,并且对我的问题工作正常。 –

self.pool.apply_async运行在Python线程每个回调(self.show_img),否则会阻塞主线程和不同步!

请注意,您只查看了正在主进程中运行的回调,而不是该进程的主线程。