mediapipe 0.8.9 用holistic_tracking_cpu.pbtxt输出右肩坐标 c++
时间: 2024-02-17 21:01:30 浏览: 245
在 C++ 中使用 Holistic 模型输出右肩坐标的示例代码如下:
```c++
#include <iostream>
#include <vector>
#include "mediapipe/framework/formats/image_frame.h"
#include "mediapipe/framework/formats/landmark.pb.h"
#include "mediapipe/framework/formats/landmark_list.pb.h"
#include "mediapipe/framework/port/opencv_highgui_inc.h"
#include "mediapipe/framework/port/opencv_imgproc_inc.h"
#include "mediapipe/framework/port/status.h"
#include "mediapipe/framework/port/statusor.h"
#include "mediapipe/framework/tool/options_util.h"
#include "mediapipe/framework/tool/status_util.h"
#include "mediapipe/framework/calculator_framework.h"
#include "mediapipe/framework/calculator_graph.h"
#include "mediapipe/framework/formats/matrix.h"
#include "mediapipe/framework/formats/matrix_data.pb.h"
#include "mediapipe/framework/formats/rect.pb.h"
#include "mediapipe/framework/formats/rect.pb.h"
#include "mediapipe/framework/formats/classification.pb.h"
#include "mediapipe/framework/formats/detection.pb.h"
#include "mediapipe/framework/formats/image_frame_opencv.h"
#include "mediapipe/framework/formats/matrix.h"
#include "mediapipe/framework/formats/matrix_data.pb.h"
#include "mediapipe/framework/formats/matrix_opencv.h"
#include "mediapipe/framework/formats/rect.pb.h"
#include "mediapipe/framework/formats/rect.pb.h"
#include "mediapipe/framework/formats/classification.pb.h"
#include "mediapipe/framework/formats/detection.pb.h"
#include "mediapipe/framework/formats/image_frame_opencv.h"
#include "mediapipe/framework/formats/rect.pb.h"
#include "mediapipe/framework/formats/rect.pb.h"
#include "mediapipe/framework/formats/classification.pb.h"
#include "mediapipe/framework/formats/detection.pb.h"
#include "mediapipe/framework/formats/image_frame_opencv.h"
#include "mediapipe/framework/formats/rect.pb.h"
#include "mediapipe/framework/formats/rect.pb.h"
#include "mediapipe/framework/formats/classification.pb.h"
#include "mediapipe/framework/formats/detection.pb.h"
#include "mediapipe/framework/formats/image_frame_opencv.h"
#include "mediapipe/framework/port/opencv_highgui_inc.h"
#include "mediapipe/framework/port/opencv_imgcodecs_inc.h"
#include "mediapipe/framework/port/opencv_video_inc.h"
#include "mediapipe/framework/port/opencv_videoio_inc.h"
#include "mediapipe/util/resource_util.h"
constexpr char kInputStream[] = "input_video";
constexpr char kOutputStream[] = "output_video";
constexpr char kLandmarksStream[] = "pose_landmarks";
constexpr char kWindowName[] = "MediaPipe";
using namespace mediapipe;
int main() {
// 初始化计算图
CalculatorGraphConfig config =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"(
input_stream: "input_video"
output_stream: "output_video"
node {
calculator: "HolisticTrackingCalculator"
input_stream: "IMAGE:input_video"
output_stream: "IMAGE:output_video"
output_stream: "POSE_LANDMARKS:pose_landmarks"
node_options: {
[type.googleapis.com/mediapipe.HolisticTrackingCalculatorOptions] {
min_detection_confidence: 0.5
min_tracking_confidence: 0.5
}
}
}
)");
CalculatorGraph graph;
MP_RETURN_IF_ERROR(graph.Initialize(config));
// 打开摄像头或者视频文件
cv::VideoCapture capture;
capture.open(0);
cv::namedWindow(kWindowName, cv::WINDOW_NORMAL);
cv::resizeWindow(kWindowName, 720, 480);
// 处理帧数据
while (capture.isOpened()) {
bool grabbed = capture.grab();
if (!grabbed) break;
cv::Mat input_frame;
capture.retrieve(input_frame, cv::CAP_PROP_CONVERT_RGB);
// 将 OpenCV 的 Mat 数据转换成 MediaPipe 的 ImageFrame 数据
auto input_frame_mat = absl::make_unique<cv::Mat>(input_frame);
auto input_frame_image =
absl::make_unique<ImageFrame>(ImageFormat::SRGB, input_frame.cols,
input_frame.rows, ImageFrame::kDefaultAlignmentBoundary);
cv::Mat input_frame_mat_bgr;
cv::cvtColor(input_frame, input_frame_mat_bgr, cv::COLOR_RGB2BGR);
cv::Mat input_frame_mat_bgr_flipped;
cv::flip(input_frame_mat_bgr, input_frame_mat_bgr_flipped, /*flipcode=*/1);
cv::Mat input_frame_mat_bgr_flipped_aligned;
cv::Mat temp_output_frame = cv::Mat::zeros(input_frame_mat_bgr_flipped.rows, input_frame_mat_bgr_flipped.cols, input_frame_mat_bgr_flipped.type());
cv::rotate(input_frame_mat_bgr_flipped, temp_output_frame, cv::ROTATE_90_COUNTERCLOCKWISE);
cv::rotate(temp_output_frame, input_frame_mat_bgr_flipped_aligned, cv::ROTATE_180);
cv::Mat input_frame_mat_aligned;
cv::cvtColor(input_frame_mat_bgr_flipped_aligned, input_frame_mat_aligned, cv::COLOR_BGR2RGB);
memcpy(input_frame_image->MutablePixelData(), input_frame_mat_aligned.data,
input_frame_mat_aligned.total() * input_frame_mat_aligned.elemSize());
input_frame_image->SetColorSpace(ImageFrame::ColorSpace::SRGB);
input_frame_image->set_timestamp(Timestamp(capture.get(cv::CAP_PROP_POS_MSEC) * 1000));
// 向计算图输入数据
MP_RETURN_IF_ERROR(graph.AddPacketToInputStream(
kInputStream, Adopt(input_frame_image.release()).At(Timestamp(capture.get(cv::CAP_PROP_POS_MSEC) * 1000))));
// 获取输出结果
mediapipe::Packet pose_landmarks_packet;
if (graph.GetOutputLandmarkList(kLandmarksStream, &pose_landmarks_packet)) {
auto& pose_landmarks = pose_landmarks_packet.Get<mediapipe::NormalizedLandmarkList>();
if (pose_landmarks.landmark_size() > 0) {
// 获取右肩坐标
auto right_shoulder = pose_landmarks.landmark(11);
std::cout << "Right shoulder coordinate: (" << right_shoulder.x() << ", " << right_shoulder.y() << ", " << right_shoulder.z() << ")" << std::endl;
}
}
// 获取输出图像
mediapipe::Packet output_packet;
if (graph.GetOutputPacket(&output_packet, kOutputStream) && !output_packet.IsEmpty()) {
auto& output_frame = output_packet.Get<mediapipe::ImageFrame>();
cv::Mat output_mat = mediapipe::formats::MatView(&output_frame);
cv::Mat output_mat_bgr;
cv::cvtColor(output_mat, output_mat_bgr, cv::COLOR_RGB2BGR);
cv::Mat output_mat_bgr_flipped;
cv::flip(output_mat_bgr, output_mat_bgr_flipped, /*flipcode=*/0);
cv::imshow(kWindowName, output_mat_bgr_flipped);
}
if (cv::waitKey(5) == 27) break;
}
// 关闭计算图
MP_RETURN_IF_ERROR(graph.CloseInputStream(kInputStream));
return graph.WaitUntilDone();
}
```
需要注意的是,Holistic 模型输出的坐标值是归一化的坐标值,范围在[0, 1]之间。如果需要将其转换为图像坐标或者其他坐标系的坐标值,需要进行相应的转换。
阅读全文