【发布时间】:2015-02-25 07:45:16
【问题描述】:
当我使用多处理模块(Windows 上的 Python 2.7)中的队列代替 Queue.Queue 时,我的程序没有完全关闭。
最终我想使用 multiprocessing.Process 处理 imgbuffer 中的帧,然后使用第二个队列拉回显示数据。这还不起作用 - 似乎 Process.start() 什么也没做 - 但由于我在调试多处理代码时遇到了麻烦,我想我会削减我拥有的最简单的代码,看看是否有人有关于下一步尝试的想法。
import sys
from PySide import QtGui, QtCore
import cv2
import time, datetime
import multiprocessing
import Queue #needed separately for the Empty exception
def imageOpenCv2ToQImage(cv_img):
height, width, bytesPerComponent = cv_img.shape
bytesPerLine = bytesPerComponent * width;
cv2.cvtColor(cv_img, cv2.cv.CV_BGR2RGB, cv_img)
return QtGui.QImage(cv_img.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
class VideoWidget(QtGui.QLabel):
def __init__(self):
super(VideoWidget, self).__init__()
self.imgbuffer = multiprocessing.Queue()
############################################
# hangs on quit using multiprocessing.Queue
# works fine when I use Queue.Queue
#########################################
self.camera = cv2.VideoCapture(1) # camera ID depends on system: 0, 1, etc
self.setGeometry(100, 100, 640, 480)
self.setScaledContents(True)
target_FPS = 30.0
self.camera_timer = QtCore.QTimer()
self.camera_timer.timeout.connect(self.on_camera_timer)
self.camera_timer.start(1000.0/target_FPS)
target_FPS = 40.0
self.repaint_timer = QtCore.QTimer()
self.repaint_timer.timeout.connect(self.on_repaint_timer)
self.repaint_timer.start(1000.0/target_FPS)
def shutdown(self):
self.camera.release()
def on_camera_timer(self):
hello, cv_img = self.camera.read()
tstamp = datetime.datetime.now()
self.imgbuffer.put((tstamp, cv_img))
def on_repaint_timer(self):
try:
tstamp, cv_img = self.imgbuffer.get(False)
pixmap = QtGui.QPixmap.fromImage(imageOpenCv2ToQImage(cv_img))
self.setPixmap(pixmap)
except Queue.Empty:
pass
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
widget = VideoWidget()
app.aboutToQuit.connect(widget.shutdown)
widget.show()
sys.exit(app.exec_())
【问题讨论】:
标签: python opencv pyqt multiprocessing pyside