Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
novabyte-games opened this issue Apr 17, 2024 · 6 comments
Assignees
Labels
os:windows MediaPipe issues on Windows platform:python MediaPipe Python issues task:image segmentation Issues related to image segmentation: Locate objects and create image masks with labels type:support General questions

Comments

@novabyte-games
Copy link

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

@kuaashish kuaashish assigned kuaashish and unassigned ayushgdev Apr 17, 2024
@kuaashish kuaashish added os:windows MediaPipe issues on Windows task:image segmentation Issues related to image segmentation: Locate objects and create image masks with labels platform:python MediaPipe Python issues type:support General questions labels Apr 17, 2024
@kuaashish
Copy link
Collaborator

kuaashish commented Apr 17, 2024

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 kuaashish added the stat:awaiting response Waiting for user response label Apr 17, 2024
@novabyte-games
Copy link
Author

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

@google-ml-butler google-ml-butler bot removed the stat:awaiting response Waiting for user response label Apr 17, 2024
@kuaashish
Copy link
Collaborator

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 kuaashish added the stat:awaiting response Waiting for user response label Apr 18, 2024
Copy link

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 github-actions bot added the stale label Apr 26, 2024
Copy link

github-actions bot commented May 3, 2024

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

@github-actions github-actions bot closed this as completed May 3, 2024
Copy link

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

@kuaashish kuaashish removed stat:awaiting response Waiting for user response stale labels May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
os:windows MediaPipe issues on Windows platform:python MediaPipe Python issues task:image segmentation Issues related to image segmentation: Locate objects and create image masks with labels type:support General questions
Projects
None yet
Development

No branches or pull requests

3 participants