当我使用来自多处理模块的队列(WindowsPython2.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_())发布于 2015-03-12 22:55:46
我想出来了..。答案毕竟在文件中:
https://docs.python.org/3/library/multiprocessing.html#pipes-and-queues
特别是:
它们的不同之处在于队列中没有引入Python2.5的task_done()和queue.Queue ()方法。
在我的例子中,解决方案似乎是将此添加到关闭代码中:
self.imgbuffer.cancel_join_thread()https://stackoverflow.com/questions/28713728
复制相似问题