I decided to try customer analysis by face recognition using Python with Raspberry Pi and camera, and build a test environment with VM. The goal this time is to create an Ubuntu virtual environment on a Mac and run Python 3 and OpenCV on Ubuntu.
--Assuming that Ubuntu 16.04 works --Introduced Python 3.6.9 --Introduced OpenCV 4.2.0
I'm using the git command to get the source code.
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
I'm using the git command to get the source code.
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git
When downloading with the wget command, unzip the zip file with the unzip command.
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/master.zip
$ unzip opencv.zip
$ unzip opencv_contrib
Check the directory where the source is saved.
Build OpenCV. In case of cmake, contrib is also built together by adding -D OPENCV_EXTRA_MODULES_PATH = ../../opencv_contrib/modules.
$ cd ~/opencv-4.2.0
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
Compile OpenCV.
$ make -j4
[100%] Build target opencv_python3 //If this message appears, the compilation is successful.
In my Mac Book Pro VM environment, it took about an hour to compile and build. After successfully compiling and building, install OpenCV.
$ sudo make install
$ sudo ldconfig
Check the version of OpenCV.
$ opencv_version
4.2.0
First, install pip, which manages Python packages. Make the following modules available (numpy, pandas, matplotlib, sklearn).
$ sudo install pip3
$ pip3 install numpy pandas matplotlib sklearn
Enter the python console and check if various modules can be imported. If there are no errors, the module is successfully installed. Check the version of opnecv just in case.
$ python3
>>>import numpy
>>>import pandas
>>>import sklearn
>>>import matplotlib
>>>import cv2 //This is the opencv library
>>>cv2.__version__ //Check the version of opencv
'4.2.0'
>>>
You can now use Python 3 and OpenCV on the virtual machine Ubuntu. If you want to do deep learning, you should be able to use it by installing a library for deep learning (tensorflow, keras, etc.) with pip3. Next time, I would like to execute the face recognition sample code using OpenCV.
・ Click here Install Python3.6 and OpenCV (Ubuntu18.04LTS) The article was very helpful. ・ This Build opencv from source article was also helpful. ・ The official OpenCV website is here .
Recommended Posts