在我的python项目中,我使用了Basler GigE Vision以太网摄像头,因为pylon不支持python,所以我使用pypylon.pylon wrapper在python中打开它。这是我打开相机的课程,但在制作可执行文件后,我打开它时会出错。 I used spec file to work with pyinstaller.我收到以下错误:
import pypylon.pylon as py
import numpy as np
class PylonVideoReader:
def __init__(self, deviceName=None):
self.deviceName = deviceName
tlfactory = py.TlFactory.GetInstance()
if not self.deviceName is None:
deviceInfoList = tlfactory.EnumerateDevices()
deviceIndex = None
for i in range(len(deviceInfoList)):
if self.deviceName == deviceInfoList[i].GetUserDefinedName():
deviceIndex = i
break
if deviceIndex is None:
print("Device: {} not found please ensure that it is "
"connected".format(self.deviceName))
exit()
else:
# Create new camera
self.camera = py.InstantCamera(tlfactory.CreateDevice(
deviceInfoList[deviceIndex]))
else:
# Create new camera
self.camera = py.InstantCamera(tlfactory.CreateFirstDevice())
# Open camera
self.camera.Open()
# Set max number of frame buffers
self.camera.MaxNumBuffer = 50
# Initialize the image format converter
self.formatConverter = py.ImageFormatConverter()
# Set output pixel format to BGR8 for opencv
self.formatConverter.OutputPixelFormat = py.PixelType_BGR8packed
# Start grabbing process
self.camera.StartGrabbing(py.GrabStrategy_LatestImageOnly)
# Grab a first image to get its size
grabResult = self.camera.RetrieveResult(10000)
# Stop grabbing process
# self.camera.StopGrabbing()
# Get dimensions of image
self.frameWidth = grabResult.GetWidth()
self.frameHeight = grabResult.GetHeight()
def get(self, code):
if code == 3:
return self.frameWidth
elif code == 4:
return self.frameHeight
else:
print("{} is not a known property code".format(code))
def read(self):
# try:
# Start grabing process
# self.camera.StartGrabbing(py.GrabStrategy_LatestImageOnly)
# Grab an image
grabResult = self.camera.RetrieveResult(10000)
# Stop grabing process
# self.camera.StopGrabbing()
# Get dimensions of image
self.frameWidth = grabResult.GetWidth()
self.frameHeight = grabResult.GetHeight()
if grabResult.GrabSucceeded():
# Convert Grab result from YUV422 to BGR8
pylonImage = self.formatConverter.Convert(grabResult)
# Convert pylon image to opencv image
# image = np.frombuffer(bytearray(pylonImage.GetBuffer()), np.uint8)
image = np.asarray(bytearray(pylonImage.GetBuffer()), np.uint8)
image = image.reshape(self.frameHeight, self.frameWidth, 3)
return (True, image)
# except :
return (False, None)
def release(self):
self.camera.StopGrabbing()
self.camera.Close()
主要代码:
if __name__ == "__main__":
cap = PylonVideoReader("Admin1")
cv2.namedWindow("Test1", cv2.WINDOW_NORMAL)
while True:
ret, image = cap.read()
if ret:
cv2.imshow("Test1", image)
if cv2.waitKey(1) % 256 == ord('q'):
break
Traceback(最近一次调用最后一次):文件 “site-packages \ pypylon \ pylon.py”,第42行,在swig_import_helper中 在import_module文件中输入“importlib__init__.py”,第126行 “”,第994行,在_gcd_import文件中 “”,第971行,在_find_and_load文件中 “”,第953行,在_find_and_load_unlocked中 ModuleNotFoundError:没有名为'pypylon._pylon'的模块
在处理上述异常期间,发生了另一个异常:
Traceback(最近一次调用最后一次):文件“MainGuiLogic.py”,第18行, 在文件中 “C:\ programdata \ anaconda3 \ LIB \站点包\ PyInstaller \装载机\ pyimod03_importers.py” 第631行,在exec_module中 exec(字节码,模块。字典)文件“PylonVideoReader.py”,第1行,在文件中 “C:\ programdata \ anaconda3 \ LIB \站点包\ PyInstaller \装载机\ pyimod03_importers.py” 第631行,在exec_module中 exec(字节码,模块。字典)文件“site-packages \ pypylon \ pylon.py”,第45行,在文件中 swig_import_helper中的“site-packages \ pypylon \ pylon.py”,第44行 在import_module文件中输入“importlib__init__.py”,第126行 “C:\ programdata \ anaconda3 \ LIB \站点包\ PyInstaller \装载机\ pyimod03_importers.py” 第714行,在load_module中 module = loader.load_module(fullname)ModuleNotFoundError:没有名为'pypylon._genicam'的模块 [4300]无法执行脚本 MainGuiLogic [4300]装载机:好的。 [4300] LOADER:清理Python 解释
答案 0 :(得分:0)
通过执行以下操作,我可以解决此问题,某些步骤可能不是必需的,但到目前为止,这对我而言已奏效。我发现this github issue很有帮助,并把我放在正确的轨道上。
首先,在脚本中过度导入
:import pypylon
from pypylon import pylon
from pypylon import genicam
from pypylon import _genicam
from pypylon import _pylon
我建议您不要在示例中导入pypylon的方式,简单地将软件包称为“ py”会使其他开发人员感到困惑。
接下来,我修改了我的规范文件的binaries
,pathex
和hiddenimports
。我将所有pylon dll和pyd文件添加到binaries
,将pypylon目录添加到pathex
,并将所有可能性添加到hiddenimports
:
import pypylon
pypylon_dir = pathlib.Path(pypylon.__file__).parent
pypylon_dlls = [(str(dll), '.') for dll in pypylon_dir.glob('*.dll')]
pypylon_pyds = [(str(dll), '.') for dll in pypylon_dir.glob('*.pyd')]
_binaries = list()
_binaries.extend(pypylon_dlls)
_binaries.extend(pypylon_pyds)
_pathex = list()
_pathex.append(str(pypylon_dir))
_hiddenimports = list()
_hiddenimports.extend(['pypylon', 'pypylon.pylon', 'pypylon.genicam', 'pypylon._pylon', 'pypylon._genicam'])
a = Analysis(...
pathex=_pathex,
binaries=_binaries,
...
hiddenimports=_hiddenimports,
我不确定所有这些都是绝对必要的,但是它对我来说适用于Python 3.4,PyInstaller 3.3和pypylon 1.3.1。
祝你好运!
答案 1 :(得分:0)
使用pyinstaller(How to make executable file with pyinstaller)生成可执行文件后,您应该从项目中使用的虚拟环境中找到pypylon文件夹,然后将pypylon文件夹复制到exe文件旁边。