我正在构建一个简单的GUI并尝试学习多处理。在我的GUI中,捕获按钮捕获一个帧并将其放在框架或声明的空间(Qlabel)内的gui中。我确实阅读了SO中的其他帖子,但并不清楚如何在我的程序中使用它。
当错误表示在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中的图像。当我运行编程时我得到错误
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
有人可以告诉我哪里出错了吗?
答案 0 :(得分:0)
self.pool.apply_async
在Python线程中运行每个回调(self.show_img
),否则会阻塞主线程而不是异步!
请注意,您只检查过回调是否在主进程中运行,而不是在进程的主线程中运行。