操作系统:Ubuntu 14.04 LTS版本,OpenCV 2.4.10
在Ubuntu14.04上安装NCNN
1、准备工作
1.1 首先安装protobuf
protobuf是一种轻便高效的结构化数据存储格式。
1.1.1、下载protobuf代码
推荐下载(亲测有效):
https://github.com/protocolbuffers/protobuf/releases/
我下载的是下面的版本:
tar xvf protobuf-2.6.1.tar.gz
解压后得到文件目录protobuf-2.6.1
cd protobuf-2.6.1
说明:选择下载版本前,请先执行下面的安装指令
sudo apt-get install libprotobuf-dev protobuf-compiler
终端打印信息如下:
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo: unable to resolve host tommy
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libprotobuf-lite8 libprotoc8
The following NEW packages will be installed:
libprotobuf-dev libprotobuf-lite8 libprotoc8 protobuf-compiler
0 upgraded, 4 newly installed, 0 to remove and 200 not upgraded.
Need to get 728 kB of archives.
After this operation, 4,543 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://cn.archive.ubuntu.com/ubuntu/ trusty/main libprotobuf-lite8 amd64 2.5.0-9ubuntu1 [52.7 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu/ trusty/main libprotoc8 amd64 2.5.0-9ubuntu1 [235 kB]
Get:3 http://cn.archive.ubuntu.com/ubuntu/ trusty/main libprotobuf-dev amd64 2.5.0-9ubuntu1 [421 kB]
Get:4 http://cn.archive.ubuntu.com/ubuntu/ trusty/main protobuf-compiler amd64 2.5.0-9ubuntu1 [19.8 kB]
Fetched 728 kB in 2s (256 kB/s)
Selecting previously unselected package libprotobuf-lite8:amd64.
(Reading database ... 201526 files and directories currently installed.)
Preparing to unpack .../libprotobuf-lite8_2.5.0-9ubuntu1_amd64.deb ...
Unpacking libprotobuf-lite8:amd64 (2.5.0-9ubuntu1) ...
Selecting previously unselected package libprotoc8:amd64.
Preparing to unpack .../libprotoc8_2.5.0-9ubuntu1_amd64.deb ...
Unpacking libprotoc8:amd64 (2.5.0-9ubuntu1) ...
Selecting previously unselected package libprotobuf-dev:amd64.
Preparing to unpack .../libprotobuf-dev_2.5.0-9ubuntu1_amd64.deb ...
Unpacking libprotobuf-dev:amd64 (2.5.0-9ubuntu1) ...
Selecting previously unselected package protobuf-compiler.
Preparing to unpack .../protobuf-compiler_2.5.0-9ubuntu1_amd64.deb ...
Unpacking protobuf-compiler (2.5.0-9ubuntu1) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up libprotobuf-lite8:amd64 (2.5.0-9ubuntu1) ...
Setting up libprotoc8:amd64 (2.5.0-9ubuntu1) ...
Setting up libprotobuf-dev:amd64 (2.5.0-9ubuntu1) ...
Setting up protobuf-compiler (2.5.0-9ubuntu1) ...
Processing triggers for libc-bin (2.19-0ubuntu6.14) ...
--------------------------------
如上可知:
安装的protobuf-compiler的版本是2.5.0版本的,所以我们下载的protobuf的安装版本一定要兼容它。
---------------------------------------
1.1.2、安装protobuf
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install autoconf automake libtool curl
./configure
make
make check
sudo make install
opencv库在demo运用时会用到,先安装。
NCNN例程序只支持opencv2,不支持opencv3。
1.2.1 编译前期准备工作
1)安装依赖包
主要为build-essential软件包,为编译程序提供必要的软件包的列别信息,这样软件包才知道头文件、库函数的位置。此外,它还会下载依赖的软件包,安装gcc/g++/gdb/make等基本编程工具,最后组成一个开发环境。使用命令为:
sudo apt-get install build-essential
2)安装能够支持图像读写以及视频读写的相关依赖包,使用命令如下:
sudo apt-get install libgtk2.0-dev libavcodec-dev libavformat-dev libtiff4-dev libswscale-dev libjasper-dev
sudo apt-get install qt5-default
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libavcodec-dev libavformat-dev libswscale-dev libdc1394 2.x libjpeg-dev libpng-dev libtiff-dev libjasper-dev
---------------------
3)安装cmake,用于编译源码,使用命令如下:
sudo apt-get install cmake
注:已经安装了,就跳过这一步。
4)安装pkg-config,它是一个统一接口计算机软件,用于从源码中编译软件时查询已安装的库,使用命令如下:
sudo apt-get install pkg-config
5)sudo apt-get update
1.2.2 编译安装过程
1)在官网上下载并解压OpenCV,网址为https://opencv.org/releases.html
下载所需版本,此处,我下的版本为opencv-2.4.10.zip,然后执行如下命令进行解压:
unzip opencv-2.4.10.zip
cd opencv-2.4.10
rm -rf build
mkdir build
cd build
6. cmake编译OpenCV源码,安装所有的lib文件都会被安装到/usr/local目录下
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j
sudo make install
此时安装完毕,加载环境变量,
设置动态链接用到的路径,可以执行:
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
1.2.3 测试是否安装成功
直接在终端敲入命令
pkg-config --modversion opencv
显示出版本号2.4.10,说明已经安装成功
---------------------
准备工作:官网下载cmake-3.6.3.tar.gz(https://cmake.org/download/)
1.解压文件tar -xvf cmake-3.6.3.tar.gz,并修改文件权限chmod -R 777 cmake-3.6.3
2.检测gcc和g++是否安装,如果没有则需安装gcc-g++:sudo apt-get install build-essential(或者直接执行这两条命令sudo apt-get install gcc,sudo apt-get install g++)
3.进入cmake-3.6.3 进入命令 cd cmake-3.6.3
4.执行sudo ./bootstrap
5.执行sudo make
6.执行 sudo make install
7.执行 cmake –version,返回cmake版本信息,则说明安装成功
---------------------
git clone https://github.com/Tencent/ncnn
下载完成后,需要对源码进行编译
cd ncnn
mkdir build && cd build
cmake ..
make -j
make install
这时在build/tools文件夹下有可执行文件caffe2ncnn和ncnn2mem,其作用分别是将caffe模型转成ncnn模型以及对ncnn模型进行加密。
进入ncnn/examples
自己编写一个Makefile文件
NCNN_DIR = /home/tommy/NCNN/ncnn
OPENCV = /home/tommy/NCNN/opencv-2.4.10
INCPATH = -I${NCNN_DIR}/build/install/include \
-I${OPENCV}/modules/objdetect/include \
-I${OPENCV}/modules/highgui/include \
-I${OPENCV}/modules/imgproc/include \
-I${OPENCV}/modules/core/include
LIBS = -lopencv_core -lopencv_highgui -lopencv_imgproc \
-fopenmp -pthread
LIBPATH = -L${OPENCV}/lib
squeezenet:squeezenet.cpp
$(CXX) $(INCPATH) $(LIBPATH) $^ ${NCNN_DIR}/build/install/lib/libncnn.a $(LIBS) -o $@
然后make,即可得到可执行文件squeezenet,
运行
squeezenet test.jpg
即可得到图像的分类结果。例如:
$ ./squeezenet test.jpg
568 = 0.183288
903 = 0.038845
589 = 0.036712
---------------------
在ncnn/exaples目录下建立一个新的目录./test,
touch 777 test.cpp
输入如下代码:
#include
#include
using namespace cv;
int main(int argc, char* argv[])
{
Mat image;
image = imread(argv[1], 1);
if (argc != 2 || !image.data)
{
printf("No image data\n");
return -1;
}
namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
9. 写一个cmake的makefile,也叫CMakeLists.txt
project(test)
find_package(OpenCV REQUIRED)
add_executable(test test)
target_link_libraries(test ${OpenCV_LIBS})
cmake_minimum_required(VERSION 2.8)
10. 编译+运行
(请注意camke的后面还有一个点)
cmake .
make
得到可执行文件test
11. 随便弄个jpg图片做个测试,注意要和上面那个可执行文件放在同一目录下面,我这里名字取的是test.jpg。
./test test.jpg
如果能看到照片,那就表示成功了。