创建exe文件

时间:2017-06-15 09:09:20

标签: python opencv py2exe opencv-python

我有使用Opencv库的python程序。我想在Windows操作系统上运行这个程序而不安装python本身。所以,我做了一些研究,发现了Py2exe,但我使用它有问题。 这是我的python代码:

import cv2
import os


# Custom Car Cascade Classifier
car_cascade = cv2.CascadeClassifier("Custom-Car-Cascade.xml")

#Get Test Images Folder From Current Running Path
directory = 'TestImages'
print(directory)
for file in os.listdir(directory):
        if file.endswith(".jpg"):
                #Red Image Into img
                img = cv2.imread(directory + "/" + file,cv2.IMREAD_COLOR)
                #Get Detected Cars Point On Image
                cars = car_cascade.detectMultiScale(img,1.4,10)
                for(x,y,w,h) in cars:
                    #Draw Rectangle 
                    cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
                #Creat Window
                cv2.namedWindow("output", cv2.WINDOW_NORMAL)
                #Resize Image
                resized = cv2.resize(img, (800, 600))
                #Show Image
                cv2.imshow("output",resized)
                k = cv2.waitKey() & 0xff
                if(k == 27):
                    break
cv2.destroyAllWindows()

这是我的 setup.py 文件:

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

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed':     True,'includes':'cv2'}},
    windows = [{'script': "carDetection.py"}],
    #data_files=[("TestImages", "TestImages/*.jpg")],
    zipfile = None,
)

每当我使用python setup.py py2exe 命令或只是运行 setup.py 时,我都会收到以下错误

  

追踪(最近一次通话):     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ Program1 \ setup.py",第10行,in       zipfile =无,     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ distutils \ core.py",第148行,在设置中       dist.run_commands()     在run_commands中的文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ distutils \ dist.py",第955行       self.run_command(CMD)     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ distutils \ dist.py",第974行,在run_command中       cmd_obj.run()     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ py2exe \ distutils_buildexe.py",第188行,运行中       self._run()     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ py2exe \ distutils_buildexe.py",第267行,在_run中       builder.analyze()     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ py2exe \ runtime.py",第158行,在分析中       self.mf.import_package(的modname [: - 2])     在import_package中的文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ py2exe \ mf3.py",第92行       self.import_hook(名称)     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ py2exe \ mf3.py",第120行,在import_hook中       module = self._gcd_import(name)     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ py2exe \ mf3.py",第274行,在_gcd_import中       return self._find_and_load(name)     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ py2exe \ mf3.py",第357行,在_find_and_load中       self._scan_code(模块。代码,模块)     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ py2exe \ mf3.py",第388行,在_scan_code中       为什么,self._scan_opcodes(代码)中的args:     文件" C:\ Users \ Hadi \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ py2exe \ mf3.py",第417行,在_scan_opcodes中       yield" store",(name [oparg],)   IndexError:元组索引超出范围

1 个答案:

答案 0 :(得分:0)

尝试添加以下行:

import numpy

在脚本的顶部。有时没有检测到numpy依赖