Hướng dẫn cài cv2 python 3.5

OpenCV (Open Source Computer Vision Library) là một bộ bộ thư viện xử lý thị giác máy tính thời gian thực phổ biến và được xử dụng rộng rãi trong cộng đồng người dùng. Đối với những người dùng kinh nghiệm, việc cài đặt bộ thư viện này hết sức đơn giản. Tuy nhiên, với những bạn mới bắt đầu làm quen với Raspberry Pi, để cài đặt OpenCV tương thích với phiên bản Python đang sử dụng sẽ rất phức tạp, đó là chưa kể đến quá trình compile OpenCV lâu và dễ xảy ra lỗi. Trong bài viết này, Cytron Việt Nam sẽ hướng dẫn bạn cái cài OpenCV 4.5 lên Raspberry Pi 4 Model B chạy Raspberry Pi OS Bullseyes một cách đơn giản nhất.

Xin lưu ý: Nhiệt độ quá nóng trong thời gian dài có thể làm hỏng Raspberry Pi. Bạn cần trang bị tản nhiệt & quạt trước khi bắt đầu.

Chuẩn bị về phần cứng

  • Đầu tiên, bạn sẽ cần một bo mạch Raspberry Pi 4 Model B. Cytron khuyến khích bạn sử dụng bo mạch từ 4GB RAM trở lên, nếu được hãy lấy bản 8GB RAM vì khi compile OpenCV hệ thống sẽ dùng rất nhiều RAM.
  • Tiếp theo đó, bạn sẽ cần một nguồn điện ổn định. Bạn có thể sử dụng Nguồn USB type C 15W (5.1V/3A) chính hãng từ Raspberry Pi hoặc Nguồn USB Type C Argon 18W (5V/3.5A).
  • Đồng thời, bạn cần một quạt làm mát và tản nhiệt cho Raspberry Pi vì quá trình cài tốn nhiều thời gian và nhiệt độ CPU rất nóng. Bạn có thể tham khảo Tản nhiệt đồng & Bộ vỏ Acrylic 9 Lớp và Quạt, đây là combo vỏ & tản nhiệt kinh tế nhất cho Raspberry Pi
  • Dĩ nhiên là bạn cũng sẽ cần một thẻ nhớ microSD có tốc độ đọc viết cao để tránh hiện tượng nghẽn cổ chai khi đọc / viết dữ liệu lên thẻ. Trong bài hướng dẫn, mình sử dụng Thẻ nhớ microSD MakerDisk 32GB được sản xuất bởi Cytron Technologies

Chuẩn bị về phần mềm

Để tránh các lỗi nhỏ phát sinh, hãy cài mới Raspberry Pi OS với Raspberry Pi Imager. Bạn có thể cấu hình WiFi và bật SSH khi cài đặt.

Hướng dẫn cài cv2 python 3.5

Chúng ta cũng cần nâng bộ nhớ cho GPU lên thành 128MB tại Raspberry Pi Configuration

Hướng dẫn cài cv2 python 3.5
Hướng dẫn cài cv2 python 3.5

Khi biên dịch OpenCV, Raspberry Pi sẽ sử dụng rất nhiều RAM. Nếu bạn đang sử dụng Raspberry Pi dưới 4GB RAM, hãy nâng dung lượng RAM ảo (swap) lên thành 4096MB thông qua việc chỉnh sửa file dphys-swapfile. Hãy chạy dòng lệnh bên dưới để chỉnh sửa file cấu hình

sudo nano /sbin/dphys-swapfile
sudo nano /etc/dphys-swapfile

Bạn hãy tìm dòng CONF_MAXSWAP=2048 và sửa nó thành CONF_MAXSWAP=4096

Hướng dẫn cài cv2 python 3.5
Hướng dẫn cài cv2 python 3.5

Sau khi chỉnh sửa xong, hãy khởi động lại Raspberry Pi bằng lệnh

sudo reboot

Cài đặt OpenCV 4.5 tự động

