forked from homuler/MediaPipeUnityPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHairSegmentationGraph.cs
More file actions
64 lines (47 loc) · 2.02 KB
/
Copy pathHairSegmentationGraph.cs
File metadata and controls
64 lines (47 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using Mediapipe;
using UnityEngine;
public class HairSegmentationGraph : DemoGraph {
private const string hairMaskStream = "hair_mask";
private OutputStreamPoller<GpuBuffer> hairMaskStreamPoller;
private GpuBufferPacket hairMaskPacket;
public override Status StartRun() {
hairMaskStreamPoller = graph.AddOutputStreamPoller<GpuBuffer>(hairMaskStream).ConsumeValueOrDie();
hairMaskPacket = new GpuBufferPacket();
return graph.StartRun();
}
public override void RenderOutput(WebCamScreenController screenController, TextureFrame textureFrame) {
var hairMask = FetchNextHairMask();
var texture = screenController.GetScreen();
texture.SetPixels32(textureFrame.GetPixels32());
RenderAnnotation(screenController, hairMask);
texture.Apply();
}
private ImageFrame FetchNextHairMask() {
if (!hairMaskStreamPoller.Next(hairMaskPacket)) {
Debug.LogWarning($"Failed to fetch next packet from {hairMaskStream}");
return null;
}
ImageFrame outputFrame = null;
var status = gpuHelper.RunInGlContext(() => {
var gpuFrame = hairMaskPacket.Get();
var gpuFrameFormat = gpuFrame.Format();
var sourceTexture = gpuHelper.CreateSourceTexture(gpuFrame);
outputFrame = new ImageFrame(
gpuFrameFormat.ImageFormatFor(), gpuFrame.Width(), gpuFrame.Height(), ImageFrame.kGlDefaultAlignmentBoundary);
gpuHelper.BindFramebuffer(sourceTexture);
var info = gpuFrameFormat.GlTextureInfoFor(0);
Gl.ReadPixels(0, 0, sourceTexture.width, sourceTexture.height, info.glFormat, info.glType, outputFrame.MutablePixelData());
Gl.Flush();
sourceTexture.Release();
return Status.Ok(false);
});
if (!status.ok) {
Debug.LogError(status.ToString());
}
return outputFrame;
}
private void RenderAnnotation(WebCamScreenController screenController, ImageFrame hairMask) {
// NOTE: input image is flipped
GetComponent<MaskAnnotationController>().Draw(screenController.GetScreen(), hairMask, new Color(0, 0, 255), true);
}
}