Install guide for OpenCV 3 on the Raspberry Pi

OpenCV (Open Source Computer Vision) is a library of programming functions primarily aimed at real-time computer vision. OpenCV supports the deep learning frameworks TensorFlow, Torch / PyTorch and Caffe. Especially deep learning and Caffe is important in this Raspberry Pi project.

Install OpenCV

The first thing you should do is expand your filesystem to include all available space on your micro-SD card:

sudo raspi-config

Once prompted, you should select the first option, „1. Expand File System, hit Enter on your keyboard, arrow down to the „<Finish>“ button, and then reboot your Pi:

sudo reboot

After rebooting, your file system should have all available space on your micro-SD card. You can verify that the disk has been expanded by executing df -h and examining the output:

df -h

The size of the filesystem should be almost the same size as the size of the SD card.

 

Install dependencies

The first step is to update and upgrade any existing packages:

sudo apt-get update
sudo apt-get upgrade
sudo rpi update

Install some developer tools, including CMake, which helps to configure the OpenCV build process:

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

Next, we need to install some image I/O packages that allow us to load various image file formats from disk. Examples of such file formats include JPEG, PNG, TIFF, etc.:

sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

Just as we need image I/O packages, we also need video I/O packages. These libraries allow us to read various video file formats from disk as well as work directly with video streams:

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

The OpenCV library comes with a sub-module named highgui  which is used to display images to our screen and build basic GUIs. In order to compile the highgui  module, we need to install the GTK development library:

sudo apt-get install libgtk2.0-dev

Many operations inside of OpenCV (namely matrix operations) can be optimized further by installing a few extra dependencies:

sudo apt-get install libatlas-base-dev gfortran

These optimization libraries are especially important for resource constrained devices such as the Raspberry Pi.

Lastly, install the Python 2.7 or Python 3 header files, depending which Pyhton version you want to use. In this project we used 2.7:

sudo apt-get install python2.7-dev

OR

sudo apt-get install python3-dev

 

Download the OpenCV source code

Now it its necessary to install the desired version archive of OpenCV from the official OpenCV repository. In this case we took the version 3.4.1. The most recent version can be seen on the OpenCV website: https://opencv.org/

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

We wanted the full install of OpenCV 3 so we also needed to grab the opencv_contrib repository as well:

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

Make sure your opencv  and opencv_contrib  versions are the same!

After that you have to install  pip, a Python package manager and NumPy, a Python package used for numerical processing:

wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
pip install numpy

Compile and Install OpenCV

First go to the directory of OpenCV then setup the build using CMake.

cd ~/opencv-3.4.2/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.1/modules \
    -D BUILD_EXAMPLES=ON ..

examine the output of CMake!

Start by scrolling down the section titled Python 2  and Python 3 .

If you are compiling OpenCV 3 for Python 2.7, then make sure your Python 2  section includes valid paths to the Interpreter , Libraries , numpy  and packages path.

Finally, it is possible to compile OpenCV:

make -j4

The -j4  command controls the number of cores to leverage when compiling OpenCV 3

This step can take hours to finish.

After that OpenCv can be installed:

sudo make install
sudo ldconfig

OpenCv should be installed now on the Raspberry Pi 3. To verify this use the ls command.

For python 2.7:

ls -l /usr/local/lib/python2.7/site-packages/

