使用pyinstaller规范fi制作python pypylon项目的exe文件

2024-05-19 02:10:36 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的python项目中,我使用了baslergigevision以太网摄像头,因为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()

主代码:

^{pr2}$

Traceback (most recent call last): File "site-packages\pypylon\pylon.py", line 42, in swig_import_helper
File "importlib__init__.py", line 126, in import_module File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 953, in _find_and_load_unlocked ModuleNotFoundError: No module named 'pypylon._pylon'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "MainGuiLogic.py", line 18, in File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module exec(bytecode, module.dict) File "PylonVideoReader.py", line 1, in File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module exec(bytecode, module.dict) File "site-packages\pypylon\pylon.py", line 45, in File "site-packages\pypylon\pylon.py", line 44, in swig_import_helper
File "importlib__init__.py", line 126, in import_module File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module module = loader.load_module(fullname) ModuleNotFoundError: No module named 'pypylon._genicam' [4300] Failed to execute script MainGuiLogic [4300] LOADER: OK. [4300] LOADER: Cleaning up Python interpreter.


Tags: toinpyimageimportselflinesite
2条回答

使用pyinstaller(How to make executable file with pyinstaller)生成可执行文件后,您应该从项目中使用的虚拟环境中找到pypylon文件夹,并复制exe文件旁边的pypylon文件夹。在

我可以通过以下步骤来解决这个问题,有些步骤可能不需要,但是到目前为止,这在我的案例中已经起到了作用。我发现this github issue很有帮助,使我走上正轨。在

首先,在我的脚本中过度导入:

import pypylon
from pypylon import pylon
from pypylon import genicam
from pypylon import _genicam
from pypylon import _pylon

我建议您不要在您的示例中导入pypylon的方式,将包简单地称为“py”会使其他开发人员感到困惑。在

接下来,我修改了规范文件的binariespathex和{}。我将所有的pylon dll和pyd文件添加到binaries,将pypylon目录附加到pathex,并将所有的可能性添加到hiddenimports

^{pr2}$

我不确定所有这些都是绝对必要的,但是对于python3.4、pyinstaller3.3和pypylon1.3.1来说,这一切都是可行的。在

祝你好运!在

相关问题 更多 >

    热门问题