Unable to install OpenCV using Pip on M1 Mac

Viewed 1176

When installing opencv-python on Mac using pip, the following error happens.

  /Users/zhaojun/Github/OpenCV_Demo/venv/bin/python /Users/zhaojun/Github/OpenCV_Demo/venv/com.ctrip.com/nowatermarkdemo.py Traceback (most recent call last): File "/Users/zhao'un/Github/OoenCV Demo/venv/com ctrio com/nowatermarkdemo ov", line 1, in <module> import cv2 as cv File 
    "/Dsers/zhao"un/Github/DoenCV Demo/venv/lib/ovthon3 9/site-oacka es/cv2/ init.cv", line 5, in <module> from .cv2 import * ImportError: dlopen(/Users/zhaojun/Github/OpenCV_Demo/venv/lib/python3.9/site-packages/cv2/cv2.cpython-39-darwin.so, 2): Library not loaded: 

|/opt/homebrew/opt/ffmpeg/lib/libavcodec.58.dylib Referenced from: /Users/zhaojun/Github/OpenCV_Demo/venv/lib/python3.9/site-packages/cv2/cv2.cpython-39-darwin.so Reason: image not found' 
    Process finished with exit code 1 

And opencv does not get installed.

Things that I have read of, but are not viable options:

  1. Using Rosetta option with terminal: This works, but it causes other issues with other libraries.
  2. Using Conda and building from source: Cannot use Conda, need to use pip
  3. Building from source: Too complex, gives similar errors along the way.
1 Answers

Found the solution:

To install OpenCV using pip, you need to resolve the ffmpeg error first. Pip by default is not picking up with the right arm64 version of ffmpeg along with opencv-python. So do it separately.

So first install: (pip or pip3)

pip3 install ffmpeg

and then

pip3 install opencv-python
Related