程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python model +opencv compile records with c++

編輯:Python

zero 、 Basic knowledge
(1) The environment : Newly installed on Baidu ECS centos7.4 System
(2) Common code :

tar zxvf filename.tar #tar Unpack 
tar czvf filename.tar dirname #tar pack 

One 、 install cmake
(1) Download url :https://cmake.org/files/v3.10/cmake-3.10.3-Linux-x86_64.sh, Copy cmake-3.10.3-Linux-x86_64.sh To /home/backup Next .
(2)sudo sh cmake-3.10.3-Linux-x86_64.sh --prefix=/usr/local --exclude-subdir
(3) Check version

 Use cmake -version see cmake edition
Use ccmake -version see ccmake edition

(4) remarks , uninstall cmake( unsuccessful , Be careful )
find make install The file that came out after that install_manifest.txt
There is a path to everything installed , Just delete them .
Refer to the order :cat install_manifest.txt | sudo xargs rm
Verify that the command is uninstalled :sudo yum remove cmake
Uninstall the original cmake : yum -y remove cmake
Search for : find / -name cmake ( Delete all the items found )
Two 、 install python2 Related plug-ins
at present python2 Not pre installed in pip command , So you need to install it yourself pip command , Easy to install python Various packages used .
(1) First, check whether it is installed python-pip, You can execute commands directly yum install python-pip
(2) If an error is reported in the previous step , It has to be executed first yum -y install epel-release; success epel Post execution yum install python-pip It can be installed normally pip
(3) installed python-pip Finally, execute the upgrade command : pip2 install --upgrade pip
(4) To validate the pip2 Is it available , Executable command pip2 install requests install requests package
3、 ... and 、 install anaconda
(1) take Anaconda3-5.2.0-Linux-x86_64.sh Put in /home/backup in . function sh Anaconda3-5.2.0-Linux-x86_64.sh, Installation location selection /opt/install/anaconda3.
(2)Do you wish the installer to prepend the Anaconda3 install location to PATH in your /root/.bashrc ?[yes|no]. choose yes. If there is no choice , Add it yourself later .
(3)Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]. Not usually used , choose ”no”
(4)source /root/.bashrc
(5) Verify availability : python. sign out python Program , Input required :exit()
Four 、 Source code compilation and installation opencv
(1)yum update
(2)gcc upgrade

yum -y install centos-release-scl
yum -y install devtoolset-6-gcc devtoolset-6-gcc-c++ devtoolset-6-binutils
scl enable devtoolset-6 bash

It should be noted that scl Command activation is only temporary , sign out shell Or restart will restore the original system gcc edition . If you want to use it for a long time gcc 6.3 Words :

echo "source /opt/rh/devtoolset-6/enable" >>/etc/profile

(3)essential install

yum install kernel-devel # install gcc-essential
yum install gcc-gfortran # install gfortran
After the installation , It can be used gcc -v 、g++ -v and fortran -v View version .

(4)gtk2.0 install

yum install git # install git
yum install libgnomeui-devel # Install the necessary packages (gtk Graphical interface library )
yum install gtk2 gtk2-devel gtk2-devel-docs # Install the necessary components 
yum install gnome-devel gnome-devel-docs # Install optional components 

(5)pkg-config install
Usually the computer comes with it , View version :pkg-config --version
(6)ffmpeg Component installation . Mostly used for video processing , Proposed installation
install epel Extended source :yum -y install epel-release
Install extension source

yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
yum -y install ffmpeg ffmpeg-devel # install ffmpeg rely on 

test ffmpeg Has it been installed successfully? : ffmpeg -version
(7) install opencv Other dependencies :

yum install python-devel numpy
yum install libdc1394-devel
yum install libv4l-devel
yum install gstreamer-plugins-base-devel
yum -y install ant( install ant, Tears recommendation )

(8) compile opencv
hold opencv-3.4.5.zip Copied to the /root below . Then unzip and enter :unzip opencv-3.4.5.zip

cd opencv-3.4.5
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3 ..

Pay attention to the compilation information , Installed here python2 edition .

-- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.5", minimum required is "2.7")
-- Found PythonLibs: /lib64/libpython2.7.so (found suitable exact version "2.7.5")
-- Found PythonInterp: /root/anaconda3/bin/python3 (found suitable version "3.6.5", minimum required is "3.2")
-- Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is exact version "3.6.5")
-- Looking for ccache - not found

Above if CMAKE_INSTALL_PREFIX The installation directory is not specified , The default is to install to /usr/local Put it together with the system ( It is recommended to default to , Non default is not responsible ), After that, you can view the relevant supported extensions , No problem compiling and installing :

make
make install

(9)opencv Configure and load dynamic libraries
(9.1) Path addition and loading :

 Enter target file : cd /etc/ld.so.conf.d
add to opencv.conf file : touch opencv.conf
add to opencv Compiled lib Library path to opencv.conf in , Following commands :
/bin/bash -c 'echo "/usr/local/opencv3/lib64" > /etc/ld.so.conf.d/opencv.conf'
load : sudo ldconfig

Be careful : Among others linux There may be differences in the system , But it is intended to let the system know opencv lib The path of the library , Can complete dynamic loading .
(9.2) add to PATH

 Enter the command vi /etc/bashrc
Add a new line at the end of the document export PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/$PKG_CONFIG_PATH
Save and exit
Input source /etc/bashrc Make configuration effective
Input updatedb Update the database

(9.3)pkg-config To configure
default pkg Search link path /usr/lib64/pkgconfig. Need to put opencv.pc copy to pkg Under the default path of .

cp /usr/local/opencv3/lib64/pkgconfig/opencv.pc /usr/lib64/pkgconfig

5、 ... and 、 install pytorch and libtorch

conda install pytorch-cpu==1.0.0 torchvision-cpu==0.2.1 cpuonly -c pytorch # Because it comes with python-3.6.6 edition . So I doubt what I used to pretend anaconda It doesn't work .
https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-1.3.1.zip # Put it in /opt/install

6、 ... and 、 Other preparations

yum install gperftools-libs # This is not necessary 

7、 ... and 、 Operation example
(7.1) Run the command

cmake -DCMAKE_PREFIX_PATH=/opt/install/libtorch .. # See you for finished products route /home/project/example04/example-app08/build

8、 ... and 、 see python2 Configuration of , Prepare to compile
(8.1) The system configuration

locate python2.7-config # In the first place to find python2.7-config The location of 
/usr/bin/python2.7-config --cflags #pythonX.Y-config --cflags will give you the recommended flags when compiling
/usr/bin/python2.7-config --ldflags #pythonX.Y-config --ldflags will give you the recommended flags when linking

(8.2)CMakeLists Content

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(custom_ops)
include_directories(/usr/include/python2.7)
link_directories(/usr/lib64/python2.7/config)
add_executable(example-app example-app.cpp)
target_link_libraries(example-app python2.7)
set_property(TARGET example-app PROPERTY CXX_STANDARD 11)

(8.3)example-app.cpp Remember to add initialization

PyRun_SimpleString("import sys");
PyRun_SimpleString("print '---import sys---'");
PyRun_SimpleString("sys.path.append('../')");

(8.4) stay build Command under folder :

./example-app multiply multiply 3 3

(8.5)pip2 install pillow/numpy


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved