python - 如何构建包含cv模块的exe文件

标签 python opencv camera py2exe pyinstaller

我正在编写一个简单的安全摄像头程序。

我使用该代码访问相机:

import cv
camera = cv.CaptureFromCAM(0)

我试过 .py 文件。有效。但是,当我编译并运行 exe 文件时,我无法访问相机。程序没有反应。在 .py 文件上,我可以从标题为“视频源”的窗口中选择相机。

我认为这是关于访问权限的问题。

之后,我通过visual studio 2010的“调试”功能打开了exe文件。我收到警告:

Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000. 

顺便说一句,我从“输出”选项卡得到了这些输出:

'security_camera.exe': Loaded '[file_path(C:\Users\........)]', Binary was not built with debug information.
'security_camera.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\user32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\gdi32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\lpk.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\usp10.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57\msvcr90.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\imm32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\msctf.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\nvinit.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\advapi32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\sechost.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\rpcrt4.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\guard32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\version.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\psapi.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\fltLib.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\shell32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\shlwapi.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\winmm.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\ole32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.7601.18201_none_ec80f00e8593ece5\comctl32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\oleaut32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\cryptsp.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\rsaenh.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\cryptbase.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\ws2_32.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\nsi.dll', Cannot find or open the PDB file
'security_camera.exe': Loaded 'C:\Windows\System32\comdlg32.dll', Cannot find or open the PDB file
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
First-chance exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x02691fcb in security_camera.exe: 0xC0000005: Access violation writing location 0x00000000.
The program '[8604] security_camera.exe: Native' has exited with code -1073741819 (0xc0000005).

顺便说一下,当我打开 exe 文件(程序)时,它会在 3-4 秒内关闭。

我该如何解决?谢谢。

注意: 我的 python 版本:2.7.3

我使用的是 Windows 7(32 位)

编辑:

我是通过py2exe构建的exe文件。

我的 setup.py 是:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True, 'includes': 'numpy'}},
    windows = [{'script': "security_camera.py"}],
    zipfile = None,
)

最佳答案

代替 py2exe,尝试 pyinstaller

这里有一些简单的说明:

1) 安装pyinstaller。如果你有 pip,就这么简单:

pip install pyinstaller

2) 创建你的可执行文件:

pyinstaller --onefile <your python script>

关于python - 如何构建包含cv模块的exe文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30016496/

相关文章:

javascript - 从浏览器访问相机

ubuntu - 找不到模块 '@ionic-native/Camera/ngx'

python - multiprocessing.pools.map_async 的回调不起作用?

python - 使用随机森林对文本文档进行分类

qt - .exe无法通过QtCreator和Qt 4.8.1的外部运行。关于dll的问题

c++ - 具有非 Qt 函数的 Qt 资源文件

python - Django - 将 Base64 图像从模板保存到静态

Python 脚本依赖于另一个 .exe。是否可以将两者合并为一个 .exe?

c++ - 在图像中的一些点周围绘制边界

iOS10,相机权限 - 黑屏 "camera app"