In some cases, OpenCV can be installed in /usr/local/lib/python2.7/dist-packages  (note the dist-packages  rather than site-packages . If you do not find the cv2.so  bindings in site-packages , be sure to check dist-packages .

For python 3:

ls -l /usr/local/lib/python2.7/site-packages/

 

Basic Installation of OpenCv

Installing OpenCV from prebuilt binaries

  1. Below Python packages are downloaded and installed at their default locations.
    1. Python 2.7.x .
    2. Numpy .
    3. Matplotlib  ( Matplotlib is optional, but recommended since we use it in our tutorials ).
  2. Install all packages into their default locations. Python wants to be installed  C:/Python27/.
  3. After installation, open Python IDLE. Enter  import numpy  and make sure Numpy is working fine.
  4. Download latest OpenCV release from  sourceforge site  and double-click to extract it.
  5. Goto  opencv / build / python / 2.7  folder.
  6. Copy  cv2.pyd  to  C: / Python27 / lib / site-packages .
  7. Open Python IDLE and type following codes in Python terminal.
    >>> import cv2 as cv
    >>> print (cv .__ version__)

If the results are printed without any errors, congratulations !!! You have installed OpenCV Python successfully.

Building OpenCV from source

  1. Download and install Visual Studio and CMake.
    1. Visual Studio 2012
    2. CMake
  2. Download and install necessary Python packages to their default locations

    1. Python 2.7.x
    2. numpy
    3. Matplotlib  ( Matplotlib is optional, but recommended since we use it in our tutorials. )
    grade
    In this case, we are using 32-bit binaries of Python packages. But if you want to use OpenCV for x64, 64-bit binaries of Python packages are to be installed. Problem is, there is no official 64-bit binaries of numpy. You have to build it on your own. For that, you have to use the same compiler to build Python. When you start Python IDLE, it shows the compiler details. You can get more  information here . So your system must have the same Visual Studio version and build Numpy from source.
    Another way to make Python packages ready for use Python distributions from third party like  AnacondaEnthought  etc. It wants to be bigger in size, but wants to have everything you need. Everything in a single shell. So you can download 32-bit versions like that.
  3. Make sure Python and Numpy are working fine.
  4. Download OpenCV source. It can be from  Sourceforge  (for official release version) or from  Github  (for latest source).
  5. Extract it to a folder, opencv and create a new folder build in it.
  6. Open CMake-gui ( Start> All Programs> CMake-gui )
  7. Fill the fields as follows (see the image below):
    1. Click on  Browse Source …  and locate the opencv folder.
    2. Click on  Browse Build …  and locate the build folder we created.
    3. Click on  Configure .

      capture1.jpg
    4. It wants to open a new window to select the compiler. Choose appropriate compiler (here, Visual Studio 11) and click  Finish .
      Capture2.png
    5. Wait until analysis is finished.
  8. You want to see all the fields are marked in red. Click on the  WITH  field to expand it. It decides what extra features you need. So mark appropriate fields. See the below image:Capture3.png
  9. Now click on  BUILD  field to expand it. First few fields configure the build method. See the below image:Capture5.png
  10. Remaining fields specify what modules are to be built. Since GPU modules are not supported by OpenCV Python, you can completely avoid it to save time (But if you work with them, keep it there). See the image below:
    Capture6.png
  11. Now click on  ENABLE  field to expand it. Make sure  ENABLE_SOLUTION_FOLDERS  is unchecked (Solution folders are not supported by Visual Studio Express edition). See the image below:
    Capture7.png
  12. So make sure that’s in the  PYTHON  field, everything is filled. (Ignore PYTHON_DEBUG_LIBRARY). Lake image below:
    Capture80.png
  13. Finally, click the  Generate  button.
  14. Now go to our  opencv / build  folder. There you will find  OpenCV.sln  file. Open it with Visual Studio.
  15. Check build mode as  Release  instead of  Debug .
  16. In the solution explorer, right-click on the  solution  (or  ALL_BUILD ) and build it. It wants to take some time to finish.
  17. Again, right-click on  INSTALL  and build it. Now OpenCV Python wants to be installed.Capture8.png
  18. Open Python IDLE and enter ‚import cv2 as cv‘. If no error, it is installed correctly.

References – https://docs.opencv.org/3.4/d5/en5/tutorial_py_setup_in_windows.html

Setting up Python in Windows 10

Installing Python under Windows 10 is easy as long as you set up your system environment correctly.

Ready? Here’s your quick guide:

1. Visit the official  Python download page  and grab the Windows installer for the latest version of Python 3. A couple of notes:

  • Python is available in two versions – Python 2 and Python 3. For beginners, that may be confusing. In short, Python 3 is the current and future state of the language; Python 2 is a legacy version that has a large base of users. Python 2 wants to  reach its end of life in January 2020  and will only fix bug till then.
  • By default, the installer provides the 32-bit version. There’s a 64-bit version available. I’m getting stuck with 32-bit for compatibility issues with some older packages.

2. Right-click on the installer and select „Run as Administrator.“ You’ll have two options – choose „Customize Installation.“

3. On the next screen, check all boxes under „Optional Features.“ Click next.

4. Under „Advanced Options,“ set the location where you want to install Python. For ease, I use:

C: \ Python36-32

How to install 32-bit Python 3.6.

Click „Install.“ When the installation finishes, close the installer.

5. Next, set the system’s PATH variable to include directories that include Python components and packages we’ll add later. To do this:

  • Open the Control Panel (easy way: click on the task bar, type „Control Panel,“ then click the icon).
  • In the Control Panel, search for Environment; click Edit the System Environment Variables. Then click the Environment Variables button.
  • In the User Variables section, you want to edit PATH variable or create one. If you’re editing an existing PATH, the values ​​are presented on separate lines in the edit dialog, like this:
C:\Python36-32 
C:\Python36-32\Lib\site-packages\
C:\Python36-32\Scripts\
  • If no PATH variable exists on your system, create one by clicking New. Make PATH the variable name and add the above directories to the variable values section as shown, separated by a semicolon:
C:\Python36-32;C:\Python36-32\Lib\site-packages;C:\Python36-32\Scripts
  • Click OK to close all the Control Panel dialogs.

6. Now, you can open a command prompt (Start Menu > Windows System > Command Prompt) and type:

python

That will load the Python interpreter:

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or license for more information.
>>>

Because of the settings you included in your PATH variable, you can now run this interpreter — and, more important, a script — from any directory on your system.

Type exit() and hit Return to exit the interpreter and get back to a C: prompt.

Optional: Set up useful Python packages

Python 3 comes with the package installer  pip  already in place, Which makes it super easy to add useful packages to your Python installation. The syntax is this (replace  some_package  with a package_name you want to install):

pip install some_package

1. Let’s add a couple of must-have utilities for web scraping:  Requests  and  BeautifulSoup . You can use the install all with one command:

pip install beautifulsoup4 requests

2.  csvkit , which is  covered here , is a great tool for dealing with comma-delimited text files. Add it:

pip install csvkit

You’re now using Python under Windows 10.

Install and configure XAMPP on Windows 10

XAMPP is a free open source software did Provides an easy way for web designers and developers to install The Necessary components to run PHP based software like  WordPress , Drupal, Joomla, and others on Windows, Mac OS X, and Linux.

Apache, MySQL, PHP, and Perl on your computer to create a test environment. XAMPP wants to save you time and frustration.

In this  guide , you’ll learn how to install the XAMPP on your computer running  Windows 10 , how to configure the software, and solve common problems.

Step-by-step guide to install XAMPP:

  1. Download XAMPP from  Apache Friends .

    Download XAMPP for Windows 10

    If you have special version requirements for PHP, then download the version you’re required to install. If you do not have a version requirement, then download the oldest version, it may help you to avoid issues trying to install a PHP based software.

  2. Double-click the file to run the installer.
  3. Click  OK  on the warning to continue.XAMPP warning on Windows
  4. Click  Next .XAMPP setup on Windows 10
  5. XAMPP offers a variety of components that you can install, such as MySQL, phpMyAdmin, PHP, Apache, and more. For the most part, you will want to use most of these components, as it is recommended to leave the default options and click  Next .XAMPP install components
  6. Uncheck the  Learn more about Bitnami for XAMPP  and click  Next .XAMPP installation wizard on Windows 10
  7. Leave the default install location settings or choose another folder to install the software, and click  Next  to begin the installation.XAMPP installation location
  8. During the installation Windows will prompt you to allow certain services to communicate through the firewall. Click Allow access through the firewall for private.XAMPP allow access through Windows Firewall
  9. Click Finish to complete the installation and to start using XAMPP Control Panel.Complete XAMPP installation on Windows 10
  10. Choose your language (English or German), and click Save to complete and open XAMPP Control Panel.XAMPP Control Panel on Windows 10

How to configure XAMPP on Windows 10

The XAMPP Control Panel includes three main sections. In Modules, you will find all the services available. You can run each service by clicking the Start button.

When you start some of the services, including Apache and MySQL, on the right side, you will also see the process ID number and TCP/IP port numbers each service is using. For example, by default Apache uses TCP/IP port 80 and 443, while MySQL uses TCP/IP port 3306.

You can also click the Admin button to get access to the administration dashboard for each service and verify that everything is working correctly.

XAMPP Control Panel

On the right side have a list of buttons to configure various aspects of the control panel, including Config to configure which modules will automatically want to start when you launch XAMPP.

XAMPP Config

Netstart will give you a list of services currently accessing the network, process ID and TCP/IP port information.

XAMPP Netstat

And there are also quick access buttons to open the shell command-line utility, XAMPP install folder, Services, and quick.

Lastly, you get the logs section, where you can get a glance of what happens every time you start a module or change a setting. This is also the first place to look at when something isn’t working.

Apache won't start on Windows 10 error

The default settings should work for most people using XAMPP to create a testing environment to run a website for personal use or a client. However, depending on your setup configuration, you may need to change the TCP/IP port number for the Apache server, the database upload size, or set the password for phpMyAdmin.

To change these settings, you’ll need to use Config button for the corresponding service. For example, you’ll need to open the httpd.conf file to change the settings on the Apache server, and the my.ini file to change the settings for MySQL.

How to fix Apache not starting on XAMPP

The XAMPP installation is very straightforward, but on Windows 10 and other versions of the operating system, sometimes there is a problem that will not allow the Apache server to run.

The issues can be attributed to the World Wide Publishing Service that may be installed and running on port 80 on your Windows PC. Port 80 also happens to be the default TCP/IP port that Apaches uses on XAMPP and no two applications can use the same port in the computer.

You have a few ways to fix the problem as shown below:

Uninstall World Wide Web Services

If you don’t have any applications depending on the World Wide Web Services feature, you can uninstall it using the following steps:

    1. Use the Windows key + X keyboard short to open the Power User menu and select Programs and Features.
    2. Click the Turn Windows features on or off link.Turn Windows features on or off
    3. Expand Internet Information Services and uncheck World Wide Web Services. Here, you can also uncheck FTP Server and Web Management Tools, if you don’t depend on those services.
  1. Click OK.Microsoft IIS - World Wide Web Services
  2. Reboot your computer to complete the task.
  3. Open XAMPP Control Panel and click Start on Apache.

Change the default Apache TCP/IP port

Alternatively, you can configure Apache to run on a different TCP/IP port using the following steps:

  1. One the XAMPP Control Panel, click the Admin button on Apache, and click httpd.conf.
  2. Scroll down and find the line: Listen 80.
  3. Change the 80 for another TCP/IP port number. You can try port 81. After the change the line should read: Listen 81.httpd.conf change TCP/IP port number
  4. Save and close the httpd.conf file.
  5. Click the Start button to run the Apache server.

The only caveat with this configuration change is that you’ll need to append TCP/IP port number on the address every time you want to connect to your website. For example, to access the Apache server on the web browser, you will need to type: localhost:81/dashboard instead of localhost/dashboard.

Manually stop World Wide Web Publishing Service

Another thing you could do is to stop World Wide Web Publishing Service (World Wide Web Services) and change its settings to only start the service manually using the following steps:

  1. Open the Start menu, do a search for Services, and press Enter.
  2. Scroll down, find, and double-click World Wide Web Publishing Service.World Wide Web Publishing Service
  3. Click Stop.
  4. Change the startup type to  manual .
  5. Click  Apply .
  6. Click  OK .Stop and set Manual settings for World Wide Web Publishing Service
  7. On XAMPP Control Panel, click the  Start  button to run the Apache server.

The best way to get Apache working again is by uninstalling the World Wide Web Publishing Service, but if that’s not possible you can change the TPC / IP port number or stop service as necessary.

References – Pureinfotech