Skip to content

Commit

Permalink
Added code to free up the image buffer and included a test to verify it
Browse files Browse the repository at this point in the history
  • Loading branch information
kinarr committed Jan 9, 2024
1 parent 8c46ce2 commit 9a8259b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
10 changes: 10 additions & 0 deletions mediapipe/tasks/c/vision/pose_landmarker/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,24 @@ cc_library(
cc_test(
name = "pose_landmarker_result_converter_test",
srcs = ["pose_landmarker_result_converter_test.cc"],
data = [
"//mediapipe/framework/formats:image_frame_opencv",
"//mediapipe/framework/port:opencv_core",
"//mediapipe/framework/port:opencv_imgproc",
"//mediapipe/tasks/testdata/vision:test_images",
],
linkstatic = 1,
deps = [
":pose_landmarker_result",
":pose_landmarker_result_converter",
"//mediapipe/framework/port:gtest",
"//mediapipe/framework/port:gtest_main",
"//mediapipe/framework/formats:image",
"//mediapipe/framework/deps:file_path",
"//mediapipe/tasks/c/components/containers:landmark",
"//mediapipe/tasks/cc/components/containers:landmark",
"//mediapipe/tasks/cc/vision/pose_landmarker:pose_landmarker_result",
"//mediapipe/tasks/cc/vision/utils:image_utils",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ void CppConvertToPoseLandmarkerResult(

void CppClosePoseLandmarkerResult(PoseLandmarkerResult* result) {
if (result->segmentation_masks) {
for (uint32_t i = 0; i < result->segmentation_masks_count; ++i) {
if (result->segmentation_masks[i].type == MpImage::IMAGE_FRAME) {
const uint8_t* buffer =
result->segmentation_masks[i].image_frame.image_buffer;
}
// TODO: Add similar cleanup for GPU_BUFFER later
}
delete[] result->segmentation_masks;
result->segmentation_masks = nullptr;
result->segmentation_masks_count = 0;
}

for (uint32_t i = 0; i < result->pose_landmarks_count; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ limitations under the License.

#include <cstdint>

#include "mediapipe/framework/deps/file_path.h"
#include "mediapipe/framework/formats/image.h"
#include "mediapipe/framework/port/gtest.h"
#include "mediapipe/framework/port/status_matchers.h"
#include "mediapipe/tasks/c/components/containers/landmark.h"
#include "mediapipe/tasks/c/vision/pose_landmarker/pose_landmarker_result.h"
#include "mediapipe/tasks/cc/components/containers/landmark.h"
#include "mediapipe/tasks/cc/vision/pose_landmarker/pose_landmarker_result.h"
#include "mediapipe/tasks/cc/vision/utils/image_utils.h"

namespace mediapipe::tasks::c::components::containers {

using ::mediapipe::file::JoinPath;
using ::mediapipe::tasks::vision::DecodeImageFromFile;

constexpr char kTestDataDirectory[] = "/mediapipe/tasks/testdata/vision/";
constexpr char kMaskImage[] = "segmentation_input_rotation0.jpg";

void InitPoseLandmarkerResult(
::mediapipe::tasks::vision::pose_landmarker::PoseLandmarkerResult*
cpp_result) {
Expand All @@ -46,6 +56,17 @@ void InitPoseLandmarkerResult(
mediapipe::tasks::components::containers::Landmarks cpp_landmarks;
cpp_landmarks.landmarks.push_back(cpp_landmark);
cpp_result->pose_world_landmarks.push_back(cpp_landmarks);

// Initialize segmentation_masks
MP_ASSERT_OK_AND_ASSIGN(
Image mask_image,
DecodeImageFromFile(JoinPath("./", kTestDataDirectory, kMaskImage)));

// Ensure segmentation_masks is instantiated and add the mask_image to it.
if (!cpp_result->segmentation_masks) {
cpp_result->segmentation_masks.emplace();
}
cpp_result->segmentation_masks->push_back(mask_image);
}

TEST(PoseLandmarkerResultConverterTest, ConvertsCustomResult) {
Expand Down Expand Up @@ -102,11 +123,13 @@ TEST(PoseLandmarkerResultConverterTest, FreesMemory) {

EXPECT_NE(c_result.pose_landmarks, nullptr);
EXPECT_NE(c_result.pose_world_landmarks, nullptr);
EXPECT_NE(c_result.segmentation_masks, nullptr);

CppClosePoseLandmarkerResult(&c_result);

EXPECT_EQ(c_result.pose_landmarks, nullptr);
EXPECT_EQ(c_result.pose_world_landmarks, nullptr);
EXPECT_EQ(c_result.segmentation_masks, nullptr);
}

} // namespace mediapipe::tasks::c::components::containers

0 comments on commit 9a8259b

Please sign in to comment.