2017-06-15 26 views
0

我有Python程序,使用Opencv庫。我想在Windows操作系統上運行這個程序,而不需要安裝python本身。所以,我做了一些研究並發現了Py2exe,但是我有問題使用它。 這裏是我的Python代碼:Py2exe元組索引出pf範圍問題當創建exe文件

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命令或只是簡單的運行設置。 py我收到以下錯誤

Traceback (most recent call last): File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\Program1\setup.py", line 10, in zipfile = None, File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\distutils_buildexe.py", line 188, in run self._run() File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\distutils_buildexe.py", line 267, in _run builder.analyze() File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\runtime.py", line 158, in analyze self.mf.import_package(modname[:-2]) File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 92, in import_package self.import_hook(name) File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 120, in import_hook module = self._gcd_import(name) File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 274, in _gcd_import return self._find_and_load(name) File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 357, in _find_and_load self._scan_code(module.code, module) File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 388, in _scan_code for what, args in self._scan_opcodes(code): File "C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py", line 417, in _scan_opcodes yield "store", (names[oparg],) IndexError: tuple index out of range

+0

你有沒有試過pyinstaller? – Stack

+0

是的,我已經試過了。或者有問題! –

+0

這工作,只是檢查。 'pyinstaller question.py --hidden-import = cv2' – Stack

回答

0

嘗試添加該行:

import numpy 

位於腳本的頂部。 numpy的依賴有時候沒有被檢測到

相關問題