Skip to content

Commit

Permalink
fix(sample): front camera image is not rotated properly (#504)
Browse files Browse the repository at this point in the history
* fix(sample): front camera image is not rotated properly

* fix format
  • Loading branch information
homuler authored Mar 25, 2022
1 parent 881642e commit 9d47f7c
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions Assets/Mediapipe/Samples/Common/Scripts/Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Initialize(ImageSource imageSource)

Resize(_imageSource.textureWidth, _imageSource.textureHeight);
Rotate(_imageSource.rotation.Reverse());
uvRect = GetUvRect(RunningMode.Async);
ResetUvRect(RunningMode.Async);
texture = imageSource.GetCurrentTexture();
}

Expand All @@ -51,27 +51,48 @@ public void ReadSync(TextureFrame textureFrame)
if (!(texture is Texture2D))
{
texture = new Texture2D(_imageSource.textureWidth, _imageSource.textureHeight, TextureFormat.RGBA32, false);
uvRect = GetUvRect(RunningMode.Sync);
ResetUvRect(RunningMode.Sync);
}
textureFrame.CopyTexture(texture);
}

private UnityEngine.Rect GetUvRect(RunningMode runningMode)
private void ResetUvRect(RunningMode runningMode)
{
var rect = new UnityEngine.Rect(0, 0, 1, 1);

if (_imageSource.isFrontFacing)
if (_imageSource.isVerticallyFlipped && runningMode == RunningMode.Async)
{
rect.x = 1;
rect.width = -1;
// In Async mode, we don't need to flip the screen vertically since the image will be copied on CPU.
rect = FlipVertically(rect);
}
if (_imageSource.isVerticallyFlipped && runningMode == RunningMode.Async)

if (_imageSource.isFrontFacing)
{
rect.y = 1;
rect.height = -1;
// Flip the image (not the screen) horizontally.
// It should be taken into account that the image will be rotated later.
var rotation = _imageSource.rotation;

if (rotation == RotationAngle.Rotation0 || rotation == RotationAngle.Rotation180)
{
rect = FlipHorizontally(rect);
}
else
{
rect = FlipVertically(rect);
}
}

return rect;
uvRect = rect;
}

private UnityEngine.Rect FlipHorizontally(UnityEngine.Rect rect)
{
return new UnityEngine.Rect(1 - rect.x, rect.y, -rect.width, rect.height);
}

private UnityEngine.Rect FlipVertically(UnityEngine.Rect rect)
{
return new UnityEngine.Rect(rect.x, 1 - rect.y, rect.width, -rect.height);
}
}
}

0 comments on commit 9d47f7c

Please sign in to comment.