VideoCapture() opencv python pyinstaller not opening

99封情书 提交于 2021-02-19 02:35:27

问题


I am trying to produce an .exe of a simple GUI made with PyQt4 in python 2.7 using pyinstaller to play a video.

Here are the details of my problem:

Simple layout with two buttons, one to load video, other to play video. In my IDE, the video loads and plays perfectly. The video pops up in another window and closes when it is over.

After running pyinstaller on the program, the GUI interface pops up after running the .exe. The open file dialog works correctly, however the video will not play.

In my IDE I can reproduce the error by removing opencv_ffmpeg2412_64.dll from the opencv installation directory. Removing anything else in the directory does not seem to affect the playability of the video within the IDE. So I figure that pyinstaller is not finding opencv_ffmpeg2412_64.dll. I have tried to manually copy it into the dist folder produced by pyinstaller. I have also tried to hook it as well as include it in the specFile. I must be doing something wrong or looking in the wrong place to try and solve this issue.

Any ideas on how I could fix this would be much appreciated. Thanks!

EDIT:

Here is the main part of my code if it can be any help. This is only a test of a larger GUI that I am trying to place in an .exe file. I am using an older version of cv2 because of forwards compatibility problems.

#imports
import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtCore
from PyQt4.QtGui import QFileDialog

import cv2
import numpy

import mainwindow


class MainWindow(QMainWindow, mainwindow.Ui_MainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        self.Load_B.clicked.connect(self.open)
        self.Play_B.clicked.connect(self.play)

    def play(self):


        cap = cv2.VideoCapture(self.video)
        while(True):
            (grabbed, frame) = cap.read()
            if not grabbed:
                break            
            currentframe = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            height, width = currentframe.shape[:2]
            cv2.namedWindow("Preview", cv2.WINDOW_NORMAL) 
            cv2.imshow("Preview",currentframe)  

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break  
        cap.release()
        cv2.destroyAllWindows()  

    def open(self):
        self.video=QFileDialog.getOpenFileName(self,"Video file",filter="Video 
    Files (*.mp4)")


app = QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
form = MainWindow()
form.show()
app.exec_()

回答1:


I just had this same problem and this worked for me:

Use the --add-binary option like BHawk pointed out to copy the dll from the site-packages folder to your dist folder when building the exe.

example:
pyinstaller program.spec --add-binary <PATH_TO_PYTHON>\Lib\site-packages\cv2\opencv_ffmpeg320_64.dll;.




回答2:


Just as Garth5689 said, --add-binary worked well, and I'm on python 3.5. So this works on both python 2 and 3. In my example: -F = one file, -w = no cmd window.

pyinstaller -F -w yourpythonscript.py --add-binary C:\PATH\Python\Python35-32\Lib\site-packages\cv2\opencv_videoio_ffmpeg411.dll;.


来源:https://stackoverflow.com/questions/44415424/videocapture-opencv-python-pyinstaller-not-opening

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!