简体   繁体   English

Mediapipepose SegmentationMask python javascript差异

[英]Mediapipe pose SegmentationMask python javascript differences

I am developing a pose recognition webapp using mediapipe pose library ( https://google.github.io/mediapipe/solutions/pose.html ).我正在使用 mediapipe 姿势库 ( https://google.github.io/mediapipe/solutions/pose.html ) 开发姿势识别 webapp。

I am using the segmentationMask to find some specific points of the human body that satisfy a constraint (the value in the n-th pixel must be > 0.1).我正在使用segmentationMask 来查找满足约束的人体的一些特定点(第n 个像素中的值必须> 0.1)。

I'am able to do this evaluation in python.我能够在 python 中进行此评估。 The library returns the segmentation mask as a matrix with the same width and height as the input image, and contains values in [0.0, 1.0] where 1.0 and 0.0 indicate high certainty of a “human” and “background” pixel respectively.该库将分割掩码作为与输入图像具有相同宽度和高度的矩阵返回,并包含 [0.0, 1.0] 中的值,其中 1.0 和 0.0 分别表示“人类”和“背景”像素的高确定性。 So I can iterate over the matrix and I am able to find the point that satisfy the constraint.所以我可以迭代矩阵,我能够找到满足约束的点。

I am trying to do the same thing in javascript, but I have a problem.我试图在 javascript 中做同样的事情,但我有一个问题。 The The javascript version of the library does not return a matrix but returns an ImageBitmap used by the html canvas to draw the mask. javascript 版本的库不返回矩阵,而是返回 html 画布用来绘制遮罩的 ImageBitmap。 The problem is that with ImageBitmap I cannot access every point of the matrix and I am not able to find the points I am interested in.问题是使用 ImageBitmap 我无法访问矩阵的每个点,也无法找到我感兴趣的点。

Is there a way to transform the javascript segmentationMask ImageBitmap in order be similar to the segmenationMask of the python versione library or at least retrive the same informations (I need the values included in this range [0.0, 1.0] for every pixel of the image).有没有办法转换 javascript segmentationMask ImageBitmap 以便类似于 python versione 库的 segmenationMask 或至少检索相同的信息(我需要图像的每个像素在此范围 [0.0, 1.0] 中包含的值) .

Thank you all.谢谢你们。

There is unfortunately no direct way to get an ImageData from an ImageBitmap, but you can drawImage() this ImageBitmap on a clear canvas and then call ctx.getImageData(0, 0, canvas.width, canvas.height) to retrieve an ImageData where you'll get access to all the pixels data.不幸的是,没有直接的方法可以从 ImageBitmap 中获取 ImageData,但是您可以在清晰的画布上drawImage()这个 ImageBitmap,然后调用ctx.getImageData(0, 0, canvas.width, canvas.height)来检索 ImageData,其中您将可以访问所有像素数据。

The confidence will be stored in the Alpha channel (every fourth item in imageData.data ) as a value between 0 and 255 .置信度将作为0255之间的值存储在 Alpha 通道中(在imageData.data中每隔四个项目)。

function onResults(results) {
  canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
  canvasCtx.drawImage(results.segmentationMask, 0, 0,
                      canvasElement.width, canvasElement.height);
  const imgData = canvasCtx.getImageData(0, 0, canvasElement.width, canvasElement.height);
  let i = 0;
  for (let y = 0; y<imgData.height; y++) {
    for (let x = 0; x<imgData.width; x++) {
      const confidence = imgData.data[i + 3];
      // do something with confidence here
      i++;
    }
  }
}

And since you're gonna read a lot from that context, don't forget to pass the willReadFrequently option when you get it.而且由于您将从该上下文中阅读很多内容,因此请不要忘记在获取时传递willReadFrequently选项。

As a fiddle since StackSnippets won't allow the use of the camera.作为一个小提琴,因为 StackSnippets 不允许使用相机。


Note that depending on what you do you may want to colorize this image from red to black using globalCompositeOperation and treat the data as an Uint32Array where the confidence would be expressed between 0 and 0xFF000000 .请注意,根据您的操作,您可能希望使用globalCompositeOperation将此图像从红色变为黑色,并将数据视为Uint32Array ,其中置信度将在00xFF000000之间表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM