Skip to content

Instantly share code, notes, and snippets.

@KonradIT
Forked from Seanmatthews/gopro_streaming.cpp
Created March 20, 2018 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KonradIT/eb43a13195c1a65dc3f1cd39fec468d8 to your computer and use it in GitHub Desktop.
Save KonradIT/eb43a13195c1a65dc3f1cd39fec468d8 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

julianraheema commented Mar 20, 2018

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

@TZVIh
Copy link

TZVIh commented Apr 13, 2018

Hi,
I want to use my gopro as webcam for develop some computer vision algorithms, I want to connect it to my pc and work in real time.
how can I connect it with usb/lan/hdmi?
can I get telemetry data? (hero5/6)
Thanks!

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