Các dòng lệnh cài đặt OpenCV và thư viện liên quan đã được chuẩn bị sẵn, bạn chỉ cần copy và dán chúng vào cửa sổ terminal để thực thi.

wget https://github.com/Qengineering/Install-OpenCV-Raspberry-Pi-32-bits/raw/main/OpenCV-4-5-5.sh
sudo chmod 755 ./OpenCV-4-5-5.sh
./OpenCV-4-5-5.sh

Quá trình cài đặt sẽ kéo dài khoảng 1 – 2 giờ đồng hồ hoặc có thể chậm hơn. Nếu thấy thông báo bên dưới, xin chúc mừng bạn, OpenCV đã được cài đặt thành công!

Hướng dẫn cài cv2 python 3.5

Dependencies cho OpenCV

Nếu không thích cài đặt OpenCV “mì ăn liền”, bạn có thể cài thủ công. Bên dưới là các dependencies cần có:

Over the past two years running the PyImageSearch blog, I’ve authored two tutorials detailing the required steps to install OpenCV (with Python bindings) on Ubuntu. You can find the two tutorials here:

  • Install OpenCV 3.0 and Python 2.7+ on Ubuntu 14.04
  • Install OpenCV 3.0 and Python 3.4+ on Ubuntu 14.04

However, with support of Ubuntu 14.04 winding down and Ubuntu 16.04 set as the next LTS (with support until April 2021), I thought it would be appropriate to create a new, updated Ubuntu + OpenCV install tutorial.

Inside this tutorial, I will document, demonstrate, and provide detailed steps to install OpenCV 3 on Ubuntu 16.04 with either Python 2.7 or Python 3.5 bindings.

Furthermore, this document has been fully updated from my previous Ubuntu 14.04 tutorials to use the latest, updated packages from the

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

0 repository.

To learn how to install OpenCV on your Ubuntu 16.04 system, keep reading.

Note: Don’t care about Python bindings and simply want OpenCV installed on your system (likely for C++ coding)? No worries, this tutorial will still work for you. Follow along with the instructions and perform the steps — by the end of this article you’ll have OpenCV installed on your system. From there, just ignore the Python bindings and proceed as usual.

Before we get into this tutorial, I want to mention that Ubuntu 16.04 actually ships out-of-the-box with both Python 2.7 and Python 3.5 installed. The actual versions (as of 24 October 2016) are:

  • Python 2.7.12 (used by default when you type $ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev 1 in your terminal).
  • Python 3.5.2 (can be accessed via the $ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev 2 command).

Again, it’s worth repeating that Python 2.7 is still the default Python version used by Ubuntu. There are plans to migrate to Python 3 and use Python 3 by default; however, as far as I can tell, we are still a long way from that actually becoming a reality.

In either case, this tutorial will support both Python 2.7 and Python 3. I’ve highlighted the steps that require you to make a decision regarding which version of Python you would like to use. Make sure you are consistent with your decision, otherwise you will inevitably run into compile, linking, and import errors.

Regarding which Python version you should use…I’m not getting into that argument. I’ll simply say that you should use whichever version of Python you are comfortable with and use on a daily basis. Keep in mind that Python 3 is the future — but also keep in mind that porting Python 2.7 code to Python 3 isn’t terribly challenging either once you understand the differences between the Python versions. And as far as OpenCV goes, OpenCV 3 doesn’t care which version of Python you’re using: the bindings will work just the same.

All that said, let’s get started installing OpenCV with Python bindings on Ubuntu 16.04.

Step

1: Install OpenCV dependencies on Ubuntu 16.04

Most (in fact, all) steps in this tutorial will be accomplished by using your terminal. To start, open up your command line and update the

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

0 package manager to refresh and upgrade and pre-installed packages/libraries:

$ sudo apt-get update $ sudo apt-get upgrade

Next, let’s install some developer tools:

$ sudo apt-get install build-essential cmake pkg-config

The

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

4 package is (very likely) already installed on your system, but be sure to include it in the above

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

