**Installing OpenCV** **[ENGR 027/CPSC 072](index.html)** **Spring 2021** **[Matt Zucker](../index.html)** First, choose your OS by selecting one of the options above. # Mac OS X I recommend using MacPorts to install its own versions of Python and OpenCV, but students have also reported success with HomeBrew as well. ## MacPorts 1. Follow the [directions](https://www.macports.org/install.php) to install MacPorts on your system. Please note this requires downloading and installing XCode, which may take a long time on your computer! 2. After the installation completes, open the terminal and run the commands ~~~ none sudo port install py38-opencv4 sudo port select --set python python38 ~~~ To verify the installation, run the `python` command in the Terminal. In the interactive Python session, type ~~~ Python import cv2 ~~~ The statement should complete with no errors. If it works, you're all set! ### Adding Keras/Tensorflow It's a good idea to install pip thru MacPorts: ~~~ none sudo port install py38-pip sudo port select --set pip pip38 ~~~ Now create a virtualenv for keras and activate it: ~~~ none python -m venv --system-site-packages ~/keras_env source ~/keras_env/bin/activate ~~~ You should see a `(keras_env)` tag in front of your command prompt. You will need to run the activate command in each new terminal session where you want to use keras. To exit this virtualenv you can always run the command `deactivate` or close the terminal window. Finally, with the `(keras_env)` activated, install keras and tensorflow: ~~~ none pip install keras tensorflow ~~~ ## Homebrew If you're already a Homebrew user, you probably know what you're doing. Feel free to drop me a line if it's not working for you, though if I can't debug your problem, I'll probably just suggest using MacPorts. # Windows (64-bit) 1. Download and run the Windows 64-bit Python 3.8 installer for the Anaconda distribution from [this page](https://www.anaconda.com/download/#windows). It should be fine to accept the default options, unless you already have a different version of Python installed, in which case you may not want to update the path and register Anaconda as the default version of Python. 2. Download this [zip file](../OpenCV%204.5.1%20Windows%2064-bit%20for%20Python%203.8.zip). Open it in Windows Explorer to verify that it contains these files: * `opencv_videoio_ffmpeg451_64.dll` * `opencv_videoio_msmf451_64.dll` * `opencv_world451.dll` * `cv2.cp38-win_amd64.pyd` 3. In another Windows Explorer window, open up the directory you installed Anaconda Python to, which should be `C:\Users\YOURNAME\anaconda3`. Drag the four files from the zip file into the Anaconda3 directory to copy them there. Finally, test the install by running the `python` command in the **Anaconda Prompt** window (available in the Start menu). In the interactive Python session, type ~~~ Python import cv2 ~~~ The statement should complete with no errors. If it works, you're all set! ## Adding Keras/Tensorflow Open the **Anaconda Prompt** and navigate to your home directory. Then run the commands ~~~ none python -m venv --system-site-packages keras_env keras_env\scripts\activate ~~~ You should see a `(keras_env)` tag in front of your command prompt. You will need to run the activate command in each new terminal session where you want to use keras. To exit this virtualenv you can always run the command `deactivate` or close the terminal window. Finally, with the `(keras_env)` activated, install keras and tensorflow: ~~~ none pip install keras tensorflow ~~~ # Ubuntu 20.04 There is a Ubuntu-maintained OpenCV 4 package. Install it with ~~~ none sudo apt install python3-opencv ~~~ Test the installation by running the `python3` command. In the interactive Python session, type ~~~ Python import cv2 ~~~ The statement should complete with no errors. If it works, you're all set! ## Adding Keras/Tensorflow Install pip and virtualenv thru apt ~~~ none sudo apt install python3-pip ~~~ Now create a virtualenv for keras and activate it: ~~~ none python3 -m venv --system-site-packages ~/keras_env source ~/keras_env/bin/activate ~~~ You should see a `(keras_env)` tag in front of your command prompt. You will need to run the activate command in each new terminal session where you want to use keras. To exit this virtualenv you can always run the command `deactivate` or close the terminal window. Finally, with the `(keras_env)` activated, install keras and tensorflow: ~~~ none pip3 install keras tensorflow ~~~ This took a while and gave me a few warnings, but seemed to work in the end. More testing may be required.