Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added udp_json_output to OpenPose demo and OpenPose core #1997

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/openpose/openpose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port), FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// GUI (comment or use default argument to disable any visual output)
const op::WrapperStructGui wrapperStructGui{
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_api_cpp/03_keypoints_from_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output};
opWrapper.configure(wrapperStructOutput);
// No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_api_cpp/06_face_from_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_api_cpp/07_hand_from_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_api_cpp/08_heatmaps_from_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port) };
opWrapper.configure(wrapperStructOutput);
// No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_api_cpp/09_keypoints_from_heatmaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_api_cpp/10_asynchronous_custom_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// GUI (comment or use default argument to disable any visual output)
const op::WrapperStructGui wrapperStructGui{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// GUI (comment or use default argument to disable any visual output)
const op::WrapperStructGui wrapperStructGui{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void configureWrapper(op::WrapperT<UserDatum>& opWrapperT)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapperT.configure(wrapperStructOutput);
// No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_api_cpp/14_synchronous_custom_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// GUI (comment or use default argument to disable any visual output)
const op::WrapperStructGui wrapperStructGui{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// GUI (comment or use default argument to disable any visual output)
const op::WrapperStructGui wrapperStructGui{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// GUI (comment or use default argument to disable any visual output)
const op::WrapperStructGui wrapperStructGui{
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_api_cpp/17_synchronous_custom_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void configureWrapper(op::Wrapper& opWrapper)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapper.configure(wrapperStructOutput);
// No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void configureWrapper(op::WrapperT<UserDatum>& opWrapperT)
op::String(FLAGS_write_video), FLAGS_write_video_fps, FLAGS_write_video_with_audio,
op::String(FLAGS_write_heatmaps), op::String(FLAGS_write_heatmaps_format), op::String(FLAGS_write_video_3d),
op::String(FLAGS_write_video_adam), op::String(FLAGS_write_bvh), op::String(FLAGS_udp_host),
op::String(FLAGS_udp_port)};
op::String(FLAGS_udp_port),FLAGS_udp_json_output };
opWrapperT.configure(wrapperStructOutput);
// No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
Expand Down
2 changes: 2 additions & 0 deletions include/openpose/filestream/headers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <openpose/filestream/jsonOfstream.hpp>
#include <openpose/filestream/keypointSaver.hpp>
#include <openpose/filestream/peopleJsonSaver.hpp>
#include <openpose/filestream/udpJsonSender.hpp>
#include <openpose/filestream/udpSender.hpp>
#include <openpose/filestream/videoSaver.hpp>
#include <openpose/filestream/wBvhSaver.hpp>
Expand All @@ -23,6 +24,7 @@
#include <openpose/filestream/wPeopleJsonSaver.hpp>
#include <openpose/filestream/wPoseSaver.hpp>
#include <openpose/filestream/wUdpSender.hpp>
#include <openpose/filestream/wUdpJsonSender.hpp>
#include <openpose/filestream/wVideoSaver.hpp>
#include <openpose/filestream/wVideoSaver3D.hpp>

Expand Down
175 changes: 175 additions & 0 deletions include/openpose/filestream/udpJsonSender.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#include <openpose/filestream/udpJsonSender.hpp>

#include <iostream>
#include <asio.hpp>

#include <openpose/filestream/fileStream.hpp>
#include <openpose/filestream/jsonOfstream.hpp>
#include <openpose/utilities/fastMath.hpp>

namespace op
{
class UdpJsonClient
{
public:
UdpJsonClient(const std::string& host, const std::string& port) :
mIoService{},
mUdpSocket{ mIoService, asio::ip::udp::endpoint(asio::ip::udp::v4(), 0) }
{
try
{
asio::ip::udp::resolver resolver{ mIoService };
asio::ip::udp::resolver::query query{ asio::ip::udp::v4(), host, port };
asio::ip::udp::resolver::iterator iter = resolver.resolve(query);
mUdpEndpoint = *iter;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}

~UdpJsonClient()
{
try
{
mUdpSocket.close();
}
catch (const std::exception& e)
{
errorDestructor(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}

void send(const std::string& msg)
{
try
{
mUdpSocket.send_to(asio::buffer(msg, msg.size()), mUdpEndpoint);
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}

private:
asio::io_service mIoService;
asio::ip::udp::socket mUdpSocket;
asio::ip::udp::endpoint mUdpEndpoint;
};

struct UdpJsonSender::ImplJsonUdpSender
{
// Used when increasing spCaffeNets
UdpJsonClient mUdpClient;

ImplJsonUdpSender(const std::string& udpHost, const std::string& udpPort) :
mUdpClient(udpHost, udpPort)
{
}
};

UdpJsonSender::UdpJsonSender(const std::string& udpHost, const std::string& udpPort)
: spImpl{ new ImplJsonUdpSender{udpHost, udpPort} }

{
// Debugging log
opLogIfDebug("Starting UDP JSON Sender", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
}

UdpJsonSender::~UdpJsonSender()
{
}

void UdpJsonSender::sendJson(const std::vector<std::pair<Array<float>, std::string>>& keypointVector)
{
try
{
// Sanity check
for (const auto& keypointPair : keypointVector)
if (!keypointPair.first.empty() && keypointPair.first.getNumberDimensions() != 3
&& keypointPair.first.getNumberDimensions() != 1)
error("keypointVector.getNumberDimensions() != 1 && != 3.", __LINE__, __FUNCTION__, __FILE__);

//Instead of streaming to a file, use a similar means to stream into a string that will be sent over udp
std::stringbuf streamBuf;
std::ostream os(&streamBuf);

//Build the json output - adapted from the file output model
os << "{";
try
{
// Sanity check
for (const auto& keypointPair : keypointVector)
if (!keypointPair.first.empty() && keypointPair.first.getNumberDimensions() != 3
&& keypointPair.first.getNumberDimensions() != 1)
error("keypointVector.getNumberDimensions() != 1 && != 3.", __LINE__, __FUNCTION__, __FILE__);
// Add people keypoints
os << "\"people\" : ";
os << "[";
// Get max numberPeople
auto numberPeople = 0;
for (auto vectorIndex = 0u; vectorIndex < keypointVector.size(); vectorIndex++)
numberPeople = fastMax(numberPeople, keypointVector[vectorIndex].first.getSize(0));
for (auto person = 0; person < numberPeople; person++)
{
os << "{";
//jsonOfstream.objectOpen();
for (auto vectorIndex = 0u; vectorIndex < keypointVector.size(); vectorIndex++)
{
const auto& keypoints = keypointVector[vectorIndex].first;
const auto& keypointName = keypointVector[vectorIndex].second;
const auto numberElementsPerRaw = keypoints.getSize(1) * keypoints.getSize(2);
os << "\"" + keypointName + "\" : ";
//Only the person_id node isn't an array (open)
if (keypointName != "person_id") {
os << "[";
}
// Body parts
if (numberElementsPerRaw > 0)
{
const auto finalIndex = person * numberElementsPerRaw;
for (auto element = 0; element < numberElementsPerRaw - 1; element++)
{
os << keypoints[finalIndex + element];
os << ",";
}
// Last element (no comma)
os << keypoints[finalIndex + numberElementsPerRaw - 1];
}

//Only the person_id node isn't an array (close)
if (keypointName != "person_id") {
// Close array
os << "]";
}
if (vectorIndex < keypointVector.size() - 1) {
os << ",";
}
}
os << "}";
if (person < numberPeople - 1)
{
os << ",";
}
}
// Close bodies array
os << "]";
os << "}";
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}

//Send the constructed json over UDP
spImpl->mUdpClient.send(streamBuf.str());

}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
}