修改为GSTREAMER

This commit is contained in:
GuanYuankai 2025-10-24 14:35:20 +08:00
parent ead081f353
commit 28c8149491
1 changed files with 61 additions and 10 deletions

View File

@ -2,16 +2,31 @@
#include <memory>
#include <sys/time.h>
#include <thread> // for std::this_thread::sleep_for
#include <chrono> // for std::chrono::milliseconds
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "rknn/rkYolov5s.hpp"
#include "rknn/rknnPool.hpp"
#include "rknnPool.hpp"
const std::string GSTREAMER_PIPELINE =
"rtspsrc location=rtsp://admin:hzx12345@192.168.1.10:554/Streaming/Channels/1301 is-live=true latency=0 ! "
"rtph265depay ! h265parse ! mppvideodec ! " // mppvideodec 是 RK 平台的硬解码器
"videoconvert ! video/x-raw,format=BGR ! appsink drop=1";
int main(int argc, char **argv)
{
char *model_name = NULL;
// const char* model_path = "/home/forlinx/rknn-cpp-Multithreading-main/model/RK3588/yolov5s-640-640.rknn";
const char* model_path = "/app/edge-proxy/models/RK3588/yolov5s-640-640.rknn";
// 参数二,模型所在路径/The path where the model is located
// 初始化rknn线程池/Initialize the rknn thread pool
int threadNum = 3;
rknnPool<rkYolov5s, cv::Mat, cv::Mat> testPool(model_path, threadNum);
if (testPool.init() != 0)
@ -20,23 +35,39 @@ int main(int argc, char **argv)
return -1;
}
// const char *video_name = "/home/forlinx/Videos/test_video.mp4";
// cv::VideoCapture capture;
// if (strlen(video_name) == 1)
// capture.open((int)(video_name[0] - '0'));
// else
// capture.open(video_name);
setenv("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;tcp", 1);
printf("Set RTSP transport protocol to TCP\n");
std::string rtsp_url = "rtsp://admin:hzx12345@192.168.1.10:554/Streaming/Channels/1301";
cv::VideoCapture capture(rtsp_url, cv::CAP_FFMPEG);
// cv::VideoCapture capture(rtsp_url, cv::CAP_FFMPEG);
cv::VideoCapture capture(GSTREAMER_PIPELINE, cv::CAP_GSTREAMER);
// if (!capture.isOpened())
// {
// printf("Error: Could not open RTSP stream.\n");
// return -1;
// }
// printf("RTSP stream opened successfully!\n");
if (!capture.isOpened())
{
printf("Error: Could not open RTSP stream.\n");
printf("Error: Could not open GStreamer RTSP stream. Check pipeline or RTSP URL.\n");
// 可以打印 GStreamer 错误到 stderr
fprintf(stderr, "GStreamer pipeline used: %s\n", GSTREAMER_PIPELINE.c_str());
return -1;
}
printf("RTSP stream opened successfully!\n");
printf("GStreamer RTSP stream opened successfully!\n");
const char* WINDOW_NAME = "RTSP Fullscreen Display";
cv::namedWindow(WINDOW_NAME, cv::WINDOW_NORMAL);
cv::setWindowProperty(WINDOW_NAME, cv::WND_PROP_FULLSCREEN, cv::WINDOW_FULLSCREEN);
struct timeval time;
gettimeofday(&time, nullptr);
auto startTime = time.tv_sec * 1000 + time.tv_usec / 1000;
@ -46,15 +77,34 @@ int main(int argc, char **argv)
while (capture.isOpened())
{
cv::Mat img;
if (capture.read(img) == false)
break;
// if (capture.read(img) == false)
// break;
if (capture.read(img) == false) {
printf("Failed to read frame from GStreamer stream. Re-attempting...\n");
// 尝试重新打开流,或者等待一段时间
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (!capture.isOpened()) { // 如果流关闭了,尝试重新打开
capture.open(GSTREAMER_PIPELINE, cv::CAP_GSTREAMER);
if (!capture.isOpened()) {
printf("Failed to re-open GStreamer stream. Exiting.\n");
break;
}
printf("GStreamer stream re-opened successfully!\n");
}
continue; // 继续下一帧
}
if (img.empty()) {
printf("Empty frame received. Skipping.\n");
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // 防止空循环过快
continue;
}
if (testPool.put(img) != 0)
break;
if (frames >= threadNum && testPool.get(img) != 0)
break;
cv::imshow(WINDOW_NAME, img);
if (cv::waitKey(1) == 'q')
if (cv::waitKey(1) == 'q') // 延时1毫秒,按q键退出/Press q to exit
break;
frames++;
@ -67,13 +117,14 @@ int main(int argc, char **argv)
}
}
// 清空rknn线程池/Clear the thread pool
while (true)
{
cv::Mat img;
if (testPool.get(img) != 0)
break;
cv::imshow(WINDOW_NAME, img);
if (cv::waitKey(1) == 'q')
if (cv::waitKey(1) == 'q') // 延时1毫秒,按q键退出/Press q to exit
break;
frames++;
}