Hi,
I’m working on project with image recognition. I use: Unity 2020.2.4 and Barracuda 1.3.0.
I have a problem with portrait mode in application. In landscape mode everything works, preview image on fullscreen is displaying correctly, frames are in correct places and have correct size. When I change device orientation to portrait, then my neural network doesn’t work and preview is in wrong orientation (black bars on top and bottom of the screen and image is distorted).
I use WebCamTexture to get image from iPhone back camera to display preview on RawImage. I use same texture and pass it to tensor. I think I should rotate texture, but I don’t know how to do this.
private void InitiateCameraDevice()
{
var webCamDevices = WebCamTexture.devices;
if (webCamDevices.Length == 0)
{
Debug.Log("No camera detected");
return;
}
foreach (var device in webCamDevices)
{
if (device.isFrontFacing == false)
{
_webCamTexture = new WebCamTexture(device.name, Screen.width, Screen.height);
break;
}
}
if (_webCamTexture == null)
{
Debug.Log("Unable to find back camera");
return;
}
_webCamTexture.Play();
_previewUI.texture = _webCamTexture;
}
private void Update()
{
if (Source == VideoSourceForDetector.Camera && _webCamTexture == null)
{
return;
}
var texture = (Source == VideoSourceForDetector.Camera) ? ProcessCameraImage() : ProcessVideoPlayerOutput();
if (texture.width <= 16)
{
return;
}
// pass texture to detector
_detector.ProcessImage(texture, _scoreThreshold, _overlapThreshold);
var i = 0;
foreach (var box in _detector.DetectedObjects)
{
if (i == _markers.Length)
{
break;
}
_markers[i++].SetAttributes(box, _webCamTexture.videoRotationAngle, ShowFrames);
}
for (; i < _markers.Length; i++)
{
_markers[i].Hide();
}
}
private Texture ProcessCameraImage()
{
var ratio = (float)Screen.width / (float)Screen.height;
_aspectRatioFitter.aspectRatio = ratio;
_markersAspectRatioFitter.aspectRatio = ratio;
var scaleY = _webCamTexture.videoVerticallyMirrored ? -1f : 1f;
_previewUI.rectTransform.localScale = new Vector3(1, scaleY, 1);
return _webCamTexture;
}
private Texture ProcessVideoPlayerOutput()
{
return _videoPlayeRenderTexture;
}