我下载的是4.5.0版本的,开发板直接git的话,下载很慢。我选择用电脑下好再传给开发板,opencv-github,opencv4.5.0链接:https://pan.baidu.com/s/15WpUWDnfabdPfhLvkONoew提取码:veqb
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python3.6-dev python3.6-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt install gstreamer1.0*
sudo apt install ubuntu-restricted-extras
sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
cd opencv-4.5.0
mkdir build
cd build
# python版本改成对应的,找到python和numpy的路径,和opencv关联起来
export PY_NAME=$(python3.6 -c 'from sys import version_info as v; print("python%d.%d" % v[:2])')
export PY_NUMPY_DIR=$(python3.6 -c 'import os.path, numpy.core; print(os.path.dirname(numpy.core.__file__))')
cmake -DCMAKE_BUILD_TYPE=RELEASE \
-DCMAKE_INSTALL_PREFIX=/usr/local \
\
-DPYTHON_DEFAULT_EXECUTABLE=$(which python3.6) \
-DPYTHON3_EXECUTABLE=$(which python3.6) \
-D WITH_GSTREAMER = ON \ #这个一定要打开
-DPYTHON3_INCLUDE_DIR=/usr/include/$PY_NAME \
-DPYTHON3_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/$PY_NAME \
-DPYTHON3_LIBRARY=/usr/lib/aarch64-linux-gnu/lib$PY_NAME.so \
-DPYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/$PY_NAME/dist-packages/numpy/core/include/ \
\
-DBUILD_DOCS=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF \
\
..
make -j6 #根据芯片的核数
sudo make install
然后就是等待编译结束,大概一个小时左右,编译期间可能会报内存不足,先是报下面这个错误,然后往上翻
解决方案:
使用swap创建临时分区
#count的大小就是增加的swap空间的大小,64M是块大小,所以空间大小是bs*count=1536MB
sudo dd if=/dev/zero of=/swapfile bs=64M count=24
#把刚才空间格式化成swap格式
sudo mkswap /swapfile
#该目录权限,不改的话,在下一步启动时会报“swapon: /swapfile: insecure permissions 0644, 0600 suggested.”错误
chmod 0600 /swapfile
#使用刚才创建的swap空间
sudo swapon /swapfile
#编译完成把临时空间关闭
swapoff -a
#详细的用法可以:swapoff --help
#查看当前内存使用情况:free -m
官方给的文档是通过shell脚本来访问的
$ gst-launch-1.0 v4l2src device=/dev/video1 ! video/x-raw, format=NV12, width=640, \height=480, framerate=30/1 ! waylandsink
在opencv中构建gstreamer的pipeline需要调整为:
'v4l2src device=/dev/video1 ! video/x-raw, format=NV12, width=640, height=480, framerate=30/1 ! videoconvert ! appsink'
测试代码
import numpy as np
import cv2 as cv
import os
import time
cap = cv.VideoCapture('v4l2src device=/dev/video1 ! video/x-raw, format=NV12, width=640, height=480, framerate=30/1 ! videoconvert ! appsink', cv.CAP_GSTREAMER)
if not cap.isOpened():
print("Cannot capture from camera. Exiting.")
os._exit()
last_time = time.time()
while(True):
ret, frame = cap.read()
this_time = time.time()
print (str((this_time-last_time)*1000)+'ms')
last_time = this_time;
cv.imshow('frame', frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
rk3399上opencv使用gstreamer访问mipi摄像头
RK3399 编译安装opencv3.4(python3.5)
Ubuntu | 你的内存不够啦:c++: internal compiler error: Killed (program cc1plus)