mediapipe icon indicating copy to clipboard operation
mediapipe copied to clipboard

Hair segmentation : ValueError: could not broadcast input array from shape (3,) into shape (337,325,4)

Open novabyte-games opened this issue 1 year ago • 4 comments
trafficstars

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

None

OS Platform and Distribution

Windows 11

MediaPipe Tasks SDK version

No response

Task name (e.g. Image classification, Gesture recognition etc.)

Hair segmentation

Programming Language and version (e.g. C++, Python, Java)

Python

Describe the actual behavior

ValueError: could not broadcast input array from shape (3,) into shape (337,325,4)

Describe the expected behaviour

should not give the error

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

import numpy as np
!pip install mediapipe
import mediapipe as mp
 
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
from mediapipe.python._framework_bindings import image as image_module
_Image = image_module.Image
from mediapipe.python._framework_bindings import image_frame
_ImageFormat = image_frame.ImageFormat
 
 
 
BG_COLOR = (192, 192, 192) # gray
MASK_COLOR = (255, 255, 255) # white
 
 
# Create the options that will be used for ImageSegmenter
base_options = python.BaseOptions(model_asset_path='hair.tflite')
options = vision.ImageSegmenterOptions(base_options=base_options,
                                       output_category_mask=True)
 
# Create the image segmenter
with vision.ImageSegmenter.create_from_options(options) as segmenter:
 
  # Loop through demo image(s)
  for image_file_name in IMAGE_FILENAMES:
 
    # Create the MediaPipe image file that will be segmented
   # image = mp.Image.create_from_file(image_file_name)
    # read image using OpenCV
    rgb_image = cv2.imread("john3.png")
    #cv2.imshow("rgb_image")
    rgba_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2RGBA)
    # set alpha channel to empty.
    rgba_image[:,:,3] = 0
    # create MP Image object from numpy array
    image = _Image(image_format=_ImageFormat.SRGBA, data=rgba_image)
    # Retrieve the masks for the segmented image
    segmentation_result = segmenter.segment(image)
    category_mask = segmentation_result.category_mask
 
    # Generate solid color images for showing the output segmentation mask.
    image_data = image.numpy_view()
    fg_image = np.zeros(image_data.shape, dtype=np.uint8)
    #fg_image[:] = MASK_COLOR
    fg_image[:, :, 0:3] = MASK_COLOR
    bg_image = np.zeros(image_data.shape, dtype=np.uint8)
    bg_image[:] = BG_COLOR
 
    condition = np.stack((category_mask.numpy_view(),) * 3, axis=-1) > 0.2
    output_image = np.where(condition, fg_image, bg_image)
 
    print(f'Segmentation mask of {name}:')
    resize_and_show(output_image)

Other info / Complete Logs

No response

novabyte-games avatar Apr 17 '24 09:04 novabyte-games

Hi @Avataryug-hs,

Could you please try using the provided Python working example? While creating the example, we encountered an issue. The scenario requires us to change the supported data type. Additionally, for hair segmentation, the number of channels should be set to 4. This adjustment is necessary to address the error you mentioned.

For better clarity, we suggest reviewing the thread. It will offer a comprehensive understanding of the context.

Thank you!!

kuaashish avatar Apr 17 '24 10:04 kuaashish

Example Provided has worked .. Thank you for your support .. probably error was because of channel mismatch ..

novabyte-games avatar Apr 17 '24 12:04 novabyte-games

Hi @Avataryug-hs,

Thank you for confirming. If there are no longer any outstanding issues on your end, may we proceed to mark the issue as resolved and close it?

kuaashish avatar Apr 18 '24 05:04 kuaashish

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

github-actions[bot] avatar Apr 26 '24 01:04 github-actions[bot]

This issue was closed due to lack of activity after being marked stale for past 7 days.

github-actions[bot] avatar May 03 '24 01:05 github-actions[bot]

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

google-ml-butler[bot] avatar May 03 '24 01:05 google-ml-butler[bot]