1
votes

In my python project, I used Basler GigE Vision ethernet cameras, because of the pylon doesn't support python then I used pypylon.pylon wrapper to open it in python. This is my class to open the camera, but after making executable file I get errors when opening it. I used spec file to work with pyinstaller.I get this below errors:

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()

main code:

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 (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.

2
Have you found a solution to this issue?Brown
Yes, I found a solution to this problem.Ahad aghapour

2 Answers

0
votes

After making the executable files with pyinstaller (How to make executable file with pyinstaller) you should find the pypylon folder from your virtual environment which used in the project and copy the pypylon folder beside the exe file.

0
votes

I was able to get around this issue by doing the following, some steps may not be necessary, but so far, this has worked in my case. I found this github issue helpful and put me on the right track.

First, excessive importing in my script:

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

I'd advise against the way you imported pypylon in your example, refering to a package simply as "py" will be confusing for other developers.

Next, I modified my spec file's binaries, pathex, and hiddenimports. I added all of the pylon dll and pyd files to binaries, appended the pypylon directory to pathex, and added all of the possibilities to 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,

I'm not sure all of that is strictly necessary, but it's working for me with Python 3.4, PyInstaller 3.3, and pypylon 1.3.1.

Good luck!