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.69k stars 5.18k forks source link

What is the cause of the rising internal sub-occupancy rate found after starting to reason during use? #5731

Open jhlibra opened 1 week ago

jhlibra commented 1 week ago

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

None

OS Platform and Distribution

win10

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

c++

MediaPipe version

10.15

Bazel version

6.10

Solution

Holistic

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

What is the cause of the rising internal sub-occupancy rate found after starting to reason during use? Is it because the previous video stream was not cleaned? Is there any documentation on how to clear the video streaming data? Also how do I reopen the video stream when I close it?

Describe the expected behaviour

There are presentations corresponding to this section

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

absl::Status GoogleMediapipeDetect::HolisticTrackingDetect::Mediapipe_RunMPPGraph_Direct(
    int image_width,
    int image_height,
    void* image_data,
    std::map<int ,FingerPos_T>& facepoint,
    std::map<int ,FingerPos_T>& rightpoint,
    std::map<int ,FingerPos_T>& leftpoint,
    bool show_result_image
)
{
    facepoint.clear();
    rightpoint.clear();
    leftpoint.clear();
    // 等待图处于空闲状态
    //m_Graph.WaitUntilIdle();

    /*----- 1 构造cv::Mat对象 -----*/
    cv::Mat camera_frame(cv::Size(image_width, image_height), CV_8UC3, (uchar*)image_data);

    // BGR转RGB
    cv::Mat camera_frame_RGB;
    cv::cvtColor(camera_frame, camera_frame_RGB, cv::COLOR_BGR2RGB);

    // 水平翻转
    //cv::flip(camera_frame_RGB, camera_frame_RGB, 1);

    //std::cout << "cv::Mat对象构建完成" << std::endl;

    /*----- 2 将OpenCV Mat转换为ImageFrame -----*/
    auto input_frame = absl::make_unique<mediapipe::ImageFrame>(mediapipe::ImageFormat::SRGB, camera_frame_RGB.cols, camera_frame_RGB.rows,mediapipe::ImageFrame::kDefaultAlignmentBoundary);
    //camera_frame_RGB.copyTo(mediapipe::formats::MatView(input_frame.get()));
    cv::Mat input_frame_mat = mediapipe::formats::MatView(input_frame.get());
    camera_frame_RGB.copyTo(input_frame_mat);

    //std::cout << "将OpenCV Mat转换为ImageFrame完成" << std::endl;

    /*----- 3 发送图片到图中推理 -----*/
    // 获取当前距离1970的微秒时间
    size_t frame_timestamp_us = 0;
    size_t frame_timestamp_us =(double)cv::getTickCount() / (double)cv::getTickFrequency() * 1e6;

    // std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds> tpMicro
    //     = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
    // size_t frame_timestamp_us = tpMicro.time_since_epoch().count();

    MP_RETURN_IF_ERROR(m_Graph.AddPacketToInputStream(m_Video_InputStreamName, mediapipe::Adopt(input_frame.release()).At(mediapipe::Timestamp(frame_timestamp_us))));

    //std::cout << "发送图片到图中推理完成" << std::endl;

    /*----- 4 得到结果 -----*/
    // 1 视频输出结果帧

    // 2 PoseLandmarks

    // 3 FaceLandmarks
    if (m_pFaceLandmarksPoller != nullptr)
    {
        mediapipe::Packet faceLandmarksPacket;
        if (m_pFaceLandmarksPoller->QueueSize() != 0)
        {
            if (m_pFaceLandmarksPoller->Next(&faceLandmarksPacket))
            {
                auto& output_landmarks = faceLandmarksPacket.Get<mediapipe::NormalizedLandmarkList>();
                FingerPos_T tempPoint2D;
                const mediapipe::NormalizedLandmark landmark = output_landmarks.landmark(4);
                float x = landmark.x() * camera_frame.cols;
                float y = landmark.y() * camera_frame.rows;

                //std::cout << x << "|" << y << std::endl;
                tempPoint2D.id =0;
                tempPoint2D.x = x;
                tempPoint2D.y = y;
                facepoint.insert(std::make_pair(0, tempPoint2D));

                //计算脸宽
                const mediapipe::NormalizedLandmark left = output_landmarks.landmark(127);
                float leftx = left.x() * camera_frame.cols;
                float lefty = left.y() * camera_frame.rows;

                const mediapipe::NormalizedLandmark right = output_landmarks.landmark(356);
                float rightx = right.x() * camera_frame.cols;
                float righty = right.y() * camera_frame.rows;

                int fw = sqrt(pow(leftx - rightx, 2) + pow(lefty - righty, 2));

                const mediapipe::NormalizedLandmark top = output_landmarks.landmark(10);
                float topx = top.x() * camera_frame.cols;
                float topy = top.y() * camera_frame.rows;

                const mediapipe::NormalizedLandmark bottom = output_landmarks.landmark(152);
                float bottomx = bottom.x() * camera_frame.cols;
                float bottomy = bottom.y() * camera_frame.rows;

                int fh = sqrt(pow(topx - bottomx, 2) + pow(topy - bottomy, 2));
                tempPoint2D.id =1;
                tempPoint2D.x = fw;
                tempPoint2D.y = fh;
                facepoint.insert(std::make_pair(1, tempPoint2D));

            }
        }
    }

     //4 RightHandLandmarks
    if (m_pRightHandLandmarksPoller != nullptr)
    {
        mediapipe::Packet rightHandLandmarksPacket;
        if (m_pRightHandLandmarksPoller->QueueSize() != 0)
        {
            if (m_pRightHandLandmarksPoller->Next(&rightHandLandmarksPacket))
            {
                auto& output_landmarks = rightHandLandmarksPacket.Get<mediapipe::NormalizedLandmarkList>();
                for (int i = 0; i < output_landmarks.landmark_size(); ++i)
                {
                    FingerPos_T  tempPoint2D;
                    const mediapipe::NormalizedLandmark landmark = output_landmarks.landmark(i);
                    float x = landmark.x() * camera_frame.cols;
                    float y = landmark.y() * camera_frame.rows;
                    //tempPoint2D.type = 'R';
                    //tempPoint2D.id = i + 2;
                    tempPoint2D.id =i;
                    tempPoint2D.x = x;
                    tempPoint2D.y = y;
                    rightpoint.insert(std::make_pair(i, tempPoint2D));

                }
            }
        }
    }

     //5 LeftHandLandmarks
    if (m_pLeftHandLandmarksPoller != nullptr)
    {
        mediapipe::Packet leftHandLandmarksPacket;
        if (m_pLeftHandLandmarksPoller->QueueSize() != 0)
        {
            if (m_pLeftHandLandmarksPoller->Next(&leftHandLandmarksPacket))
            {
                auto& output_landmarks = leftHandLandmarksPacket.Get<mediapipe::NormalizedLandmarkList>();
                for (int i = 0; i < output_landmarks.landmark_size(); ++i)
                {
                    FingerPos_T tempPoint2D;
                    const mediapipe::NormalizedLandmark landmark = output_landmarks.landmark(i);
                    float x = landmark.x() * camera_frame.cols;
                    float y = landmark.y() * camera_frame.rows;
                    tempPoint2D.id =i;
                    tempPoint2D.x = x;
                    tempPoint2D.y = y;
                    leftpoint.insert(std::make_pair(i, tempPoint2D));

                }
            }
        }
    }

    //m_Graph.WaitUntilIdle();
    //m_Graph.CloseAllPacketSources();
    return absl::OkStatus();
}

absl::Status GoogleMediapipeDetect::HolisticTrackingDetect::Mediapipe_ReleaseGraph()
{
    MP_RETURN_IF_ERROR(m_Graph.CloseInputStream(m_Video_InputStreamName));
    return m_Graph.WaitUntilDone();
}

Other info / Complete Logs

No response