1、下载opencv源码
我下载的是4.5.0版本的,开发板直接git的话,下载很慢。我选择用电脑下好再传给开发板,opencv-github,opencv4.5.0链接:https://pan.baidu.com/s/15WpUWDnfabdPfhLvkONoew提取码:veqb
2、安装所需的库,python版本是3.6
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
3、解压编译
cd opencv-4.5.0
mkdir build
cd build
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 \
\
..
build完成后
make -j6
sudo make install
然后就是等待编译结束,大概一个小时左右,编译期间可能会报内存不足,先是报下面这个错误,然后往上翻
使用swap创建临时分区
sudo dd if=/dev/zero of=/swapfile bs=64M count=24
sudo mkswap /swapfile
chmod 0600 /swapfile
sudo swapon /swapfile
swapoff -a
4、Python 通过Gstreamer调用摄像头
官方给的文档是通过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()
最终效果图
5、 致谢参考
rk3399上opencv使用gstreamer访问mipi摄像头
RK3399 编译安装opencv3.4(python3.5)
Ubuntu | 你的内存不够啦:c++: internal compiler error: Killed (program cc1plus)
Original: https://blog.csdn.net/weixin_41957211/article/details/121394582
Author: 咕噜咕噜_water
Title: RK3399 opencv4.5.0编译 Gstreamer访问摄像头
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/704895/
转载文章受原作者版权保护。转载请注明原作者出处!