google-ai-edge / mediapipe

Cross-platform, customizable ML solutions for live and streaming media.
https://ai.google.dev/edge/mediapipe
Apache License 2.0
27.52k stars 5.15k forks source link

examples pose_tracking_cpu in some frame would stop in "if (!poller_mark.Next(&packet_mark))" #4224

Closed fishboyzyf closed 1 year ago

fishboyzyf commented 1 year ago

Have I written custom code (as opposed to using a stock example script provided in MediaPipe)

No

OS Platform and Distribution

window 10 桌面

Mobile device if the issue happens on mobile device

No response

Browser and version if the issue happens on browser

No response

Programming Language and version

vc 2019 python 3.9.10

MediaPipe version

No response

Bazel version

No response

Solution

mediapipe 0.9

Android Studio, NDK, SDK versions (if issue is related to building in Android environment)

No response

Xcode & Tulsi version (if issue is related to building for iOS)

No response

Describe the actual behavior

In same video ,for demo ,it would work out.But I want get pose_landmark,
add "constexpr char kOutputStream_point[] = "pose_landmarks";"

" ASSIGN_OR_RETURN(mediapipe::OutputStreamPoller poller_mark,
graph.AddOutputStreamPoller(kOutputStream_point));"
mediapipe::Packet packet,packet_mark;

"if (!poller_mark.Next(&packet_mark)) break;" in demo.
in some frame ,it would stop in "if (!poller_mark.Next(&packet_mark)) break;"
and ,it never break, just stop.

Describe the expected behaviour

"There are faces in the video, and the operation can be successful,",

If there is no face in the video, it will stop at this position

Standalone code/steps you may have used to try to get what you need

"There are faces in the video, and the operation can be successful,",

If there is no face in the video, it will stop at this position

Other info / Complete Logs

No response

kuaashish commented 1 year ago

Hi @fishboyzyf, Could you please let us know the complete details such as complete steps you have followed, Standalone code to reproduce the issue from our end to share the possible solution on query raised. thank you!

fishboyzyf commented 1 year ago

// Copyright 2019 The MediaPipe Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // An example of sending OpenCV webcam frames into a MediaPipe graph.

include

include "absl/flags/flag.h"

include "absl/flags/parse.h"

include "mediapipe/framework/calculator_framework.h"

include "mediapipe/framework/formats/image_frame.h"

include "mediapipe/framework/formats/image_frame_opencv.h"

include "mediapipe/framework/port/file_helpers.h"

include "mediapipe/framework/port/opencv_highgui_inc.h"

include "mediapipe/framework/port/opencv_imgproc_inc.h"

include "mediapipe/framework/port/opencv_video_inc.h"

include "mediapipe/framework/port/parse_text_proto.h"

include "mediapipe/framework/port/status.h"

include "mediapipe/framework/formats/detection.pb.h"

include "mediapipe/framework/formats/landmark.pb.h"

include "mediapipe/framework/formats/rect.pb.h"

constexpr char kInputStream[] = "input_video"; constexpr char kOutputStream[] = "output_video"; constexpr char kFaceOutput[] = "multi_face_landmarks"; constexpr char kWindowName[] = "MediaPipe";

ABSL_FLAG(std::string, calculator_graph_config_file, "", "Name of file containing text format CalculatorGraphConfig proto."); ABSL_FLAG(std::string, input_video_path, "", "Full path of video to load. " "If not provided, attempt to use a webcam."); ABSL_FLAG(std::string, output_video_path, "", "Full path of where to save result (.mp4 only). " "If not provided, show result in a window.");

