A few days ago, I was playing around with some interesting object detection projects using OpenCV and Python3. In this blog, I’d like to share some issues I found during installation and development environment setup.
Details of my dev environment:
- ubuntu 16.04 LTS
- Python 3.5.2
- OpenCV 3.1.0
Now, I’ll show you how to install them and set up a dev environment probably.
First there are lots of libs and dependencies before you start installing Python or OpenCV. Please see below lists of commands. It’s better to follow them step and step to install all of them.
Upgrade any pre-installed packages:
Install tools to compile OpenCV 3.1.0:
If you are going to develop object detection functionalities on images, then below libraries and packages are needed:
Also, if you need to work on videos, below libraries are needed as well:
Install packages that are used to optimize various functions inside OpenCV, such as matrix operations:
After you run above commands to install all libraries and packages without errors, you are ready to go next step - setting up Python environment.
NOTE: It’s highly recomended to always use virtual environment for Python development. But if you still don’t want to use virtual environment, you could skip this part.
Here I’m going to use virtualenv and virtualenvwrapper. They will help us create separate and independent Python environments, so that we don’t junk up our system Python install. Moreover we can have a separate Python environment for each of our projects).
Get pip using below commands: (You need to indicate Python3 inside the commands)
Install and setup virtualenv and virtualenvwrapper:
Update ~/.bashrc file. Below commands need to be appended to the file:
It’s pointing VIRTUALENVWRAPPER_PYTHON to where Python3.5 binary exists on Ubuntu system. Next is to reload ~/.bashrc file:
Create cv virtual environment where you could work on object detection projects using OpenCV and Python.
To test if the virtual environment is created successfully, open another terminal, type commands:
If you see cv at the begining of our terminal, it means you have set up virtual environment to use OpenCV and Python successfully.
Now we have a virtual environment available for Python development. Let’s install Python 3.5 in this environment. Below command is used to install the Python 3.5 headers and development files:
Install necessary libraries:
You could other libraries you need.
Make sure you are still in virtual environment. Pull OpenCV projects from github and checkout the 3.1.0 version:
Meanwhile you need to grab the opencv_contrib repo as well. Without this repository, you will be missing some of OpenCV 3.0 features like text detection in natural images:
Make sure that you checkout the same version for opencv_contrib that you did for OpenCV above, otherwise you could run into compilation errors.
Next step is to build OpenCV: (I’m sharing my cmake commands. You could find different commands from other website. If mine is not working on your local machine, please find others’ build commands. There must be a list of cmake commands working in your situation)
In order to build OpenCV 3.1.0 , you need to set -D INSTALL_C_EXAMPLES=OFF (rather than ON ) in the cmake command.
If your build output looks like below, then your build mostly is done successfully.
CMake is now using the Python 3.5. It means OpenCV 3.1.0 will be compiled with Python 3.5. Let’s start the OpenCV compile process:
Assuming OpenCV 3.1.0 compiled without errors, you can install it on your system:
If you’ve reached this step, OpenCV 3.1.0 should now be installed in /usr/local/lib/python3.5/site-packages/. In this folder you can see OpenCV bindings are stored under the name cv2.cpython-35m.so. To use OpenCV 3.1.0 within cv virtual environment, you need to sym-link OpenCV into site-packages:
After this, Python can import our OpenCV bindings using name cv2
Again, make sure you are in the cv virtual environment, then call Pyhthon3 and try to import cv2:
Congratulations! You are able to use Python and OpenCV on computer vision projects!