Profile Pic

Iqbal Ahmed

I develop Android Apps along with Backend
and My only LOVE is Kotlin

2 minute read

In this post we will install OpenCV and Python on MacOS

Install XCode

sudo xcodebuild -license
sudo xcode-select install

Install Homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update 
#To resolve brew error
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow 
#To resolve brew error
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow 
brew update --force --quiet`

Install Python3

#Installing python2 and python3 if not exist
brew install python python3

#Symlink python2 and python3
brew link python
brew link python3

#If python already installed then it may ask for an upgrade
brew upgrade python
brew upgrade python3

#Verify python installed correctly
which python2
which python3

#Check python version
python2 --version
python3 --version

Install OpenCV

# Install OpenCV 3 with Python2 and Python3 bindings
brew install opencv@3
#add OpenCV site-packages path to global site-packages
echo /usr/local/opt/opencv@3/lib/python3.9/site-packages >> /usr/local/lib/python3.9/site-packages/opencv3.pth

#In case of the path not found, update the python version in the path
#if python version is Python 3.9.2 then the path should be /python3.9/

Test OpenCv

python3
import cv2
print(cv2.__version__)
import numpy as np
print(np.__version__)

Install OpenCV in Virtual Environment

Install virtualenv

pip install virtualenv virtualenvwrapper
echo "VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3" >> ~/.bash_profile
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
source ~/.bash_profile 

Create Environment

mkvirtualenv opencv-py3 -p python3
workon opencv-py3
pip install numpy ipython scipy pandas scikit-image scikit-learn matplotlib

cd ~/.virtualenvs/opencv-py3/lib/python3.9/site-packages/
ln -s /usr/local/opt/opencv@3/lib/python3.9/site-packages/cv2.cpython-39m-darwin.so cv2.so

deactivate

Test OpenCV in virtualenv

workon opencv-py3

ipython
import cv2
print(cv2.__version__)
import numpy as np
print(np.__version__)

deactivate

Say something

Comments

Nothing yet.

Recent posts

Categories

About

I develop Android Apps along with Backend
and My only LOVE is Kotlin