Skip to content

Commit

Permalink
[RLlib] Make opencv-python-headless default over opencv-python (#48776)
Browse files Browse the repository at this point in the history
## Why are these changes needed?

Make `opencv-python-headless` defeault requirement for RLlib/
This is necessary, because `opencv-python` may not work in many headless
environments.
  • Loading branch information
ArturNiederfahrenhorst authored Nov 19, 2024
1 parent b5934e5 commit aaac19c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/requirements/ml/rllib-test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Atari
ale_py==0.10.1
imageio==2.34.2
opencv-python==4.8.1.78
opencv-python-headless==4.8.1.78

# For testing MuJoCo envs with gymnasium.
mujoco==3.2.4
Expand Down
19 changes: 18 additions & 1 deletion rllib/utils/images.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import logging
import importlib

import numpy as np

from ray.rllib.utils.annotations import DeveloperAPI

logger = logging.getLogger(__name__)


@DeveloperAPI
def is_package_installed(package_name):
try:
importlib.metadata.version(package_name)
return True
except importlib.metadata.PackageNotFoundError:
return False


try:
import cv2

cv2.ocl.setUseOpenCL(False)

logger.debug("CV2 found for image processing.")
except ImportError:
except ImportError as e:
if is_package_installed("opencv-python"):
raise ImportError(
f"OpenCV is installed, but we failed to import it. This may be because "
f"you need to install `opencv-python-headless` instead of "
f"`opencv-python`. Error message: {e}",
)
cv2 = None


Expand Down

0 comments on commit aaac19c

Please sign in to comment.