最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Why does a numpy transpose break mediapipe's Image command? - Stack Overflow

programmeradmin2浏览0评论

I would like to rotate an image in opencv before putting it into mediapipe. I took a transpose using numpy but, although the type is still numpy.uint8, mediapipe complains,

import cv2
import numpy as np
import mediapipe as mp

image_file_name = "IMG_0237.JPG"
cvImage = cv2.imread(image_file_name)
cvImage = np.transpose(cvImage, axes=[1,0,2]) #without this line code runs fine
print(cvImage.dtype)
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=cvImage)

returns,

uint8

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 9
      7 cvImage = np.transpose(cvImage, axes=[1,0,2]) #without this line code runs fine
      8 print(cvImage.dtype)
----> 9 mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=cvImage)

TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. mediapipe.python._framework_bindings.image.Image(image_format: mediapipe::ImageFormat_Format, data: numpy.ndarray[numpy.uint8])
    2. mediapipe.python._framework_bindings.image.Image(image_format: mediapipe::ImageFormat_Format, data: numpy.ndarray[numpy.uint16])
    3. mediapipe.python._framework_bindings.image.Image(image_format: mediapipe::ImageFormat_Format, data: numpy.ndarray[numpy.float32])

Invoked with: kwargs: image_format=<ImageFormat.SRGB: 1>, data=array([[[176, 223, 255],
        [176, 223, 255],

I tried commenting out the transpose line and the code runs fine. I checked my types and they seem to be good too.

As a bonus question, if anyone knows of an AI model that gives a face and hair (i.e. a selfie) mask without mediapipe that would be most welcome.

I would like to rotate an image in opencv before putting it into mediapipe. I took a transpose using numpy but, although the type is still numpy.uint8, mediapipe complains,

import cv2
import numpy as np
import mediapipe as mp

image_file_name = "IMG_0237.JPG"
cvImage = cv2.imread(image_file_name)
cvImage = np.transpose(cvImage, axes=[1,0,2]) #without this line code runs fine
print(cvImage.dtype)
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=cvImage)

returns,

uint8

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 9
      7 cvImage = np.transpose(cvImage, axes=[1,0,2]) #without this line code runs fine
      8 print(cvImage.dtype)
----> 9 mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=cvImage)

TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. mediapipe.python._framework_bindings.image.Image(image_format: mediapipe::ImageFormat_Format, data: numpy.ndarray[numpy.uint8])
    2. mediapipe.python._framework_bindings.image.Image(image_format: mediapipe::ImageFormat_Format, data: numpy.ndarray[numpy.uint16])
    3. mediapipe.python._framework_bindings.image.Image(image_format: mediapipe::ImageFormat_Format, data: numpy.ndarray[numpy.float32])

Invoked with: kwargs: image_format=<ImageFormat.SRGB: 1>, data=array([[[176, 223, 255],
        [176, 223, 255],

I tried commenting out the transpose line and the code runs fine. I checked my types and they seem to be good too.

As a bonus question, if anyone knows of an AI model that gives a face and hair (i.e. a selfie) mask without mediapipe that would be most welcome.

Share Improve this question edited Mar 6 at 20:38 Christoph Rackwitz 15.9k5 gold badges39 silver badges51 bronze badges asked Mar 6 at 18:35 ShoeatrixShoeatrix 231 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

The issue here is that ndarray.transpose does not actually move anything in memory. It simply resets the strides so that accesses APPEAR to be using new locations.

mediapipe, on the other hand, requires that the input array be physically contiguous in memory. Your array isn't. You can use np.ascontiguousarray to force it to rearrange the contents.

Is numpy.transpose reordering data in memory?

发布评论

评论列表(0)

  1. 暂无评论