ros-perception / vision_opencv

Apache License 2.0
557 stars 601 forks source link

[FYI] A workaound for cv_bridge_boost's ImportError on Windows #537

Open sgr-aoba opened 6 days ago

sgr-aoba commented 6 days ago

This is a workaround for cv_bridge_boost's ImportError with ROS2 on Windows (humble). I hope this helps anyone encountering the same error.

Environment

Component Version
Operating System Windows 10 22H2
ROS2 Release Humble Binary installation by following the guide.
vision_opencv Humble branch Installed as a package (by git submodule). And set the environment variable OpenCV_DIR.
boost 1.74.0 (msvc-14.2-64) Binary installation from sourceforge. And set the environment variable BOOST_ROOT.

Steps to reproduce issue

Expected behavior

Nodes using CvBridge work as expected.

Actual behavior

Nodes using CvBridge will exit with the following error.

ImportError: DLL load failed while importing cv_bridge_boost: The specified module could not be found.

Workaround

It need to add DLL paths explicitly. Add the following snippet before importing CvBridge.

import platform
if platform.system() == 'Windows':
    import os
    from pathlib import Path
    from ament_index_python.packages import get_package_prefix
    dll_dir: Callable[[Path], Path] = lambda p: next(p.rglob('*.dll')).parent
    for d in [
        Path(os.environ.get('BOOST_ROOT')),
        Path(os.environ.get('OPENCV_DIR')),
        Path(get_package_prefix('cv_bridge')),
    ]:
        os.add_dll_directory(dll_dir(d))

from cv_bridge import CvBridge, CvBridgeError
sgr-aoba commented 1 day ago

Related issues: #247, ms-iot/ROSOnWindows#371