Skip to content

Commit

Permalink
feat(example)!: pass the input texture name to MediaPipe
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Dec 21, 2020
1 parent 0eda94c commit 7f3628d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class TextureFrame {
}

public TextureFrame(int width, int height) {
this.texture = new Texture2D(width, height, TextureFormat.BGRA32, false);
this.texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
releaseCallbackHandle = GCHandle.Alloc((GlTextureBuffer.DeletionCallback)this.OnRelease, GCHandleType.Pinned);
}

Expand Down
34 changes: 31 additions & 3 deletions Assets/MediaPipe/Examples/Scripts/SceneDirector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;

using Mediapipe;
using UnityEngine;
Expand All @@ -22,6 +23,9 @@ public class SceneDirector : MonoBehaviour {
bool IsAssetLoaded = false;
bool IsAssetLoadFailed = false;

delegate void PluginCallback(int eventId);
GCHandle InitializeGpuHelperHandle;

void OnEnable() {
var nameForGlog = Path.Combine(Application.dataPath, "MediaPipePlugin");
var logDir = Path.Combine(Application.persistentDataPath, "Logs", "MediaPipe");
Expand All @@ -33,13 +37,30 @@ public class SceneDirector : MonoBehaviour {
Glog.Initialize(nameForGlog, logDir);
}

void InitializeGpuHelper(int eventId) {
// context is EGL_NO_CONTEXT if the graphics API is not OpenGL ES
var context = Egl.getCurrentContext();

if (context == IntPtr.Zero) {
Debug.LogWarning("No EGL Context Found");
} else {
Debug.Log($"EGL Context Found ({context})");
}

gpuResources = GpuResources.Create(context).ConsumeValueOrDie();
gpuHelper = new GlCalculatorHelper();
gpuHelper.InitializeForTest(gpuResources);
}

async void Start() {
webCamScreen = GameObject.Find("WebCamScreen");

if (useGPU) {
gpuResources = GpuResources.Create(Egl.getCurrentContext()).ConsumeValueOrDie();
gpuHelper = new GlCalculatorHelper();
gpuHelper.InitializeForTest(gpuResources);
PluginCallback gpuHelperInitializer = InitializeGpuHelper;
InitializeGpuHelperHandle = GCHandle.Alloc(gpuHelperInitializer, GCHandleType.Pinned);

var fp = Marshal.GetFunctionPointerForDelegate(gpuHelperInitializer);
GL.IssuePluginEvent(fp, 1);
}

#if UNITY_EDITOR
Expand All @@ -60,6 +81,12 @@ public class SceneDirector : MonoBehaviour {
}

void OnDisable() {
StopGraph();

if (InitializeGpuHelperHandle.IsAllocated) {
InitializeGpuHelperHandle.Free();
}

Glog.Shutdown();
}

Expand Down Expand Up @@ -140,6 +167,7 @@ public class SceneDirector : MonoBehaviour {
var graph = graphContainer.GetComponent<IDemoGraph<TextureFrame>>();

if (useGPU) {
// TODO: have to wait for gpuHelper to be initialized.
graph.Initialize(gpuResources, gpuHelper);
} else {
graph.Initialize();
Expand Down
3 changes: 2 additions & 1 deletion Assets/MediaPipe/Examples/Scripts/WebCamScreenController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class WebCamScreenController : MonoBehaviour {

try {
webCamTexture.Play();
Debug.Log($"WebCamTexture Graphics Format: {webCamTexture.graphicsFormat}");
} catch (Exception e) {
Debug.LogWarning(e.ToString());
return;
Expand Down Expand Up @@ -74,7 +75,7 @@ public class WebCamScreenController : MonoBehaviour {

public void InitScreen() {
Renderer renderer = GetComponent<Renderer>();
outputTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.BGRA32, false);
outputTexture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
renderer.material.mainTexture = outputTexture;

pixelData = new Color32[webCamTexture.width * webCamTexture.height];
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,15 @@ PlayerSettings:
m_GraphicsJobMode: 0
m_BuildTargetGraphicsAPIs:
- m_BuildTarget: AndroidPlayer
m_APIs: 150000000b000000
m_APIs: 0b000000
m_Automatic: 0
- m_BuildTarget: LinuxStandaloneSupport
m_APIs: 1500000011000000
m_Automatic: 0
m_BuildTargetVRSettings: []
openGLRequireES31: 0
openGLRequireES31AEP: 0
openGLRequireES32: 0
openGLRequireES32: 1
m_TemplateCustomTags: {}
mobileMTRendering:
Android: 1
Expand Down

0 comments on commit 7f3628d

Please sign in to comment.