0

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?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
joesan
  • 13,963
  • 27
  • 95
  • 232
  • use the **official** package, hosted on PyPI, installable using `pip`: `opencv-contrib-python` or `opencv-python`. both contain all the base modules. – Christoph Rackwitz Jul 17 '22 at 12:28

2 Answers2

1

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
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
joesan
  • 13,963
  • 27
  • 95
  • 232
  • it's been 2-3 hours, which is very little time. OpenCV people aren't that many, and we tend to avoid anything involving anaconda/conda/conda-forge because that's more of a headache than simply installing the official package with pip. – Christoph Rackwitz Jul 17 '22 at 12:29
  • Then how do you manage Python environments? How do you share them in a group of developers in a company? – joesan Jul 17 '22 at 12:33
  • [venv, virtualenv, ...](https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe) Continuum didn't invent the ideas. -- usually I don't bother with any of that, beyond a `requirements.txt` that anyone can deal with by just passing it to pip. people are responsible for their own environments. I don't tell them what to use. – Christoph Rackwitz Jul 17 '22 at 12:41
0

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

FlyingTeller
  • 17,638
  • 3
  • 38
  • 53