#include "mainwindow.h" #include "ui_mainwindow.h" #include "YOLOPv2.h" #include #include #include string gstreamer_pipeline (int capture_width, int capture_height, int display_width, int display_height, int framerate, int flip_method) { return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" + to_string(capture_width) + ", height=(int)" + to_string(capture_height) + ", format=(string)NV12, framerate=(fraction)" + to_string(framerate) + "/1 ! nvvidconv flip-method=" + to_string(flip_method) + " ! video/x-raw, width=(int)" + to_string(display_width) + ", height=(int)" + to_string(display_height) + ", format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink"; } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); //ShowImage(); ShowVideo(); //OpenCSICamera(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::ShowImage() { Net_config YOLOPv2_nets = { 0.5, 0.5, "/home/wuxianfu/Projects/Fast-YolopV2/build/onnx/yolopv2_192x320.onnx" }; ////choices = onnx文件夹里的文件 YOLOPv2 net(YOLOPv2_nets); string imgpath = "/home/wuxianfu/Projects/Fast-YolopV2/build/images/3c0e7240-96e390d2.jpg"; static const string kWinName = "Deep learning object detection in ONNXRuntime"; namedWindow(kWinName, WINDOW_NORMAL); Mat srcimg = imread(imgpath); imshow(kWinName, srcimg); Mat outimg = net.detect(srcimg); imshow(kWinName, outimg); waitKey(0); destroyAllWindows(); } void MainWindow::ShowVideo() { Net_config YOLOPv2_nets = { 0.5, 0.5, "/home/wuxianfu/Projects/Fast-YolopV2/build/onnx/yolopv2_736x1280.onnx" }; ////choices = onnx文件夹里的文件 YOLOPv2 net(YOLOPv2_nets); static const string kWinName = "Deep learning object detection in ONNXRuntime"; namedWindow(kWinName, WINDOW_NORMAL); cv::VideoCapture cap("/home/wuxianfu/Projects/Fast-YolopV2/build/videos/566a351c29c00924a337e91e85fa7dec.mp4"); if (!cap.isOpened()) { std::cerr << "Error opening video stream" << std::endl; return; } cv::Mat frame; while (true) { cap >> frame; // 读取一帧 if (frame.empty()) { std::cerr << "Error reading frame" << std::endl; break; } auto start = std::chrono::steady_clock::now(); Mat outimg = net.detect(frame); auto end = std::chrono::steady_clock::now(); std::chrono::duration spent = end - start; qDebug()<< " Time: " << spent.count() << " sec \n"; imshow(kWinName, outimg); if (cv::waitKey(5) >= 0) break; // 按任意键退出循环 } cap.release(); // 释放资源 cv::destroyAllWindows(); // 关闭所有OpenCV窗口 } void MainWindow::OpenUSBCamera() { Net_config YOLOPv2_nets = { 0.5, 0.5, "/home/wuxianfu/Projects/Fast-YolopV2/build/onnx/yolopv2_192x320.onnx" }; ////choices = onnx文件夹里的文件 YOLOPv2 net(YOLOPv2_nets); cv::VideoCapture cap(1); // 使用默认的摄像头索引(通常是0) if (!cap.isOpened()) { std::cerr << "Error opening video stream" << std::endl; return; } cv::Mat frame; while (true) { cap >> frame; // 读取一帧 if (frame.empty()) { std::cerr << "Error reading frame" << std::endl; break; } Mat outimg = net.detect(frame); cv::imshow("USB Camera", outimg); // 显示帧 if (cv::waitKey(10) >= 0) break; // 按任意键退出循环 } cap.release(); // 释放资源 cv::destroyAllWindows(); // 关闭所有OpenCV窗口 } void MainWindow::OpenCSICamera() { Net_config YOLOPv2_nets = { 0.5, 0.5, "/home/wuxianfu/Projects/Fast-YolopV2/build/onnx/yolopv2_736x1280.onnx" }; ////choices = onnx文件夹里的文件 YOLOPv2 net(YOLOPv2_nets); string imgpath = "/home/wuxianfu/Projects/Fast-YolopV2/build/images/0ace96c3-48481887.jpg"; Mat srcimg = imread(imgpath); int capture_width = 3280 ; int capture_height = 1848 ; int display_width = 3280 ; int display_height = 1848 ; //3280*2464最大支持21帧 //3280*1848最大支持28帧 //1920*1080最大支持29帧 //1640*1232最大支持29帧 //1280*720 最大支持59帧 int framerate = 10; int flip_method = 0; //创建管道 string pipeline = gstreamer_pipeline(capture_width, capture_height, display_width, display_height, framerate, flip_method); std::cout << "使用gstreamer管道: \n\t" << pipeline << "\n"; //管道与视频流绑定 VideoCapture cap(pipeline, CAP_GSTREAMER); if(!cap.isOpened()) { std::cout<<"打开摄像头失败."<800) { new_height=int(new_width*1.0/width*height); } cv::resize(img, img, cv::Size(new_width, new_height)); Mat outimg = net.detect(img); auto end = std::chrono::steady_clock::now(); std::chrono::duration spent = end - start; qDebug()<< " Time: " << spent.count() << " sec \n"; imshow("CSI Camera",outimg); int keycode = cv::waitKey(2) & 0xff ; //ESC键退出 if (keycode == 27) break ; } cap.release(); destroyAllWindows() ; }