0 command just in case. The

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

6 program is used to automatically configure our OpenCV build.

OpenCV is an image processing and computer vision library. Therefore, OpenCV needs to be able to load various image file formats from disk such as JPEG, PNG, TIFF, etc. In order to load these images from disk, OpenCV actually calls other image I/O libraries that actually facilitate the loading and decoding process. We install the necessary ones below:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

Okay, so now we have libraries to load images from disk — but what about video? Use the following commands to install packages used to process video streams and access frames from cameras:

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

OpenCV ships out-of-the-box with a very limited set of GUI tools. These GUI tools allow us to display an image to our screen (

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

7 ), wait for/record keypresses (

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

8 ), track mouse events (

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

9 ), and create simple GUI elements such as sliders and trackbars. Again, you shouldn’t expect to be building full-fledged GUI applications with OpenCV — these are just simple tools that allow you to debug your code and build very simple applications.

Internally, the name of the module that handles OpenCV GUI operations is

$ sudo apt-get install libgtk-3-dev

0 . The

$ sudo apt-get install libgtk-3-dev

0 module relies on the GTK library, which you should install using the following command:

$ sudo apt-get install libgtk-3-dev

Next, we install libraries that are used to optimize various functionalities inside OpenCV, such as matrix operations:

$ sudo apt-get install libatlas-base-dev gfortran

We’ll wrap up Step

1 by installing the Python development headers and libraries for both Python 2.7 and Python 3.5 (that way you have both):

$ sudo apt-get install python2.7-dev python3.5-dev

Note: If you do not install the Python development headers and static library, you’ll run into issues during Step

4 where we run

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

6 to configure our build. If these headers are not installed, then the

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

6 command will be unable to automatically determine the proper values of the Python interpreter and Python libraries. In short, the output of this section will look “empty” and you will not be able to build the Python bindings. When you get to Step

4, take the time to compare your output of the command to mine.

Step

2: Download the OpenCV source

At the time of this article’s publication, the most recent version of OpenCV is

$ sudo apt-get install libgtk-3-dev

4 , which we download a

$ sudo apt-get install libgtk-3-dev

5 of and unarchive using the following commands:

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

When new versions of OpenCV are released you can check the official OpenCV GitHub and downloaded the latest release by changing the version number of the

$ sudo apt-get install libgtk-3-dev

5 .

However, we’re not done downloading source code yet — we also need the opencv_contrib repository as well:

$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip $ unzip opencv_contrib.zip

Why are we bothering to download the contrib repo as well?

Well, we want the full install of OpenCV 3 to have access to features (no pun intended) such as SIFT and SURF. In OpenCV 2.4, SIFT and SURF were included in the default installation of OpenCV. However, with the release of OpenCV 3+, these packages have been moved to contrib, which houses either (1) modules that are currently in development or (2) modules that are marked as “non-free” (i.e., patented). You can learn more about the reasoning behind the SIFT/SURF restructuring in this blog post.

Note: You might need to expand the commands above using the “<=>” button during your copy and paste. The

$ sudo apt-get install libgtk-3-dev

5 in the

$ sudo apt-get install libgtk-3-dev

8 may be cutoff in smaller browser windows. For convenience, I have included the full URL of both the

$ sudo apt-get install libgtk-3-dev

9 archive as well as the

$ sudo apt-get install libatlas-base-dev gfortran

0 archive below:

  • https://github.com/Itseez/opencv/archive/3.1.0.zip
  • https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip

I also want to mention that both your

$ sudo apt-get install libgtk-3-dev

9 and

$ sudo apt-get install libatlas-base-dev gfortran

0 versions should be the same (in this case,

$ sudo apt-get install libgtk-3-dev

4 ). If the versions numbers do not matchup, you could very easily run into compile time errors (or worse, runtime errors that are near impossible to debug).

Step

3: Setup your Python environment — Python 2.7 or Python 3

We are now ready to start configuring our Python development environment for the build. The first step is to install

