diff --git a/src/rknn/main.cc b/src/rknn/main.cc index a33f625..1e8276b 100644 --- a/src/rknn/main.cc +++ b/src/rknn/main.cc @@ -2,16 +2,31 @@ #include #include +#include // for std::this_thread::sleep_for +#include // 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 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++; }