1.ubunut系统搭建opencv+python开发环境
1.1.ubuntu系统安装pip3工具
sudo apt-get install python3-pip //安装python模块安装工具pip3
sudo apt install python3-tk //安装tkinter模块(类似),图形显示模块
1.2.打开pycharm开发工具,点击File->New Project->工程保存在/opt/project/opencv目录下
1.3.然后点击File->Setting->Project opencv->Project interpreter->右侧:Project interpreter:Python3.6 /usr/bin/python3.6
1.4.然后点击“+”号,在弹出的对话框中输入“opencv”进行搜索,将"opencv-python"和"opencv-contrib-python"分别选中然后点击“Install Package”安装即可
同样搜索numpy,matplotlib安装
1.5.拷贝ftp://project/code/day15_day16/images图片目录到/opt/project/opencv/目录下
cp images /opt/project/opencv/
1.6.然后新建一个文件helloopencv.py测试是否能够显示图片,支持开发环境搭建完毕
工程保存在:/opt/project/opencv/opencv_test
添加代码:
import cv2 as cv
src = cv.imread("/opt/project/opencv/images/lena.png")
cv.imshow("input", src)
cv.waitKey(0)
cv.destroyAllWindows()
1.7.其余代码参见opencv目录代码和验证即可
2.ubuntu系统搭建opencv,c++开发环境
sudo apt-get install libopencv-dev
测试:
cd /opt/project/opencv/opencv_test
vim opencv_test.cpp 添加
#include
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat srcImage = imread("/opt/project/opencv/images/lena.png");
imshow("源图像",srcImage);
waitKey(0);
return 0;
}
保存推出
g++ pkg-config opencv --cflags
opencv_test.cpp -o opencv_test pkg-config opencv --libs
./opencv_test
2.opencv移植步骤[目前支持C++]:
上位机执行:
2.1.获取源码:ftp://project/code/day14/opencv-3.4.3.zip
2.2.安装:sudo apt-get install cmake-qt-gui
2.3.配置编译opencv
mkdir /opt/project/opencv_source
mkdir /opt/project/opencv_source/opencv_arm
mkdir /opt/project/opencv_source/opencv_install
cp opencv-3.4.3.zip /opt/project/opencv_source/
cd /opt/project/opencv_source
unzip opencv-3.4.3.zip
cd /opt/project/opencv/opencv-3.4.3
cmake-gui
然后在出现的界面中做一下配置:
1.选择源代码目录:/opt/project/opencv_source/opencv-3.4.3/
2.选择Build目录:/opt/project/opencv_source/opencv_arm
3.点击Configure,保持generator为Unix Makefiles,选择Specify options for cross-compiling,点击Next
4.Operating System填写Linux
5.C Compilers填写/opt/toolchains/bin/arm-cortex_a9-linux-gnueabi-gcc
6.C++ Compilers填写/opt/toolchains/bin/arm-cortex_a9-linux-gnueabi-g++
7.程序库的Target Root填写/opt/toolchains/include
8.点击Finish
9.修改默认配置,默认安装目录为/usr/local,对于交叉编译的库来说并不合适,所以我把CMAKE_INSTALL_PREFIX变量改为/opt/project/opencv_source/opencv_install
10.选中INSTALL_PYTHON_EXAMPLE
11.将PYTHON3_EXECUTABLE修改为自己交叉编译的python路经:/opt/project/python_arm_install/bin/python3
12.将PYTHON3_INCLUDE_DIR修改为自己交叉编译的python路径:/opt/project/python_arm_install//include/python3.5m
13.将PYTHON3_LIBRARY修改为自己交叉编译python路径:/opt/project/python_arm_install/lib/python3.5/config-3.5m/libpython3.5m.a
//14.将PYTHON3_NUMPY_INCLUDE_DIRS修改为/usr/local/lib/python3.6/dist-packages/numpy/core/include
然后选择WITH_LIBV4L和WITH_V4L和WITH_QT
然后点击Configure
然后再次修改QT相关选项:
Qt5Concurrent_DIR:PATH=/opt/project/qt/lib/cmake/Qt5Concurrent
Qt5Core_DIR:PATH=/opt/project/qt/lib/cmake/Qt5Core
Qt5Gui_DIR:PATH=/opt/project/qt/lib/cmake/Qt5Gui
Qt5Test_DIR:PATH=/opt/project/qt/lib/cmake/Qt5Test
Qt5Widgets_DIR:PATH=/opt/project/qt/lib/cmake/Qt5Widgets
15.然后按Configure再点击Genertor
2.4.修改配置:
cd /opt/project/opencv_source/opencv_arm 执行
find ./ -name "flags.make" -exec sed -i "s/CXX_FLAGS = -fsigned-char/CXX_FLAGS = -fpic -fsigned-char/g" {} ;
find ./ -name "flags.make" -exec sed -i "s/C_FLAGS = -fsigned-char/C_FLAGS = -fpic -fsigned-char/g" {} ;
vim CMakeCache.txt
将:PYTHON3_INCLUDE_PATH:INTERNAL=/usr/local/include/python3.5m
修改为:
PYTHON3_INCLUDE_PATH:INTERNAL=/opt/project/python_arm_install/include/python3.5m
vim CMakeCache.txt
将PYTHON3_LIBRARIES:INTERNAL=/usr/local/lib/libpython3.5m.a
修改为PYTHON3_LIBRARIES:INTERNAL=/opt/project/python3_5_6_install/lib/python3.5/config-3.5m/libpython3.5m.a
保存推出
vim /opt/project/opencv_source/opencv-3.4.3/modules/videoio/src/cap_v4l.cpp
将#define MAX_CAMERAS 8
修改为#define MAX_CAMERAS 9
保存推出
编译安装
make -j4
make install
mkdir /opt/rootfs/home/opencv/
cp /opt/project/opencv_source/opencv_install/lib /opt/rootfs/home/opencv/ -frd
cp /opt/project/opencv_source/opencv_install/share /opt/rootfs/home/opencv/ -frd
cp /opt/project/opencv_source/opencv_install/bin /opt/rootfs/home/opencv/ -frd
vim /opt/rootfs/etc/profile 文件最后添加
export LD_LIBRARY_PATH=/home/opencv/lib:$LD_LIBRARY_PATH
保存退出
重启下位机
2.5.opencv C++测试:
参考代码位于:ftp://project/code/day14/
1.上位机执行:
mkdir /opt/project/opencv_test/capture/
cd /opt/project/opencv_test/capture
vim capture.cpp //拍照程序
/opt/project/qt/bin/qmake -project
vim capture.pro 添加:
INCLUDEPATH+=/opt/project/opencv_source/opencv_install/include
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_core.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_highgui.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_calib3d.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_features2d.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_flann.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_imgproc.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_imgcodecs.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_ml.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_objdetect.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_photo.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_superres.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_shape.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_videoio.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_video.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_videostab.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_stitching.so
保存退出
/opt/project/qt/bin/qmake
make
cp caputure /opt/rootfs/home/apptest
mkdir /opt/project/opencv_test/video/
cd /opt/project/opencv_test/video/
vim video_stream.cpp //视频显示程序
vim video.pro 添加:
INCLUDEPATH+=/opt/project/opencv_source/opencv_install/include
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_core.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_highgui.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_calib3d.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_features2d.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_flann.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_imgproc.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_imgcodecs.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_ml.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_objdetect.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_photo.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_superres.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_shape.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_videoio.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_video.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_videostab.so
LIBS+=/opt/project/opencv_source/opencv_install/lib/libopencv_stitching.so
保存退出
/opt/project/qt/bin/qmake
make
cp video /opt/rootfs/home/apptest
2.下位机然后插入摄像头运行:
cd /home/apptest
./capture
运行提示各种动态库找不到,在上位机上从交叉编译器中拷贝即可:
cp /opt/toolchains/arm-cortex_a9-linux-gnueabi/sysroot/lib/libstdc++.so.6* /opt/rootfs/lib/ -frd
cp /opt/toolchains/arm-cortex_a9-linux-gnueabi/sysroot/lib/librt* /opt/rootfs/lib/ -frd
3.然后下位机执行:
cd /home/apptest
./capture
查看picture.jpg照片
./video //查看LCD显示的视频
4.尝试将参考代码中的camerface.cpp人脸识别的代码在下位机
运行,摄像头对准头像实现人脸检测!
2.6.然后django添加拍照显示功能!
0.添加摄像头拍照硬件操作库
mkdir /opt/project/hwlib/capture/
cd /opt/project/hwlib/capture/
vim capture.cpp 添加:
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include<iostream>
#include<stdio.h>
using namespace cv;
using namespace std;
extern "C"
int camera(void)
{
VideoCapture capture(9);
Mat frame;
char filename[200];
capture >> frame;
sprintf(filename, "/home/django/ehome/ehome/static/images/picture.jpeg");
imwrite(filename, frame);
return 0;
}
保存退出
arm-cortex_a9-linux-gnueabi-g++ -shared -fpic -o libcapture.so capture.cpp
-I /opt/project/opencv_source/opencv_install/include/
-L /opt/project/opencv_source/opencv_install/lib/*.so
cp libcapture.so /opt/rootfs/home/applib
//注意一下操作步骤用pycharm工具搞定
1.修改urls.py,添加:
url('^capture$', view.capture),
2.修改view.py,文件最后添加:
拍照片
from ctypes import *
import os, sys
handle = CDLL('/home/applib/libcapture.so')
def capture(reqeuest):
ret = handle.camera()
if ret == 0:
return HttpResponse('拍照完毕,在主页面请刷新')
else:
return HttpResponse('拍照失败')
3.修改ehome.html,文件最后添加:
<form action="/capture">
<img id="picture" src="/static/images/picture.jpeg" width="320" height="240">
<input style="color: dodgerblue " type="submit" value="点击拍照">
<button onclick="reflush();return false">刷新</button>
<script type="text/javascript">
function reflush()
{
document.getElementById('picture').src="/static/images/picture.jpeg?"+Math.random();
console.log("刷新")
}
</script>
</form>
<hr/>
<br/>
<br/>
<br/>
注意路径问题
4.修改settings.py文件,文件最后添加:
cd /opt/rootfs/home/django/ehome/ehome
vim settings.py 文件最后添加:
设置图片等静态文件的路径
STATIC_ROOT = os.path.join(os.path.dirname(file),'static')
STATICFILES_DIRS = (
('css',os.path.join(STATIC_ROOT,'css').replace('\','/') ),
('js',os.path.join(STATIC_ROOT,'js').replace('\','/') ),
('images',os.path.join(STATIC_ROOT,'images').replace('\','/') ),
('upload',os.path.join(STATIC_ROOT,'upload').replace('\','/') ),
)
5.创建目录
cd /opt/rootfs/home/django/ehome/ehome //注意路经问题
mkdir static
cd static
mkdir images css js
说明:
images:保存图片
css:保存CSS配置文件
js:保存JS文件
6.浏览器测试:192.168.1.110:8000/ehome
下位机提前启动服务器:python manage.py runserver 0.0.0.0:8000