$ sudo apt-get install libatlas-base-dev gfortran

4 , a Python package manager:

$ cd ~ $ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py

I’ve mentioned this in every single OpenCV + Python install tutorial I’ve ever done, but I’ll say it again here today: I’m a huge fan of both virtualenv and virtualenvwrapper. These Python packages allow you to create separate, independent Python environments for each project that you are working on.

In short, using these packages allows you to solve the “Project X depends on version 1.x, but Project Y needs 4.x dilemma. A fantastic side effect of using Python virtual environments is that you can keep your system Python neat, tidy, and free from clutter.

While you can certainly install OpenCV with Python bindings without Python virtual environments, I highly recommend you use them as other PyImageSearch tutorials leverage Python virtual environments. I’ll also be assuming that you have both

$ sudo apt-get install libatlas-base-dev gfortran

5 and

$ sudo apt-get install libatlas-base-dev gfortran

6 installed throughout the remainder of this guide.

If you would like a full, detailed explanation on why Python virtual environments are a best practice, you should absolutely give this excellent blog post on RealPython a read. I also provide some commentary on why I personally prefer Python virtual environments in the first half of this tutorial.

Again, let me reiterate that it’s standard practice in the Python community to be leveraging virtual environments of some sort, so I suggest you do the same:

$ sudo apt-get install build-essential cmake pkg-config

0

Once we have

$ sudo apt-get install libatlas-base-dev gfortran

5 and

$ sudo apt-get install libatlas-base-dev gfortran

6 installed, we need to update our

$ sudo apt-get install libatlas-base-dev gfortran

9 file to include the following lines at the bottom of the file:

$ sudo apt-get install build-essential cmake pkg-config

1

The

$ sudo apt-get install libatlas-base-dev gfortran

9 file is simply a shell script that Bash runs whenever you launch a new terminal. You normally use this file to set various configurations. In this case, we are setting an environment variable called

$ sudo apt-get install python2.7-dev python3.5-dev

1 to point to the directory where our Python virtual environments live. We then load any necessary configurations from

$ sudo apt-get install libatlas-base-dev gfortran

6 .

To update your

$ sudo apt-get install libatlas-base-dev gfortran

9 file simply use a standard text editor. I would recommend using

$ sudo apt-get install python2.7-dev python3.5-dev

4 ,

$ sudo apt-get install python2.7-dev python3.5-dev

5 , or

$ sudo apt-get install python2.7-dev python3.5-dev

6 . You can also use graphical editors as well, but if you’re just getting started,

$ sudo apt-get install python2.7-dev python3.5-dev

4 is likely the easiest to operate.

A more simple solution is to use the

$ sudo apt-get install python2.7-dev python3.5-dev

8 command and avoid editors entirely:

$ sudo apt-get install build-essential cmake pkg-config

2

After editing our

$ sudo apt-get install libatlas-base-dev gfortran

9 file, we need to reload the changes:

$ sudo apt-get install build-essential cmake pkg-config

3

Note: Calling

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

0 on

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

1 only has to be done once for our current shell session. Anytime we open up a new terminal, the contents of

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

1 will be automatically executed (including our updates).

Now that we have installed

$ sudo apt-get install libatlas-base-dev gfortran

5 and

$ sudo apt-get install libatlas-base-dev gfortran

6 , the next step is to actually create the Python virtual environment — we do this using the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

5 command.

But before executing this command, you need to make a choice: Do you want to use Python 2.7 or Python 3?

The outcome of your choice will determine which command you run in the following section.

Creating your Python virtual environment

If you decide to use Python 2.7, use the following command to create a Python 2.7 virtual environment:

$ sudo apt-get install build-essential cmake pkg-config

4

Otherwise, use this command to create a Python 3 virtual environment:

$ sudo apt-get install build-essential cmake pkg-config

5

Regardless of which Python command you decide to use, the end result is that we have created a Python virtual environment named

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 (short for “computer vision”).

