Installation of OpenCV 3.3/3.2/3.0 and Python 2.7 on Windows 10 (64-bit)
Today, we’ll setup our environment with OpenCV 3 with Python on Windows 10. If you have noticed, we have already covered the Step by Step for Windows 10 OpenCV 3.3 for C++ builds.
Follow that tutorial if you would like to configure a C++ build instead of Python.
Alright, let’s get started.
Note: Bold indicates something that will change depending on your version of OpenCV, Python, or NumPy
1) If you have a version of both Python 2.X and Python 3.X installed on your computer currently it would be recommended to uninstall Python 3.X, remove any references to Python 3.X in your PATH variable, then reboot before continuing. This guide will not cover installation/configuration with OpenCV in the case of concurrent Python 2.X and Python 3.X installs.
2) Download the latest version of OpenCV, ex. OpenCV 3.3
3) Make a folder “C:\OpenCV-X.X.X” for your version of OpenCV, for example. “C:\OpenCV-3.3.0” and extract OpenCV there.
4a) Download and install the latest Python 2.X (NOT Python 3.X), for example 2.7.10 for x64 if you are on Windows 64-Bit.
4b) Some users have experienced Python hanging on Windows 10 when attempting to install before the installation screen appears. If this happens to you, it is possible that not running as an administrator may be the cause, see Appendix A.
4c) During the install, on the screen “Customize Python 2.7.10“, scroll down to “Add python.exe to Path“, click on the drop down and choose “Will be installed on the local hard drive”, this will add Python to your PATH (for all other install options the defaults are ok)
5) Reboot and make sure “C:\Python27\” is in your path variable, if not, add it (also remove any other Python paths) then reboot again
6a) Download and install the latest NumPy matching your version of Python 2.X, for example “numpy-1.9.2-win32-superpack-python2.7.exe”
Note that the version of NumPy has to match within 2.X, i.e. if you have Python 2.7.X, the NumPy for Python 2.5.X or 2.6.X will not work.
6b) If the NumPy install hangs before the installation screen appears, reboot, then rather than double-clicking on the NumPy download, instead right click the NumPy download and choose “Run as administrator”
7) If you do not want to use IDLE (the editor that ships with Python), download and install your editor of choice. PyCharm Community Edition by JetBrains (approx. 150 MB) is highly recommended (yes, it’s free, and has good auto code completion)
8a) Copy “cv2.pyd”
(note that starting OpenCV3.2, you won’t see an x86 directory for cv2.pyd)
8b) Reboot (might not be required though)
10) If you are using an example with a still image (i.e. Image_Still.py), copy any JPEG image into the project directory and rename it “image.jpg”(if you are using a webcam example then this step does not apply).
11) Run the program, for those of you that are new to Python, this can be done in one of at least 3 ways:
a) choose run in your chosen Python editor
b) double click on the .py file in Windows Explorer : Easiest
c) run from the operating system command prompt, i.e. Locate to the folder where you have stored your file and then run “python <file_name>” without quotes. If you have setup the environment variable correctly, it will work fine.
That’s the end of this easy tutorial, happy coding.
Appendix A –
How to install Python as an Administrator in Windows 10
If downloading Python and double-clicking on the downloaded .msi file as usual results in the Python install hanging before it begins, try the following steps to install Python as an administrator. With executables you can simply right-click on the downloaded file and choose “Run as administrator”, but the Python .msi file is not an executable and therefore does not present this option. The following is a work around to install Python as an administrator in Windows 10:
A1) In the “Search the web and Windows” box at the lower left, type “cmd”, an option “Command Prompt” should appear.
A2) Right-click on “Command Prompt” and choose “Run as administrator”
A3) cd to the directory where you have downloaded the Python .msi install file.
A4) Type the name of the python install file, ex. python-2.7.10.msi
Error and Troubleshooting:
Error#1: ImportError: DLL load failed: %1 is not a valid Win32 application.
But you have successfully copied the cv2.pyd to the python directory? what to do?
Answer: if this is the case, it means that you are using a 32-bit python but you have copied a 64-bit cv.pyd into the site-packages.
You have two options here, either install 64-bit python from the following link or you can downgrade the openCV version to an earlier version that 3.0 which supports x86 file python. I would recommend going with former if you are on windows 10 x64-bit.
Error#2: ImportError: numpy.core.multiarray failed to import
Answer: If you get this message even after installing the numpy correctly, it simply means that there are some configuration issues with numpy. Nothing to worry.
Open command prompt and cd/navigate to : C:\Python27\Scripts
run the below command:
pip install -U numpy
And you should be good to go.