系統(tǒng)debian10痰催,攝像頭:海康;
鑒于瑞芯微官網(wǎng)論壇介紹硬解碼使用不夠詳細(xì)迎瞧,在踩了許多坑的情況下總結(jié)如下夸溶,供大家參考。
系統(tǒng)軟件包升級(jí)
- 更新源:sudo apt update --fix-missing
- 升級(jí)軟件包:sudo apt -y upgrade
注意:升級(jí)過(guò)去中會(huì)有提示確認(rèn)是否提供/etc/apt/sources.list.d/toybrick.list,請(qǐng)輸入"Y" - 再次更新源:sudo apt update
說(shuō)明:上述步驟只需要執(zhí)行一次即可凶硅,后續(xù)軟件包升級(jí)只需要執(zhí)行命令
sudo apt update; sudo apt upgrade
參考:
瑞芯微社區(qū)關(guān)于toybrick系列debian10系統(tǒng)軟件包升級(jí)說(shuō)明
一缝裁、 python方式
install toybrick-0.3.0-py3-none-any.whl 下載地址:鏈接: https://pan.baidu.com/s/1AkJ70nTTIIXbgDYbF6IPUQ 提取碼: b54n
安裝以下依賴(lài)
sudo apt-get install g++ binutils-gold xorg-dev libglu1-mesa-dev
sudo apt install libgbm-dev
sudo apt install rockchip-mpp
sudo apt install toybrick-gbm-dev
sudo toybrick-mali.sh link
sudo pip3 install toybrick-0.3.0-py3-none-any.whl
驗(yàn)證安裝是否成功
import toybrick as toy
python掉用代碼
import toybrick as toy
import time
import cv2
url = "rtsp://admin:123456@192.168.1.200:554/h264/ch1/main/av_stream"
username = "admin"
pwd = "123456"
#rtsp = toy.input.createRtspClient(url, username, pwd, False)
rtsp = toy.input.createRtspClient(url)
rtsp.connect()
last = time.time()
gl = toy.output.createGLDrmDisplay(toy.DisplayPort.HDMI_A)
idx0 = gl.add_view(50, 600, 768, 432)
frame_index = 0
while rtsp.is_opened():
frame = rtsp.read_rgb(768, 432)
now = time.time()
gl.show(idx0, frame)
print (frame_index, "----------------------------",now - last)
frame = frame.array()
cv2.imwrite("images/4_" + str(frame_index) + ".jpg", frame)
# cv2.imshow('Carplate demo', cv2.resize(frame, (960, 540))) #
frame_index += 1
last = now
運(yùn)行效果
參考:
瑞芯微社區(qū)RK3399Pro入門(mén)教程(8)6路1080P30幀解碼顯示范例
二.、C++方式
安裝依賴(lài)
sudo apt-get install curl
sudo apt-get install libcurl4-openssl-dev
sudo apt-get install libopencv-dev
sudo apt install rockchip-drm-dev libdrm-dev
sudo apt install rockchip-rtsp-dev
sudo apt install rockchip-mpp-dev
sudo apt install rockchip-rga-dev
rtsp_ssd .cpp
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <thread>
#include <memory.h>
#include <sys/time.h>
#include <queue>
using namespace std;
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <rockchip/rockchip_rtsp.h>
#include <rockchip/rockchip_mpp.h>
extern "C" {
#include <rockchip/rockchip_rga.h>
}
#define RTSP_URL "rtsp://192.168.1.200/h264/ch1/main/av_stream"
#define RTSP_USER "admin"
#define RTSP_PWD "123456"
static MppDecoder *mpp_dec = NULL;
static std::queue<DecFrame *> frame_queue;
static int run_flag = 0;
unsigned long get_time(void)
{
struct timeval ts;
gettimeofday(&ts, NULL);
return (ts.tv_sec * 1000 + ts.tv_usec / 1000);
}
void onRtspHandle(unsigned char *buf, size_t len)
{
std::cout << "frame recived " << len << std::endl;
mpp_dec->ops->enqueue(mpp_dec, buf, len);
}
void inference_thread(RockchipRga *rga, int width, int height)
{
int ret;
int resize_w = 1920, resize_h = 1080;
static int frame_size = 0;
unsigned char *frame_rgb = NULL;
rga->ops->initCtx(rga);
rga->ops->setRotate(rga, RGA_ROTATE_NONE);
rga->ops->setSrcFormat(rga, V4L2_PIX_FMT_NV12, width, height);
rga->ops->setDstFormat(rga, V4L2_PIX_FMT_BGR24, resize_w, resize_h);
frame_size = resize_w * resize_h * 3;
frame_rgb = (unsigned char *)malloc(frame_size);
cv::Mat img(resize_h , resize_w , CV_8UC3, frame_rgb);
if (!frame_rgb)
goto exit;
rga->ops->setDstBufferPtr(rga, frame_rgb);
while (run_flag) {
if (frame_queue.empty()) {
usleep(1000);
continue;
}
auto frame = frame_queue.front();
rga->ops->setSrcBufferPtr(rga, frame->data);
ret = rga->ops->go(rga);
printf("inference_thread ................\n");
if (!ret) {
///do something with frame_rgb
cv::imshow("test", img);
cv::waitKey(10);
}
frame_queue.pop();
mpp_dec->ops->freeFrame(frame);
}
exit:
run_flag = 0;
while (!frame_queue.empty()) {
auto frame = frame_queue.front();
mpp_dec->ops->freeFrame(frame);
frame_queue.pop();
}
if (frame_rgb)
free(frame_rgb);
}
void decode_thread(RockchipRga *rga)
{
int ret;
int first_frame = 0;
std::thread t_inference;
while (run_flag) {
DecFrame *frame = mpp_dec->ops->dequeue_timeout(mpp_dec, 300);
if (frame != NULL) {
std::cout << "decode frame" << frame->width << "x" << frame->height << std::endl;
if (!first_frame) {
std::cout << "first_frame" << std::endl;
t_inference = std::thread(inference_thread, rga, frame->width, frame->height);
first_frame = 1;
}
if (frame_queue.size() < 30)
frame_queue.push(frame);
else
mpp_dec->ops->freeFrame(frame);
}
}
exit:
run_flag = 0;
t_inference.join();
while (!frame_queue.empty()) {
auto frame = frame_queue.front();
mpp_dec->ops->freeFrame(frame);
frame_queue.pop();
}
}
int main(int argc, char **argv)
{
int ret;
RockchipRga *rga;
unsigned char ready[5] = {'r', 'e', 'a', 'd', 'y'};
RtspClient rtsp_client(RTSP_URL, RTSP_USER, RTSP_PWD);
rtsp_client.setDataCallback(onRtspHandle);
mpp_dec = MppDecoderCreate(DECODE_TYPE_H264);
if (!mpp_dec) {
std::cout << "MppDecoderCreate error!\n" << std::endl;
return -1;
}
rga = RgaCreate();
if (!rga) {
MppDecoderDestroy(mpp_dec);
std::cout << "rgaCreate error!\n" << std::endl;
return -1;
}
run_flag = 1;
rtsp_client.enable();
std::thread t_decode(decode_thread, rga);
while (run_flag) {
usleep(10000);
}
rtsp_client.disable();
run_flag = 0;
t_decode.join();
RgaDestroy(rga);
MppDecoderDestroy(mpp_dec);
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_COMPILER g++)
find_package(CURL REQUIRED)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(link_libs rockchip_rtsp rockchip_mpp curl rockchip_rga pthread
opencv_core opencv_highgui opencv_imgcodecs)
add_executable(rtsp_ssd rtsp_ssd.cpp)
target_link_libraries(rtsp_ssd ${link_libs})
編譯
cd local_rtsp/build
cmake ..
make
./rtsp_ssd
運(yùn)行效果
參考:
- 瑞芯微社區(qū)Toybrick Debian 10用戶(hù)指南
- 瑞芯微社區(qū)RK3399Pro入門(mén)教程(7)RTSP庫(kù)的使用