You can name this virtual environment whatever you like (and create as many Python virtual environments as you want), but for the time bing, I would suggest sticking with the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 name as that is what I’ll be using throughout the rest of this tutorial.

Verifying that you are in the “cv” virtual environment

If you ever reboot your Ubuntu system; log out and log back in; or open up a new terminal, you’ll need to use the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

8 command to re-access your

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment. An example of the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

8 command follows:

$ sudo apt-get install build-essential cmake pkg-config

6

To validate that you are in the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment, simply examine your command line — if you see the text

$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip $ unzip opencv_contrib.zip

2 preceding your prompt, then you are in the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment:

Hướng dẫn cài cv2 python 3.5
Figure 1: Make sure you see the “(cv)” text on your prompt, indicating that you are in the cv virtual environment.

Otherwise, if you do not see the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 text, then you are not in the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment:

Hướng dẫn cài cv2 python 3.5
Figure 2: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment and need to run the “workon” command to resolve this issue.

To access the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment simply use the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

8 command mentioned above.

Install NumPy into your Python virtual environment

The final step before we compile OpenCV is to install NumPy, a Python package used for numerical processing. To install NumPy, ensure you are in the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment (otherwise NumPy will be installed into the system version of Python rather than the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 environment). From there execute the following command:

$ sudo apt-get install build-essential cmake pkg-config

7

Step

4: Configuring and compiling OpenCV on Ubuntu 16.04

At this point, all of our necessary prerequisites have been installed — we are now ready to compile and OpenCV!

But before we do that, double-check that you are in the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment by examining your prompt (you should see the

$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip $ unzip opencv_contrib.zip

2 text preceding it), and if not, use the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

8 command:

$ sudo apt-get install build-essential cmake pkg-config

6

After ensuring you are in the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment, we can setup and configure our build using CMake:

$ sudo apt-get install build-essential cmake pkg-config

9

The above commands change directory to

$ cd ~ $ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py

4 , which if you have been following this tutorial is where you downloaded and unarchived the

$ sudo apt-get install libgtk-3-dev

5 files.

Note: If you are getting an error related to

$ cd ~ $ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py

6 during either the

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

6 or

$ cd ~ $ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py

8 phase of this tutorial you’ll also need to include the following option to CMake:

$ cd ~ $ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py

9 . In this case I would suggest deleting your

$ sudo apt-get install build-essential cmake pkg-config

00 directory, re-creating it, and then re-running CMake with the above option included. This will resolve the

$ sudo apt-get install build-essential cmake pkg-config

01 error. Thank you to Carter Cherry and Marcin for pointing this out in the comments section!

Inside this directory we create a sub-directory named

$ sudo apt-get install build-essential cmake pkg-config

00 and change into it. The

$ sudo apt-get install build-essential cmake pkg-config

00 directory is where the actual compile is going to take place.

Finally, we execute

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev

6 to configure our build.

Before we move on to the actual compilation of OpenCV, make sure you examine the output of CMake!

To do this, scroll down the section titled

$ sudo apt-get install build-essential cmake pkg-config

05 and

$ sudo apt-get install build-essential cmake pkg-config

06 .

If you are compiling OpenCV on Ubuntu 16.04 with Python 2.7 support, make sure the

$ sudo apt-get install build-essential cmake pkg-config

05 section includes valid paths to the

$ sudo apt-get install build-essential cmake pkg-config

08 ,

$ sudo apt-get install build-essential cmake pkg-config

09 ,

$ sudo apt-get install build-essential cmake pkg-config

10 , and

$ sudo apt-get install build-essential cmake pkg-config

11 . Your output should be similar to mine below:

Hướng dẫn cài cv2 python 3.5
Figure 3: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for Ubuntu 16.04.

