Instalar OpenCV en Mac

Instalando OpenCV: Requisitos 

Para poder utilizar las librerías de OpenCV necesitamos un compilador de C/C++. Además necesitaréis el CMake, para poder construir programas de OpenCV.

https://cmake.org/download/


  • brew install homebrew/science/opencv
  • Automaticamente va actualizar el homebrew

    Updating Homebrew...



    Es un poco tardado pero en caso de que te salga error prueba con la siguiente linea de comando:

    1. brew install homebrew/science/opencv3
    Va a descargar un par de paquetes de Internet por lo que es necesario estar conectado:

    ==> Downloading https://ghcr.io/v2/homebrew/portable-ruby/portable-ruby/blobs/sha256:0cb1cc7af109437fe0e020c9f3b7b95c3c709b140bde9f991ad2c1433496dd42

    ######################################################################## 100.0%

    ==> Pouring portable-ruby-2.6.8.yosemite.bottle.tar.gz


    En caso de que te de error:

    Error: homebrew/science was deprecated. This tap is now empty and all its contents were either deleted or migrated.


    Prueba con el otro comando

    1. brew install homebrew/science/opencv3




    Running `brew update --preinstall`...

    Warning: You are using macOS 10.13.

    We (and Apple) do not provide support for this old version.

    You will encounter build failures with some formulae.

    Please create pull requests instead of asking for help on Homebrew's GitHub,

    Twitter or any other official channels. You are responsible for resolving

    any issues you experience while you are running this

    old version.


    ==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2022-04-26

    ######################################################################## 100.0%

    ==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:c05a44feba2a

    ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:c05a4

    ######################################################################## 100.0%

    ==> Pouring ca-certificates--2022-04-26.all.bottle.tar.gz


    En caso de que no te funcione la instalación otra forma de instalar OpenCV es con el siguiente comando:

    pip install opencv-contrib-python



    1. Si solo se necesita módulos principales se debe ejecutar pip install opencv-python.
    2. Si se necesitan tanto módulos principales como adicionales (contrib) se debe ejecutar pip install opencv-contrib-python.

    Es probable que te aparezca el siguiente error

    DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality. 

    Esto se debe a la versión de python por lo que tendrías que utilizar python3

    $python3 -m pip install opencv-contrib-python



    Collecting opencv-contrib-python

      WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/1c/c0/67ce42700579df6579864760bfa89520154e7f01b1fcbe19bf0a10dfb0de/opencv-contrib-python-4.5.5.64.tar.gz

      WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/1c/c0/67ce42700579df6579864760bfa89520154e7f01b1fcbe19bf0a10dfb0de/opencv-contrib-python-4.5.5.64.tar.gz


    Si te aparece el siguiente error, debes actualizar PIP:

    ERROR: Could not install packages due to an EnvironmentError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Max retries exceeded with url: /packages/1c/c0/67ce42700579df6579864760bfa89520154e7f01b1fcbe19bf0a10dfb0de/opencv-contrib-python-4.5.5.64.tar.gz (Caused by ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)"))


    WARNING: You are using pip version 19.2.3, however version 22.0.4 is available.

    You should consider upgrading via the 'pip install --upgrade pip' command.



    python3 -m pip install --upgrade pip


    Collecting pip

      Downloading https://files.pythonhosted.org/packages/4d/16/0a14ca596f30316efd412a60bdfac02a7259bf8673d4d917dc60b9a21812/pip-22.0.4-py3-none-any.whl (2.1MB)

         |████████████████████████████████| 2.1MB 413kB/s 

    Installing collected packages: pip

      Found existing installation: pip 19.2.3

        Uninstalling pip-19.2.3:

          Successfully uninstalled pip-19.2.3



    Debe salir el siguiente mensaje 

    Successfully installed pip-22.0.4



    Entonces si procedemos a instalar opencv-contrib-python

    python3 -m pip install opencv-contrib-python


    Collecting opencv-contrib-python

      Downloading opencv-contrib-python-4.5.5.64.tar.gz (150.4 MB)

         ━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━━━━━━━━ 74.8/150.4 MB 2.6 MB/s eta 0:00:29


    Después de descargar los 150MB va a tardar un tiempo ya que se va instalar las dependencias:

    Installing build dependencies ... -


    Installing build dependencies ... done

      Getting requirements to build wheel ... done

      Preparing metadata (pyproject.toml) ... done

    Requirement already satisfied: numpy>=1.14.5 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from opencv-contrib-python) (1.19.2)

    Building wheels for collected packages: opencv-contrib-python

      Building wheel for opencv-contrib-python (pyproject.toml) ... |


    Si llegara a salir el siguiente error 

          [end of output]

      

      note: This error originates from a subprocess, and is likely not a problem with pip.

      ERROR: Failed building wheel for opencv-contrib-python

    Failed to build opencv-contrib-python

    ERROR: Could not build wheels for opencv-contrib-python, which is required to install pyproject.toml-based projects


    se puede solucionar con la siguientes linea de comando:

    python3 -m pip install --upgrade pip setuptools wheel

    Va a salirte el siguiente mensaje 


    Successfully installed setuptools-62.1.0 wheel-0.37.1



    y posteriormente volver a instalar 

    python3 -m pip install opencv-contrib-python









    Comentarios