absl::Status RunMPPGraph() { std::string calculator_graph_config_contents; std::cout <<"-----" << absl::GetFlag(FLAGS_calculator_graph_config_file) << std::endl; MP_RETURN_IF_ERROR(mediapipe::file::GetContents( absl::GetFlag(FLAGS_calculator_graph_config_file), &calculator_graph_config_contents)); LOG(ERROR) << "Get calculator graph config contents: " << calculator_graph_config_contents; mediapipe::CalculatorGraphConfig config = mediapipe::ParseTextProtoOrDie( calculator_graph_config_contents);

LOG(ERROR) << "Initialize the calculator graph."; mediapipe::CalculatorGraph graph; MP_RETURN_IF_ERROR(graph.Initialize(config));

LOG(ERROR) << "Initialize the camera or load the video."; cv::VideoCapture capture; std::cout << "-----" << absl::GetFlag(FLAGS_input_video_path) << std::endl; const bool load_video = !absl::GetFlag(FLAGS_input_video_path).empty(); if (load_video) { capture.open(absl::GetFlag(FLAGS_input_video_path)); } else { capture.open(0); } RET_CHECK(capture.isOpened());

std::cout << "-----" << absl::GetFlag(FLAGS_output_video_path) << std::endl; cv::VideoWriter writer; const bool save_video = !absl::GetFlag(FLAGS_output_video_path).empty(); if (!save_video) { cv::namedWindow(kWindowName, /flags=WINDOW_AUTOSIZE/ 1);

if (CV_MAJOR_VERSION >= 3) && (CV_MINOR_VERSION >= 2)

capture.set(cv::CAP_PROP_FRAME_WIDTH, 640);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
capture.set(cv::CAP_PROP_FPS, 30);

endif

}

LOG(ERROR) << "Start running the calculator graph."; ASSIGN_OR_RETURN(mediapipe::OutputStreamPoller poller, graph.AddOutputStreamPoller(kOutputStream));

ASSIGN_OR_RETURN(mediapipe::OutputStreamPoller facemesh, graph.AddOutputStreamPoller(kFaceOutput));

MP_RETURN_IF_ERROR(graph.StartRun({}));

/ mediapipe::StatusOrPoller faceLandmarks = graph.AddOutputStreamPoller("multi_face_landmarks"); assert(faceLandmarks.ok()); std::unique_ptr m_pFaceLandmarksPoller = std::make_unique(std::move(faceLandmarks.value())); LOG(ERROR) << "Empty frame, end of video reached."; / LOG(ERROR) << "Start grabbing and processing frames."; bool grab_frames = true; while (grab_frames) { // Capture opencv camera or video frame. cv::Mat camera_frame_raw; capture >> camera_frame_raw; if (camera_frame_raw.empty()) { if (!load_video) { LOG(ERROR) << "Ignore empty frames from camera."; continue; } LOG(ERROR) << "Empty frame, end of video reached."; break; } cv::Mat camera_frame; cv::cvtColor(camera_frame_raw, camera_frame, cv::COLOR_BGR2RGB); if (!load_video) { cv::flip(camera_frame, camera_frame, /flipcode=HORIZONTAL/ 1); }

LOG(INFO) << "Start grabbing and processing frames.";
// Wrap Mat into an ImageFrame.
auto input_frame = absl::make_unique<mediapipe::ImageFrame>(
    mediapipe::ImageFormat::SRGB, camera_frame.cols, camera_frame.rows,
    mediapipe::ImageFrame::kDefaultAlignmentBoundary);
cv::Mat input_frame_mat = mediapipe::formats::MatView(input_frame.get());
camera_frame.copyTo(input_frame_mat);

// Send image packet into the graph.
size_t frame_timestamp_us =
    (double)cv::getTickCount() / (double)cv::getTickFrequency() * 1e6;
MP_RETURN_IF_ERROR(graph.AddPacketToInputStream(
    kInputStream, mediapipe::Adopt(input_frame.release())
                      .At(mediapipe::Timestamp(frame_timestamp_us))));

LOG(ERROR) << "Start grabbing and processing frames.";
mediapipe::Packet faceLandmarksPacket;
if (!facemesh.Next(&faceLandmarksPacket)) break;
LOG(ERROR) << "Empty frame, end of video reached.";
auto& output_landmarks = faceLandmarksPacket.Get<std::vector<mediapipe::NormalizedLandmarkList>>();
std::cout << "FaceLandmarks size:" << output_landmarks.size() << std::endl;

/* */
cv::Mat face = camera_frame.clone();
LOG(ERROR) << "Empty frame, end of video reached." << output_landmarks.size();
for (auto& face_mesk_points : output_landmarks) {
    for (int i = 0; i < face_mesk_points.landmark_size(); ++i)
    {
        const mediapipe::NormalizedLandmark landmark = face_mesk_points.landmark(i);
        float x = landmark.x() * camera_frame.cols;
        float y = landmark.y() * camera_frame.rows;
        std::cout << "point X:" << x << " Y:" << y << std::endl;
        cv::circle(face, cv::Point(x, y), 2, cv::Scalar::all(255), 1);
        cv::putText(face, std::to_string(i), cv::Point(x, y), 1, 1, cv::Scalar::all(255));
    }
    std::cout << "landmark_size:" << face_mesk_points.landmark_size() << std::endl;
}
cv::imwrite("c:/1.jpg", face);

exit(0);

LOG(INFO) << "Start grabbing and processing frames.";
// Get the graph result packet, or stop if that fails.
mediapipe::Packet packet;
if (!poller.Next(&packet)) break;
LOG(INFO) << "Start grabbing and processing frames.";
auto& output_frame = packet.Get<mediapipe::ImageFrame>();
LOG(INFO) << "Start grabbing and processing frames.";
// Convert back to opencv for display or saving.
cv::Mat output_frame_mat = mediapipe::formats::MatView(&output_frame);
cv::cvtColor(output_frame_mat, output_frame_mat, cv::COLOR_RGB2BGR);

if (save_video) {
  if (!writer.isOpened()) {
    LOG(ERROR) << "Prepare video writer.";
    writer.open(absl::GetFlag(FLAGS_output_video_path),
                mediapipe::fourcc('a', 'v', 'c', '1'),  // .mp4
                capture.get(cv::CAP_PROP_FPS), output_frame_mat.size());
    RET_CHECK(writer.isOpened());
  }
  writer.write(output_frame_mat);
} else {
  cv::imshow(kWindowName, output_frame_mat);
  // Press any key to exit.
  const int pressed_key = cv::waitKey(5);
  if (pressed_key >= 0 && pressed_key != 255) grab_frames = false;
}

}

LOG(ERROR) << "Shutting down."; if (writer.isOpened()) writer.release(); MP_RETURN_IF_ERROR(graph.CloseInputStream(kInputStream)); return graph.WaitUntilDone(); }

int main(int argc, char** argv) { google::InitGoogleLogging(argv[0]); absl::ParseCommandLine(argc, argv); absl::Status run_status = RunMPPGraph(); if (!run_status.ok()) { LOG(ERROR) << "Failed to run the graph: " << run_status.message(); return EXIT_FAILURE; } else { LOG(ERROR) << "Success!"; } return EXIT_SUCCESS; }

fishboyzyf commented 1 year ago

mediapipe::StatusOrPoller faceLandmarks = m_Graph.AddOutputStreamPoller(m_OutputStreamName,true);

// 5 FaceLandmarks
mediapipe::Packet faceLandmarksPacket;
if (!m_pFaceLandmarksPoller->Next(&faceLandmarksPacket)) {
    return absl::InvalidArgumentError("no next packet");
}
if (faceLandmarksPacket.IsEmpty()) {
    return absl::InvalidArgumentError("no next packet");
}
google-ml-butler[bot] commented 1 year ago

Are you satisfied with the resolution of your issue? Yes No