Mediapipe python crashes script

1 day ago 4
ARTICLE AD BOX

I have a mediapipe script that crashes causing python itself to crash, here's the basic script:

img = cv2.imread(os.sep.join(["test-imgs", path])) rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) mp_img = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb) obj_res = obj_detector.detect(mp_img) mp_cropped = None for idx, human in enumerate([dt for dt in obj_res.detections if len([ct for ct in dt.categories if ct.category_name == "person"]) > 0]): x1, x2 = human.bounding_box.origin_x, human.bounding_box.origin_x + human.bounding_box.width y1, y2 = human.bounding_box.origin_y, human.bounding_box.origin_y + human.bounding_box.height cropped = img[y1:y2, x1:x2] p = os.sep.join(["test-imgs", "-".join(["cropped", str(idx), path.split(".")[0]])]) + ".jpg" cv2.imwrite(p, cropped) cropped_rgb = cv2.cvtColor(np.ascontiguousarray(cropped), cv2.COLOR_BGR2RGB) mp_cropped = mp.Image(image_format=mp.ImageFormat.SRGB, data=cropped_rgb) pos_res = pose_detector.detect(mp_cropped) if pos_res.segmentation_masks is None: print(f"img: {path}-{idx}: 0") continue print(f"img: {path}-{idx} {len(pos_res.segmentation_masks)} ~ {cropped.shape}") for ix0, seg in enumerate(pos_res.segmentation_masks): print(f"i: {ix0} {seg.width}x{seg.height} - {mp_cropped.numpy_view().shape} - {seg.channels} - {mp_cropped.is_contiguous()}") data = seg.numpy_view()

now the line that actually crashes this is data = seg.numpy_view(), the error that comes into terminal:

F0000 00:00:1771447031.969185 25790645 image_frame.cc:415] Check failed: 1 == ChannelSize() (1 vs. 4) *** Check failure stack trace: *** @ 0x130bcef5c absl::log_internal::LogMessage::SendToLog() @ 0x130bceef4 absl::log_internal::LogMessage::Flush() @ 0x130857d68 mediapipe::ImageFrame::CopyToBuffer() @ 0x12faa6e08 mediapipe::tasks::vision::core::GenerateContiguousDataArray<>() @ 0x12faa53cc mediapipe::tasks::vision::core::GetCachedContiguousDataAttr<>() @ 0x12faa52e8 MpImageDataFloat32 @ 0x1a38f0050 ffi_call_SYSV @ 0x1a38f9604 ffi_call_int @ 0x109de32f0 _ctypes_callproc @ 0x109de090c PyCFuncPtr_call @ 0x1052e63d8 _PyObject_MakeTpCall @ 0x10540ea18 _PyEval_EvalFrameDefault @ 0x10540c1ec PyEval_EvalCode @ 0x10547c7d4 run_eval_code_obj @ 0x10547c21c run_mod @ 0x10547aafc pyrun_file @ 0x105479e3c _PyRun_SimpleFileObject @ 0x105479ad0 _PyRun_AnyFileObject @ 0x1054a1738 pymain_run_file_obj @ 0x1054a13f4 pymain_run_file @ 0x1054a0600 Py_RunMain @ 0x1054a0c74 pymain_main @ 0x1054a0d14 Py_BytesMain @ 0x18f22dd54 start

This actually crashes the python runtime itself, I have already spent a few hours debugging this and the worst part is, I have no clue why its happening, AI agents have not been helpful either.

My goal is to run the following sequence of steps:

Detect objects in image (persons specifically) Crop that detected persons Using the pose landmarker, get segmentation of the person (Am using pose landmarker so as to further verify that the human detected is not a false positive) Extract the segmented person pixels and embed/store

I have actually hit a wall and would appreciate any help, if there is another way of doing this, am all ears, just that what's eating me alive is I cannot figure out what is the actual error here, any workarounds are also welcomed, thanks.

P.S. If I pass a non-cropped image to pose_landmarker, the error is not thrown

Read Entire Article