Skip to content

Commit

Permalink
fix: synchronize the input image and the output annotation (#359)
Browse files Browse the repository at this point in the history
* fix: synchronize annotations and images

* fix: image is upside down on iOS devices
  • Loading branch information
homuler committed Nov 27, 2021
1 parent 06423ca commit 47598ce
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 11 deletions.
12 changes: 12 additions & 0 deletions Assets/Mediapipe/Samples/Common/Scripts/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ protected static void SetupScreen(RawImage screen, ImageSource imageSource)
{
screen.uvRect = new UnityEngine.Rect(0, 1, 1, -1);
}

screen.texture = imageSource.GetCurrentTexture();
}

protected static void SetupAnnotationController<T>(AnnotationController<T> annotationController, ImageSource imageSource, bool expectedToBeMirrored = false) where T : HierarchicalAnnotation
Expand Down Expand Up @@ -106,5 +108,15 @@ protected static void ReadFromImageSource(ImageSource imageSource, TextureFrame
textureFrame.ReadTextureFromOnCPU(sourceTexture);
}
}

protected static void UpdateScreenSync(RawImage screen, TextureFrame textureFrame)
{
if (!(screen.texture is Texture2D))
{
screen.texture = new Texture2D(textureFrame.width, textureFrame.height, TextureFormat.RGBA32, false);
screen.uvRect = new UnityEngine.Rect(0, 0, 1, 1);
}
textureFrame.CopyTexture(screen.texture);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

Logger.LogInfo(TAG, $"Running Mode = {runningMode}");

Expand Down Expand Up @@ -114,6 +113,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var trackedDetections = _graphRunner.FetchNextValue();
_trackedDetectionsAnnotationController.DrawNow(trackedDetections);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

Logger.LogInfo(TAG, $"Model Selection = {modelType}");
Logger.LogInfo(TAG, $"Running Mode = {runningMode}");
Expand Down Expand Up @@ -121,6 +120,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var detections = _graphRunner.FetchNextValue();
_faceDetectionsAnnotationController.DrawNow(detections);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

Logger.LogInfo(TAG, $"Max Num Faces = {maxNumFaces}");
Logger.LogInfo(TAG, $"Refine Landmarks = {refineLandmarks}");
Expand Down Expand Up @@ -138,6 +137,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var value = _graphRunner.FetchNextValue();
_faceDetectionsAnnotationController.DrawNow(value.faceDetections);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

Logger.LogInfo(TAG, $"Running Mode = {runningMode}");

Expand Down Expand Up @@ -114,6 +113,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var hairMask = _graphRunner.FetchNextValue();
_hairMaskAnnotationController.DrawNow(hairMask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

Logger.LogInfo(TAG, $"Max Num Hands = {maxNumHands}");
Logger.LogInfo(TAG, $"Running Mode = {runningMode}");
Expand Down Expand Up @@ -132,6 +131,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var value = _graphRunner.FetchNextValue();
_palmDetectionsAnnotationController.DrawNow(value.palmDetections);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

_worldAnnotationArea.localEulerAngles = imageSource.rotation.Reverse().GetEulerAngles();

Logger.LogInfo(TAG, $"Model Complexity = {modelComplexity}");
Expand Down Expand Up @@ -148,6 +148,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var value = _graphRunner.FetchNextValue();
_poseDetectionAnnotationController.DrawNow(value.poseDetection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

Logger.LogInfo(TAG, $"Running Mode = {runningMode}");

Expand Down Expand Up @@ -136,6 +135,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var value = _graphRunner.FetchNextValue();
_trackedAnchorDataAnnotationController.DrawNow(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

Logger.LogInfo(TAG, $"Running Mode = {runningMode}");

Expand Down Expand Up @@ -120,6 +119,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var value = _graphRunner.FetchNextValue();
_faceDetectionsAnnotationController.DrawNow(value.faceDetections);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

Logger.LogInfo(TAG, $"Running Mode = {runningMode}");

Expand Down Expand Up @@ -114,6 +113,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var detections = _graphRunner.FetchNextDetections();
_outputDetectionsAnnotationController.DrawNow(detections);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

Logger.LogInfo(TAG, $"Category = {category}");
Logger.LogInfo(TAG, $"Max Num Objects = {maxNumObjects}");
Expand Down Expand Up @@ -137,6 +136,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var value = _graphRunner.FetchNextValue();
_liftedObjectsAnnotationController.DrawNow(value.liftedObjects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private IEnumerator Run()
}
// NOTE: The _screen will be resized later, keeping the aspect ratio.
SetupScreen(_screen, imageSource);
_screen.texture = imageSource.GetCurrentTexture();

_worldAnnotationArea.localEulerAngles = imageSource.rotation.Reverse().GetEulerAngles();

Logger.LogInfo(TAG, $"Model Complexity = {modelComplexity}");
Expand Down Expand Up @@ -138,6 +138,9 @@ private IEnumerator Run()

if (runningMode == RunningMode.Sync)
{
// TODO: copy texture before `textureFrame` is released
UpdateScreenSync(_screen, textureFrame);

// When running synchronously, wait for the outputs here (blocks the main thread).
var value = _graphRunner.FetchNextValue();
_poseDetectionAnnotationController.DrawNow(value.poseDetection);
Expand Down

0 comments on commit 47598ce

Please sign in to comment.