From 0dfc3b5c135522c76390cc5a923cd430d8da6622 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Wed, 11 Dec 2024 15:43:05 +0100 Subject: [PATCH] attempt with gstreamer --- src/C++/Driver/src/main.cpp | 60 ++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/src/C++/Driver/src/main.cpp b/src/C++/Driver/src/main.cpp index 1975979..ed9c0eb 100644 --- a/src/C++/Driver/src/main.cpp +++ b/src/C++/Driver/src/main.cpp @@ -290,42 +290,32 @@ void sendKobukiData(TKobukiData &data) std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } } - -void CapnSend() -{ - VideoCapture cap; - if (!cap.open("/dev/video0", cv::CAP_V4L2)) - { - cerr << "Error: Could not open camera with /dev/video0" << endl; - if (!cap.open("/dev/video1", cv::CAP_V4L2)) - { - cerr << "Error: Could not open camera with /dev/video1" << endl; - if (!cap.open("/dev/media2", cv::CAP_V4L2)) - { - cerr << "Error: Could not open camera with /dev/video1" << endl; - return; - } - } +void CapnSend() { + // Use GStreamer pipeline to access the PiCam + std::string pipeline = "libcamerasrc ! video/x-raw,width=640,height=480,framerate=30/1 ! videoconvert ! appsink"; + VideoCapture cap(pipeline, cv::CAP_GSTREAMER); + if (!cap.isOpened()) { + cerr << "Error: Could not open camera with GStreamer pipeline" << endl; + return; } - Mat frame; - while (true) - { - cap >> frame; // Capture a new image frame - if (frame.empty()) - { - cerr << "Error: Could not capture image" << endl; - continue; - } - - // Convert the image to a byte array - vector buf; - imencode(".jpg", frame, buf); - auto *enc_msg = reinterpret_cast(buf.data()); - - // Publish the image data - client.publishMessage("kobuki/cam-", string(enc_msg, enc_msg + buf.size())); - cout << "Sent image" << endl; - std::this_thread::sleep_for(std::chrono::milliseconds(400)); // Send image every 1000ms + Mat frame; + while (true) { + cap >> frame; // Capture a new image frame + if (frame.empty()) { + cerr << "Error: Could not capture image" << endl; + continue; } + + // Convert the image to a byte array + vector buf; + imencode(".jpg", frame, buf); + auto* enc_msg = reinterpret_cast(buf.data()); + + // Publish the image data + client.publishMessage("kobuki/cam", string(enc_msg, enc_msg + buf.size())); + cout << "Sent image" << endl; + + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // Send image every 1000ms } +} \ No newline at end of file