我有使用 Opencv 库的 python 程序。我想在不安装 python 本身的情况下在 Windows 操作系统上运行这个程序。所以,我做了一些研究,发现了 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时,我都会收到以下错误
Traceback(最近一次调用最后一次):文件“C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\Program1\setup.py”,第 10 行,zipfile = None,文件“C:\Users \Hadi\AppData\Local\Programs\Python\Python36-32\lib\distutils\core.py”,第 148 行,在 setup dist.run_commands() 文件“C:\Users\Hadi\AppData\Local\Programs\Python \Python36-32\lib\distutils\dist.py”,第 955 行,在 run_commands 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]) 文件“C:\Users\Hadi \AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py”,第 92 行,在 import_package self.import_hook(name) 文件“C:\Users\Hadi\AppData\Local\ Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py”,第 120 行,在 import_hook 模块 = self._gcd_import(name) 文件“C:\Users\Hadi\AppData\Local\Programs\Python \Python36-32\lib\site-packages\py2exe\mf3.py”,第 274 行,在 _gcd_import 返回 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(module.代码,模块)文件“C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py”,第 388 行,在 _scan_code 中,用于什么,在 self.args 中。 _scan_opcodes(代码):文件“C:\Users\Hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\py2exe\mf3.py”,第 417 行,在 _scan_opcodes 中产生“存储”,( names[oparg],) IndexError: 元组索引超出范围