Skip to content

Instantly share code, notes, and snippets.

@Seanmatthews
Last active November 24, 2021 14:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Seanmatthews/261d1e3c59237fef7be3a148a5713992 to your computer and use it in GitHub Desktop.
Save Seanmatthews/261d1e3c59237fef7be3a148a5713992 to your computer and use it in GitHub Desktop.
GoPro Hero4 Black C++ Streaming With OpenCV
#include <boost/exception/exception.hpp>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;
bool newFrame = false;
VideoCapture* cap;
Mat frame;
boost::mutex mtx;
boost::condition_variable cond;
void captureFunc()
{
cap = new VideoCapture("udp://@10.5.5.9:8554");
while (cap->isOpened())
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(20));
{
boost::lock_guard<boost::mutex> lock(mtx);
*cap >> frame;
newFrame = true;
}
cond.notify_one();
}
return;
}
void sendToCB(const boost::system::error_code& ec)
{
cerr << "Error " << ec.value() << endl;
}
void keepAlive()
{
try
{
boost::asio::io_service ioService;
boost::asio::ip::udp::resolver resolver(ioService);
boost::asio::ip::udp::endpoint dest(boost::asio::ip::address::from_string("10.5.5.9"), 8554);
boost::asio::ip::udp::socket sock(ioService, boost::asio::ip::udp::v4());
for (;;)
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(2000));
sock.async_send_to(boost::asio::buffer("_GPHD_:0:0:2:0.000000\n", 22), dest,
boost::bind(&sendToCB, boost::asio::placeholders::error));
cout << "Sent stay alive\n";
}
}
catch (boost::exception& e)
{
cerr << "socket errored\n";
}
}
int main(int argc, char** argv)
{
namedWindow("frame");
boost::thread capThread {captureFunc};
boost::thread keepAliveThread {keepAlive};
for (;;)
{
boost::unique_lock<boost::mutex> lock(mtx);
while (!newFrame)
{
cond.wait(lock);
}
imshow("frame", frame);
newFrame = false;
if (27 == waitKey(10)) break; // the thread sleeps for longer
}
keepAliveThread.interrupt();
cap->release();
capThread.join();
return 0;
}
@julianraheema
Copy link

How to receive json data from the camera. The camera info could be accessed through this url and the camera must be connected to the wifi http://10.5.5.9/gp/gpControl

@ardianumam
Copy link

Hi,

In case someone wanna find the similar code but in python-opencv, you may check this video tutorial with the code. Thanks.

@LennK
Copy link

LennK commented Nov 18, 2021

hello,
the frame window is stuck in loading procedure and all grey. do you have an idea what the problem is ? i am using hero4 black firmware 5.0.

@Seanmatthews
Copy link
Author

This is an ancient gist, compared to the speed of GoPro releases. I believe I wrote this code for either the Hero 3 or 4. If the code does not work for you, it most likely means that they've changed their interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment