Home > Back-end >  Installing OpenCV From Conda Environment YML File
Installing OpenCV From Conda Environment YML File

Time:07-19

I'm trying out OpenCV with Python bindings for which I'm using the following YML file:

name: opencv-python-sandbox
channels:
  - menpo
  - conda-forge
  - defaults
dependencies:
  - jupyter=1.0.0
  - jupyterlab=0.34.9
  - keras=2.9.0
  - matplotlib=3.5.2
  - numpy=1.23.1
  - opencv-python==4.6.0.66
  - pandas=1.4.3
  - python=3.8.0
  - scikit-learn=1.1.1
  - scipy=1.8.1
  - tensorboard=2.9.1
  - tensorflow=2.9.1

When I rain it threw some errors and says that it is not able to resolve OpenCV and Tensorflow:

(ml-sandbox) joesan@joesan-InfinityBook-S-14-v5:~/Projects/Private/ml-projects/ml-sandbox/opencv-python-sandbox$ conda env create -f environment.yml 
Collecting package metadata (repodata.json): done
Solving environment: failed

ResolvePackageNotFound: 
  - tensorflow=2.9.1
  - opencv-python==4.6.0.66

How to get this fixed? Do I need to add pip to my environment.yml and then manually install opencv via pip after activating the conda environment?

CodePudding user response:

Not sure why this was not answered by anyone else as this seems to be a very common problem. Nevertheless, I was able to solve this by adding pip as a dependency in my environment.yml and use pip to install OpenCV and any other libraries that won't resolve with Conda.

My environment.yml looks like this:

name: ml-sandbox
channels:
  - menpo
  - conda-forge
  - defaults
dependencies:
  - jupyter=1.0.0
  - jupyterlab=0.34.9
  - keras=2.9.0
  - matplotlib=3.5.2
  - pandas=1.4.3
  - python=3.8.0
  - pip=22.1.2
  - scikit-learn=1.1.1
  - scipy=1.8.1
  - tensorboard=2.9.1
  - pip:
      - numpy==1.23.1
      - opencv-contrib-python==4.6.0.66

CodePudding user response:

You have fixed it yourself by moving the requirements to the pip section, which results in an installation from Pypi. I just wanted to add explanation why your original attempt did not work and suggestions in case you want to strictly stick to using conda. Note that for both tensorflow and opencv, the packages provided on conda-forge are not maintained by the respective developers, often resulting in them lacking behind in versions.

The python bindings for openCV are called py-opencv on conda forge and have different version strings, so you would need to put py-opencv==4.6.0 in your yml

tensorflow on conda-forge goes only up to 2.8.1. So when strictly sticking to conda, you would need to downgrade the version

You can always check available versions for packages by using conda search -c <channel> <package-name> from your terminal

  • Related