无法通过 python 独立可执行文件在另一台计算机上的 PyQt4 gui 中渲染通过 OpenCV 加载的视频。不给出错误什么也不做
Can't render videos loaded through OpenCV in PyQt4 gui on another computer through a python standalone executable. giving no errors does nothing
这里是 py2exe 安装脚本:
from setuptools import setup
import py2exe
from glob import glob
import numpy
import sys
import cv2
SETUP_DICT = {
'windows': [{
'script': 'C:\Users\Codemen1\Documents\Python Scripts\video.py',
}],
'zipfile': 'lib/library.zip',
'data_files': (
('', glob(r'C:\Windows\SYSTEM32\msvcp100.dll')),
('', glob(r'C:\Windows\SYSTEM32\msvcr100.dll')),
),
'options': {
'py2exe': {
'bundle_files': 3,
#'bundle_files': 1,
#'packages': 'cv2',
'includes': ['sip', 'PyQt4.QtCore','numpy','sys','cv2'],
},
}
}
setup(**SETUP_DICT)
这个 setup.py 脚本可以完美地处理图像和其他功能,如果我使它们可执行的话。
这是我用来显示视频的 pyqt 代码
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("Control panel"))
MainWindow.setWindowTitle(_translate("Control panel", "Control panel", None))
#MainWindow.resize(1407, 722)
MainWindow.setFixedSize(800, 722)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayoutWidget = QtGui.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 20, 160, 661))
self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.videoFrame = QtGui.QLabel(self.centralwidget)
self.videoFrame.setGeometry(QtCore.QRect(100, 40, 591, 611))
self.videoFrame.setText(_fromUtf8(""))
self.videoFrame.setObjectName(_fromUtf8("videoFrame"))
MainWindow.setCentralWidget(self.centralwidget)
self.videoFrame = QtGui.QLabel(self.centralwidget)
self.videoFrame.setGeometry(QtCore.QRect(190, 40, 591, 611))
self.videoFrame.setText(_fromUtf8(""))
self.videoFrame.setObjectName(_fromUtf8("videoFrame"))
MainWindow.setCentralWidget(self.centralwidget)
class Video():
def __init__(self,capture):
self.capture = capture
self.currentFrame=np.array([])
def captureNextFrame1(self):
"""
capture frame and reverse RBG BGR and return opencv image
"""
ret, readFrame=self.capture.read()
if(ret==True):
self.currentFrame=cv2.cvtColor(readFrame,cv2.COLOR_BGR2RGB)
def convertFrame(self):
""" converts frame to format suitable for QtGui """
try:
height,width=self.currentFrame.shape[:2]
bytesPerLine = 3 * width
img=QtGui.QImage(self.currentFrame.data,width,height,bytesPerLine,QtGui.QImage.Format_RGB888)
img=QtGui.QPixmap.fromImage(img)
self.previousFrame = self.currentFrame
return img
except:
return None
class Gui(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.video1 = Video(cv2.VideoCapture("C:\Users\Public\OpenCVproject\IMG_1491.mov"))
self._timer = QtCore.QTimer(self)
self._timer.timeout.connect(self.play)
self._timer.start(27)
self.update()
def play(self):
try:
self.video1.captureNextFrame1()
self.ui.videoFrame.setPixmap(self.video1.convertFrame())
self.ui.videoFrame.setScaledContents(True)
except TypeError:
print "No frame"
def main():
app = QtGui.QApplication(sys.argv)
ex = Gui()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
到目前为止,代码没有任何问题。完美运行。但是,如果我将其设为独立的 exe 文件,则视频不会在其他计算机上呈现。完美展现在我身上。
我解决了这个问题。 py2exe 无法获取所有 ffmpeg、highgui 和其他 dll 和 pyd 文件。所以必须手动将所有这些文件导入我的可执行文件 & 它解决了问题。谢谢你。
这里是 py2exe 安装脚本:
from setuptools import setup
import py2exe
from glob import glob
import numpy
import sys
import cv2
SETUP_DICT = {
'windows': [{
'script': 'C:\Users\Codemen1\Documents\Python Scripts\video.py',
}],
'zipfile': 'lib/library.zip',
'data_files': (
('', glob(r'C:\Windows\SYSTEM32\msvcp100.dll')),
('', glob(r'C:\Windows\SYSTEM32\msvcr100.dll')),
),
'options': {
'py2exe': {
'bundle_files': 3,
#'bundle_files': 1,
#'packages': 'cv2',
'includes': ['sip', 'PyQt4.QtCore','numpy','sys','cv2'],
},
}
}
setup(**SETUP_DICT)
这个 setup.py 脚本可以完美地处理图像和其他功能,如果我使它们可执行的话。
这是我用来显示视频的 pyqt 代码
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("Control panel"))
MainWindow.setWindowTitle(_translate("Control panel", "Control panel", None))
#MainWindow.resize(1407, 722)
MainWindow.setFixedSize(800, 722)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayoutWidget = QtGui.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 20, 160, 661))
self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.videoFrame = QtGui.QLabel(self.centralwidget)
self.videoFrame.setGeometry(QtCore.QRect(100, 40, 591, 611))
self.videoFrame.setText(_fromUtf8(""))
self.videoFrame.setObjectName(_fromUtf8("videoFrame"))
MainWindow.setCentralWidget(self.centralwidget)
self.videoFrame = QtGui.QLabel(self.centralwidget)
self.videoFrame.setGeometry(QtCore.QRect(190, 40, 591, 611))
self.videoFrame.setText(_fromUtf8(""))
self.videoFrame.setObjectName(_fromUtf8("videoFrame"))
MainWindow.setCentralWidget(self.centralwidget)
class Video():
def __init__(self,capture):
self.capture = capture
self.currentFrame=np.array([])
def captureNextFrame1(self):
"""
capture frame and reverse RBG BGR and return opencv image
"""
ret, readFrame=self.capture.read()
if(ret==True):
self.currentFrame=cv2.cvtColor(readFrame,cv2.COLOR_BGR2RGB)
def convertFrame(self):
""" converts frame to format suitable for QtGui """
try:
height,width=self.currentFrame.shape[:2]
bytesPerLine = 3 * width
img=QtGui.QImage(self.currentFrame.data,width,height,bytesPerLine,QtGui.QImage.Format_RGB888)
img=QtGui.QPixmap.fromImage(img)
self.previousFrame = self.currentFrame
return img
except:
return None
class Gui(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.video1 = Video(cv2.VideoCapture("C:\Users\Public\OpenCVproject\IMG_1491.mov"))
self._timer = QtCore.QTimer(self)
self._timer.timeout.connect(self.play)
self._timer.start(27)
self.update()
def play(self):
try:
self.video1.captureNextFrame1()
self.ui.videoFrame.setPixmap(self.video1.convertFrame())
self.ui.videoFrame.setScaledContents(True)
except TypeError:
print "No frame"
def main():
app = QtGui.QApplication(sys.argv)
ex = Gui()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
到目前为止,代码没有任何问题。完美运行。但是,如果我将其设为独立的 exe 文件,则视频不会在其他计算机上呈现。完美展现在我身上。
我解决了这个问题。 py2exe 无法获取所有 ffmpeg、highgui 和其他 dll 和 pyd 文件。所以必须手动将所有这些文件导入我的可执行文件 & 它解决了问题。谢谢你。