VideoCapture()opencv python pyinstaller未打开 [英] VideoCapture() opencv python pyinstaller not opening

查看:121
本文介绍了VideoCapture()opencv python pyinstaller未打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pyinstaller 在 python 2.7 中使用 PyQt4 制作一个简单的 GUI 的 .exe 来播放视频.

以下是我的问题的详细信息:

带有两个按钮的简单布局,一个用于加载视频,另一个用于播放视频.在我的 IDE 中,视频加载并完美播放.视频在另一个窗口中弹出并在结束时关闭.

在程序上运行pyinstaller后,运行.exe后弹出GUI界面.打开文件对话框工作正常,但视频无法播放.

在我的 IDE 中,我可以通过从 opencv 安装目录中删除 opencv_ffmpeg2412_64.dll 来重现该错误.删除目录中的任何其他内容似乎不会影响 IDE 中视频的可播放性.所以我认为 pyinstaller 没有找到 opencv_ffmpeg2412_64.dll.我尝试手动将其复制到 pyinstaller 生成的 dist 文件夹中.我还尝试将其挂钩并将其包含在 specFile 中.我一定是做错了什么或找错了地方来尝试解决这个问题.

任何有关我如何解决此问题的想法将不胜感激.谢谢!

如果有帮助的话,这是我代码的主要部分.这只是对我试图放置在 .exe 文件中的更大 GUI 的测试.由于前向兼容性问题,我正在使用旧版本的 cv2.

#imports导入系统从 PyQt4.QtGui 导入 *从 PyQt4.QtCore 导入 *从 PyQt4 导入 QtCore从 PyQt4.QtGui 导入 QFileDialog导入 cv2导入 numpy导入主窗口类 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)定义播放(自我):cap = cv2.VideoCapture(self.video)而(真):(抓取,框架)= cap.read()如果没有抓住:休息当前帧 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)高度,宽度 = currentframe.shape[:2]cv2.namedWindow("预览", cv2.WINDOW_NORMAL)cv2.imshow("预览",当前帧)如果 cv2.waitKey(1) &0xFF == ord('q'):休息cap.release()cv2.destroyAllWindows()定义打开(自我):self.video=QFileDialog.getOpenFileName(self,"视频文件",filter="视频文件 (*.mp4)")app = QApplication(sys.argv)app.aboutToQuit.connect(app.deleteLater)表单 = MainWindow()表单.show()app.exec_()

解决方案

我刚刚遇到了同样的问题,这对我有用:

使用 --add-binary 选项,就像 BHawk 指出的那样,在构建 exe 时将 dll 从 site-packages 文件夹复制到你的 dist 文件夹.>

示例:
pyinstaller program.spec --add-binary \Lib\site-packages\cv2\opencv_ffmpeg320_64.dll;.

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_()

解决方案

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;.

这篇关于VideoCapture()opencv python pyinstaller未打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