OpenCV in a Python script

I'd like to use OpenCV in a KLayout Python script.

I'm on Windows and I'm using KLayout 0.26.8.

I put the OpenCV package (with some others) in the %KLAYOUT%\python directory.

The OpenCV package has been retrieved with a "pip install opencv-contrib-python" command using a Python 3.6 instance.

When I try an import OpenCV from the console line of the macro development window (with a "import cv2" command), I get this error: "No module named 'cv2.cv2' (Class ModuleNotFoundError)".

I tried many approaches for several long hours, re-reading especially the KLayout documentation regarding environment variables, search paths, packages ... without success.

Any idea?

Many thanks for your help.

Comments

  • @numero8 You can't just add any Python package to KLayout on Windows. This has been a topic in this forum a couple of times. See for example here for an explanation: https://www.klayout.de/forum/discussion/comment/6430#Comment_6430

    You can use KLayout database engine inside an external Python instance (using PyPI's klayout package). Or you can build KLayout yourself against your favourite Python distribution. I'm building against MSYS2, for which there are some packages, but only a few on PyPI.

    Linux is easy as KLayout shares Python with the system installation.

    Matthias

  • Thank you very much for these explanations, Matthias (and for all your work, indeed!). That's clear for me now.

    So, as I do need to work on a Windows platform, I have at least three alternatives at the moment:
    1) rebuild OpenCV in the same way than KLayout: probably rather long and hazardous;

    2) rebuild KLayout (preferably using Visual Studio 2017) against the Python distribution that my project is currently using (standard one, V3.6.6): seems feasible (while this is not completely clear at the moment for me, especially on the way to refer to my distribution and to integrate it on the rebuild process: many hidden details may emerge and impede significantly the process);

    3) use the KLayout package available on Pypi: seems great! However, a first look at the package reveals that it includes the "db" package but not the "lay" package and its LayoutView, CellView ... classes that my current Python scripts are using to load existing GDSII's, zoom on subparts of them, extract some images...

    So, in my case, and unless there is a way to integrate the "lay" package in the same way than the "klayout" package, the best approach would probably be the second one.

    Is this your feeling too?

    Is there a way to load an existing GDSII using the "klayout" package only?

    Many thanks for your help.

  • Hi,

    PyPI is just a subset and does not include the UI part. The reason is simple: it comes without Qt, which is good because that creates another axis in the dependency matrix and would render an unmanageable mess. Plus avoids potential licensing issues.

    Ideally, KLayout would be built against Anaconda/MSVC and share it's wealth of packages.But I definitely don't want to extend the range of systems I am providing support for. Each system flavour I support eats away more of my time. And I don't want to make all users having to install Anaconda first, so my choice is the standalone version. I can try to include OpenCV in the next release, but I can't promise. This depends whether this package is available for MSYS2.

    There is another solution maybe: Someone may be able to set up and maintain an Azure job that runs the build against Anaconda. There are some templates in "azure-pipelines.yml" in the sources. I'd appreciate such a service to society. But I for myself hate to do this DevOps stuff. Gone through it too many times and out of energy.

    Matthias

  • Thank you for your feedback, Mathias.

    My project finally decided to investigate a fourth option, which is to use the "subprocess" module to call an independent Python script in a separate process with all the modules we need. This requires to have an already installed Python interpreter, but this also opens a lot of opportunities for a minimal investment (we spent a week trying an autonomous and reproducible development environment to rebuild KLayout as it is today and prepare to support another Python interpreter like Anaconda ... without success; we'll probably have a look on this subject later, at least to optimize our solution).

Sign In or Register to comment.