Examining this output, you can see that:

  1. The $ sudo apt-get install build-essential cmake pkg-config 08 points to the Python 2.7 binary in the $ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 6 virtual environment.
  2. $ sudo apt-get install build-essential cmake pkg-config

    09 points to the Python 2.7 library (which we installed during the final step of Step

    1).

  3. The $ sudo apt-get install build-essential cmake pkg-config 10 value points to our NumPy installation in the $ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 6 virtual environment.
  4. And finally, the $ sudo apt-get install build-essential cmake pkg-config 11 points to $ sudo apt-get install build-essential cmake pkg-config 18 . When combined with the $ sudo apt-get install build-essential cmake pkg-config 19 , this means that after compiling OpenCV, we’ll find our $ sudo apt-get install build-essential cmake pkg-config 20 bindings in $ sudo apt-get install build-essential cmake pkg-config 21 .

Similarly, if you’re compiling OpenCV 16.04 with Python 3 support, you’ll want to ensure your

$ sudo apt-get install build-essential cmake pkg-config

06 section looks similar to mine below:

Hướng dẫn cài cv2 python 3.5
Figure 4: Validating that Python 3 will be used when compiling OpenCV 3 for Ubuntu 16.04.

Again, notice how my

$ sudo apt-get install build-essential cmake pkg-config

08 ,

$ sudo apt-get install build-essential cmake pkg-config

09 ,

$ sudo apt-get install build-essential cmake pkg-config

10 and

$ sudo apt-get install build-essential cmake pkg-config

11 have all been correctly set.

If you do not see the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environments in these variable paths, it’s almost certainly because you are NOT in the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment prior to running CMake!

If that is indeed the case, simply access the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment by calling

$ sudo apt-get install build-essential cmake pkg-config

30 and re-run the CMake command mentioned above.

Assuming your CMake command exited without any errors, you can now compile OpenCV:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

0

The

$ sudo apt-get install build-essential cmake pkg-config

31 switch controls the number of processes to be used when compiling OpenCV — you’ll want to set this value to the number of processors/cores on your machine. In my case, I have a quad-core processor, so I set

$ sudo apt-get install build-essential cmake pkg-config

32 .

Using multiple processes allows OpenCV to compile faster; however, there are times where race conditions are hit and the compile bombs out. While you can’t really tell if this is the case without a lot of previous experience compiling OpenCV, if you do run into an error, my first suggestion would be to run

$ sudo apt-get install build-essential cmake pkg-config

33 to flush the build, followed by compiling using only a single core:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

1

Below you can find a screenshot of a successful OpenCV + Python compile on Ubuntu 16.04:

Hướng dẫn cài cv2 python 3.5
Figure 5: Successfully compiling OpenCV 3 for Ubuntu 16.04.

The last step is to actually install OpenCV 3 on Ubuntu 16.04:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

2

Step

5: Finish your OpenCV install

You’re coming down the home stretch, just a few more steps to go and your Ubuntu 16.04 system will be all setup with OpenCV 3.

For Python 2.7:

After running

$ sudo apt-get install build-essential cmake pkg-config

34 , your Python 2.7 bindings for OpenCV 3 should now be located in

$ sudo apt-get install build-essential cmake pkg-config

35 . You can verify this using the

$ sudo apt-get install build-essential cmake pkg-config

36 command:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

3

Note: In some cases, you may find that OpenCV was installed in

$ sudo apt-get install build-essential cmake pkg-config

37 rather than

$ sudo apt-get install build-essential cmake pkg-config

38 (note

$ sudo apt-get install build-essential cmake pkg-config

39 versus

$ sudo apt-get install build-essential cmake pkg-config

40 ). If your

$ sudo apt-get install build-essential cmake pkg-config

20 bindings are not in the

$ sudo apt-get install build-essential cmake pkg-config

40 directory, be sure to check

$ sudo apt-get install build-essential cmake pkg-config

43 .

The final step is to sym-link our OpenCV

$ sudo apt-get install build-essential cmake pkg-config

20 bindings into our

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment for Python 2.7:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

4

For Python 3.5:

After running

$ sudo apt-get install build-essential cmake pkg-config

34 , your OpenCV + Python 3 bindings should be located in

