linux gstreamer gst rk3399


原文链接: linux gstreamer gst rk3399

译安装支持gstreamer版本的opencv

我下载的是opencv3.4.2的源码。在rk3399上编译安装。

编译之前需要安装依赖的包:

apt-get update
apt-get install -y libgstreamer1.0-0 \
            gstreamer1.0-plugins-base \
            gstreamer1.0-plugins-good \
            gstreamer1.0-plugins-bad \
            gstreamer1.0-plugins-ugly \
            gstreamer1.0-libav \
            gstreamer1.0-doc \
            gstreamer1.0-tools \
            libgstreamer1.0-dev \
            libgstreamer-plugins-base1.0-dev

apt-get install libpng12-0
apt-get install -y \
        build-essential \
        cmake \
        git
apt-get install -y pkg-config \
        libjpeg8-dev
apt-get install -y libgtk2.0-dev \
        libavcodec-dev \
        libavformat-dev \
        libswscale-dev \
        libv4l-dev \
        libatlas-base-dev \
        gfortran \
        libhdf5-dev
apt-get install libjpeg-dev
apt-get install libtiff4-dev
apt-get install -y libtbb-dev libeigen3-dev

编译opencv的cmake命令:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D PYTHON_EXECUTABLE=/usr/bin/python3 -D PYTHON_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.6/dist-packages/numpy/core/include/ -D WITH_GSTREAMER=ON -D WITH_GTK=ON -D WITH_GTHREAD=ON -D WITH_TBB=ON -DWITH_OPENGL=ON ..

1
2
注意上面我指定了python3的环境目录,因为一会我要用python接口调用opencv,只使用c++接口的可以去掉python相关的选项。-D WITH_GSTREAMER=ON是为了开启gstreamer。

编译和安装

make -j 6

make install

gst-launch-1.0 rkisp io-mode=4 path-iqf=/etc/cam_iq/rk-ov4689.xml ! video/x-raw,format=NV12,width=1280,height=720,framerate=30/1 ! rkximagesink

make install

1
2
测试代码
这里使用python调用opencv。

上面说了,shell脚本调用gst-launch-1.0的参数如下:

gst-launch-1.0 rkisp io-mode=4 path-iqf=/etc/cam_iq/rk-ov4689.xml ! video/x-raw,format=NV12,width=1280,height=720,framerate=30/1 ! rkximagesink
1
在opencv中构建gstreamer的pipeline需要调整为:

rkisp io-mode=4 path-iqf=/etc/cam_iq/rk-ov4689.xml ! video/x-raw,format=NV12,width=740 ,height=360,framerate=30/1 ! videoconvert !appsink
1
下面是完整代码:

import numpy as np
import cv2 as cv

#cap = cv.VideoCapture('demo.mp4')
cap = cv.VideoCapture('rkisp io-mode=4 path-iqf=/etc/cam_iq/rk-ov4689.xml ! video/x-raw,format=NV12,width=740 ,height=360,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()
    cv.imshow('frame', frame)

    if cv.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()

我发现在NanoPC T4开发板上,这样访问摄像头对于画面分辨率会有要求,分辨率高于740x360就会出现画面上下割裂的情况,很像是两个线程处理一个buffer没同步好,我自己是gstreamer初学者,暂时不清楚怎么解决这个问题。不知道是rock官方该背锅还是友善之臂该背锅(论硬件平台成熟的重要性,此处省去三千字…)。而用gst-launch-1.0访问无论多大分辨率都不会有此问题。

我暂时只使用740x360这个分辨率。

`