hello_world example expanded with library option

This commit is contained in:
Ivan Martinović 2022-07-14 23:21:04 +02:00
parent 4a20e9909d
commit 66171159ef
5 changed files with 41 additions and 18 deletions

View File

@ -205,7 +205,7 @@ new_local_repository(
# For local MacOS builds, the path should point to an opencv@3 installation. # For local MacOS builds, the path should point to an opencv@3 installation.
# If you edit the path here, you will also need to update the corresponding # If you edit the path here, you will also need to update the corresponding
# prefix in "opencv_macos.BUILD". # prefix in "opencv_macos.BUILD".
path = "/usr/local", path = "/opt/homebrew",
) )
new_local_repository( new_local_repository(

View File

@ -16,9 +16,10 @@ licenses(["notice"])
package(default_visibility = ["//mediapipe/examples:__subpackages__"]) package(default_visibility = ["//mediapipe/examples:__subpackages__"])
cc_binary( cc_library(
name = "hello_world", name = "hello_world_library",
srcs = ["hello_world.cc"], srcs = ["hello_world.cc"],
hdrs = ["hello_world.hh"],
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [ deps = [
"//mediapipe/calculators/core:pass_through_calculator", "//mediapipe/calculators/core:pass_through_calculator",
@ -28,3 +29,11 @@ cc_binary(
"//mediapipe/framework/port:status", "//mediapipe/framework/port:status",
], ],
) )
cc_binary(
name = "hello-world-binary",
srcs = ["hello-world-main.cc"],
deps = [
":hello_world_library",
],
)

View File

@ -0,0 +1,7 @@
#include "hello_world.hh"
int main(int argc, char **argv)
{
theFunctionToCall(argv[0]);
return 0;
}

View File

@ -14,17 +14,18 @@
// //
// A simple example to print out "Hello World!" from a MediaPipe graph. // A simple example to print out "Hello World!" from a MediaPipe graph.
#include "hello_world.hh"
#include "mediapipe/framework/calculator_graph.h" #include "mediapipe/framework/calculator_graph.h"
#include "mediapipe/framework/port/logging.h" #include "mediapipe/framework/port/logging.h"
#include "mediapipe/framework/port/parse_text_proto.h" #include "mediapipe/framework/port/parse_text_proto.h"
#include "mediapipe/framework/port/status.h" #include "mediapipe/framework/port/status.h"
namespace mediapipe { absl::Status PrintHelloWorld()
{
absl::Status PrintHelloWorld() {
// Configures a simple graph, which concatenates 2 PassThroughCalculators. // Configures a simple graph, which concatenates 2 PassThroughCalculators.
CalculatorGraphConfig config = mediapipe::CalculatorGraphConfig config =
ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( mediapipe::ParseTextProtoOrDie<mediapipe::CalculatorGraphConfig>(R"pb(
input_stream: "in" input_stream: "in"
output_stream: "out" output_stream: "out"
node { node {
@ -39,29 +40,32 @@ absl::Status PrintHelloWorld() {
} }
)pb"); )pb");
CalculatorGraph graph; mediapipe::CalculatorGraph graph;
MP_RETURN_IF_ERROR(graph.Initialize(config)); MP_RETURN_IF_ERROR(graph.Initialize(config));
ASSIGN_OR_RETURN(OutputStreamPoller poller, ASSIGN_OR_RETURN(mediapipe::OutputStreamPoller poller,
graph.AddOutputStreamPoller("out")); graph.AddOutputStreamPoller("out"));
MP_RETURN_IF_ERROR(graph.StartRun({})); MP_RETURN_IF_ERROR(graph.StartRun({}));
// Give 10 input packets that contains the same string "Hello World!". // Give 10 input packets that contains the same string "Hello World!".
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i)
{
MP_RETURN_IF_ERROR(graph.AddPacketToInputStream( MP_RETURN_IF_ERROR(graph.AddPacketToInputStream(
"in", MakePacket<std::string>("Hello World!").At(Timestamp(i)))); "in", mediapipe::MakePacket<std::string>("Hello World!").At(mediapipe::Timestamp(i))));
} }
// Close the input stream "in". // Close the input stream "in".
MP_RETURN_IF_ERROR(graph.CloseInputStream("in")); MP_RETURN_IF_ERROR(graph.CloseInputStream("in"));
mediapipe::Packet packet; mediapipe::Packet packet;
// Get the output packets string. // Get the output packets string.
while (poller.Next(&packet)) { while (poller.Next(&packet))
{
LOG(INFO) << packet.Get<std::string>(); LOG(INFO) << packet.Get<std::string>();
} }
return graph.WaitUntilDone(); return graph.WaitUntilDone();
} }
} // namespace mediapipe
int main(int argc, char** argv) { void theFunctionToCall(const char* argv0)
google::InitGoogleLogging(argv[0]); {
CHECK(mediapipe::PrintHelloWorld().ok()); google::InitGoogleLogging(argv0);
return 0; CHECK(PrintHelloWorld().ok());
} }

View File

@ -0,0 +1,3 @@
#pragma once
void theFunctionToCall(const char* argv0);