$ sudo apt-get install build-essential cmake pkg-config

47 . Again, you can verify this using the

$ sudo apt-get install build-essential cmake pkg-config

36 command:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

5

I’ve been puzzled regarding this behavior ever since OpenCV 3 was released, but for some reason, when compiling OpenCV with Python 3 support, the output

$ sudo apt-get install build-essential cmake pkg-config

20 filename is different. The actual filename might vary for you, but it should look something similar to

$ sudo apt-get install build-essential cmake pkg-config

50 .

Again, I have no idea exactly why this happens, but it’s a very easy fix. All we need to do is rename the file:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

6

After renaming

$ sudo apt-get install build-essential cmake pkg-config

50 to simply

$ sudo apt-get install build-essential cmake pkg-config

20 , we can sym-link our OpenCV bindings into the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

6 virtual environment for Python 3.5:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

7

Step

6: Testing your OpenCV install

Congratulations, you now have OpenCV 3 installed on your Ubuntu 16.04 system!

To verify that your installation is working:

  1. Open up a new terminal.
  2. Execute the $ cd $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 8 command to access the $ cd $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 6 Python virtual environment.
  3. Attempt to import the Python + OpenCV bindings.

I have demonstrated how to perform these steps below:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

8

As you can see, I can import my OpenCV bindings into my Python 3.5 shell.

Below follows a screenshot of me utilizing the same steps outlined in this tutorial and importing OpenCV bindings into a Python 2.7 shell:

Hướng dẫn cài cv2 python 3.5
Figure 6: Ensuring that I can successfully import my Python + OpenCV bindings on Ubuntu 16.04.

Thus, regardless of which Python version you decide to use, simply follow the steps detailed in this tutorial and you’ll be able to install OpenCV + Python on your Ubuntu 16.04 system.

Once OpenCV has been installed, you can delete both the

$ sudo apt-get install build-essential cmake pkg-config

56 and

$ sudo apt-get install build-essential cmake pkg-config

57 directories (along with their associated

$ sudo apt-get install libgtk-3-dev

5 files):

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

9

But again, be careful when running this command! You’ll want to make sure you have properly installed OpenCV on your system prior to blowing along these directories. Otherwise, you’ll need to restart the entire compile process!

Troubleshooting and FAQ

In this section, I address some of the common questions, problems, and issues that arise when installing OpenCV on Ubuntu 16.04.

  1. When I execute

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

5 or

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

8 , I get a “command not found error”.

  1. There are three primary reasons why you would be getting this error message, all of which are related to Step

    3:

  1. First, make sure you have installed $ sudo apt-get install libatlas-base-dev gfortran 5 and $ sudo apt-get install libatlas-base-dev gfortran 6 using the $ sudo apt-get install libatlas-base-dev gfortran 4 package manager. You can verify this by running $ sudo apt-get install build-essential cmake pkg-config 64 , examining the output, and ensuring that you see both $ sudo apt-get install libatlas-base-dev gfortran 5 and $ sudo apt-get install libatlas-base-dev gfortran 6 in the list of installed packages.
  2. Your

    $ sudo apt-get install libatlas-base-dev gfortran 9 file may not be updated correctly. To diagnose this, use a text editor such as $ sudo apt-get install python2.7-dev python3.5-dev 4 and view the contents of your $ sudo apt-get install libatlas-base-dev gfortran 9 file. At the bottom of the file, you should see the proper $ sudo apt-get install build-essential cmake pkg-config 70 and $ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 0 commands are present (again, check Step

    3 for the commands that should be appended to

    $ sudo apt-get install libatlas-base-dev gfortran 9 ).
  3. After editing your $ sudo apt-get install libatlas-base-dev gfortran 9 file, you may have forgotten to $ cd $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 0 it and reload its contents. Make sure you run source $ sudo apt-get install libatlas-base-dev gfortran 9 after editing it to ensure the contents are reloaded — this will give you access to the $ cd $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 5 and $ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 8 commands.
  1. Whenever I open a new terminal, logout, or reboot my Ubuntu system, I cannot execute the

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

5 or

$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip

8 commands.

  1. See reason

    2 from the previous question.

  1. When I (1) open up a Python shell that imports OpenCV or (2) execute a Python script that calls OpenCV, I get an error:

$ sudo apt-get install build-essential cmake pkg-config

80 .

  1. Unfortunately, the exact cause of this error message is extremely hard to diagnose as there are multiple reasons this could be happening. In general, I recommend the following suggestions to help diagnose and resolve the error:
  1. Make sure you are in the $ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 6 virtual environment by using the $ sudo apt-get install build-essential cmake pkg-config 30 command. If this command gives you an error, then see the first question in this FAQ.
  2. If after you’ve ensured your $ sudo apt-get install libatlas-base-dev gfortran 9 file has been updated properly and $ cd $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 0 ‘d, then try investigating the contents of the $ sudo apt-get install build-essential cmake pkg-config 40 directory in your $ cd $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 6 virtual environment. You can find the $ sudo apt-get install build-essential cmake pkg-config 40 directory in $ sudo apt-get install build-essential cmake pkg-config 88 or $ sudo apt-get install build-essential cmake pkg-config 89 depending on your Python version. Make sure that (1) there is a $ sudo apt-get install build-essential cmake pkg-config 20 file in this $ sudo apt-get install build-essential cmake pkg-config 40 directory and (2) that it’s properly sym-linked to a valid, existing file.
  3. Be sure to check the $ sudo apt-get install build-essential cmake pkg-config 40 (and even $ sudo apt-get install build-essential cmake pkg-config 39 ) directory for the system install of Python located in $ sudo apt-get install build-essential cmake pkg-config 21 and $ sudo apt-get install build-essential cmake pkg-config 47 , respectively. Ideally, you should have a $ sudo apt-get install build-essential cmake pkg-config 20 file there.
  4. If all else fails, check in your $ sudo apt-get install build-essential cmake pkg-config 97 directory of your OpenCV build. There should be a $ sudo apt-get install build-essential cmake pkg-config 20 file there (provided that both $ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev 6 and $ cd $ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py 8 executed without error). If the $ sudo apt-get install build-essential cmake pkg-config 20 file is present, manually copy it into both the system $ sudo apt-get install build-essential cmake pkg-config 40 directory as well as the $ sudo apt-get install build-essential cmake pkg-config 40 directory for the $ cd $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip 6 virtual environment.

What's next? We recommend PyImageSearch University.

Course information: 83 total classes • 113+ hours of on-demand code walkthrough videos • Last updated: December 2023 ★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

I strongly believe that if you had the right teacher you could master computer vision and deep learning.

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s not the case.

All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that’s exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.

Inside PyImageSearch University you'll find:

  • ✓ 83 courses on essential computer vision, deep learning, and OpenCV topics
  • ✓ 83 Certificates of Completion
  • ✓ 113+ hours of on-demand video
  • ✓ Brand new courses released regularly, ensuring you can keep up with state-of-the-art techniques
  • ✓ Pre-configured Jupyter Notebooks in Google Colab
  • ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
  • ✓ Access to centralized code repos for all 532+ tutorials on PyImageSearch
  • ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
  • ✓ Access on mobile, laptop, desktop, etc.

Click here to join PyImageSearch University

Summary

In today’s blog post, I demonstrated how to install OpenCV 3 with either Python 2.7 or Python 3 bindings on your Ubuntu 16.04 system.

For more OpenCV install tutorials on other operating systems (such as OSX, Raspbian, etc.), please refer to this page where I provide additional links and resources.

But before you go…

If you’re interested in learning more about OpenCV, computer vision, and image processing, be sure to enter your email address in the form below to be notified when new blog posts are published!

Hướng dẫn cài cv2 python 3.5

Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to join the PyImageSearch Newsletter and download my FREE 17-page Resource Guide PDF on Computer Vision, OpenCV, and Deep Learning.