tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

[tfjs-react-native] CameraStream wrong orientation in landscape mode.

Open nicholasio opened this issue 6 years ago • 11 comments

To get help from the community, we encourage using Stack Overflow and the tensorflow.js tag.

TensorFlow.js version

    "@tensorflow/tfjs": "^1.5.2",
    "@tensorflow/tfjs-react-native": "^0.2.3",

Browser version

This is an issue with tensorflow for react native. react-native-cli: 2.0.1 react-native: 0.61.5

Describe the problem or feature request

The Camera Stream does not work well in landscape mode. The image is streamed/render in the wrong orientation/dimensions.

Code to reproduce the bug / link to feature request

In iOS set landscape mode in Xcode and use the TensorCamera as usual:

const TensorCamera = cameraWithTensors( ExpoCamera );

const Camera = () => {
	return (<TensorCamera
		style={{ flex: 1} }
		cameraTextureHeight={ 1920 }
		cameraTextureWidth={ 1080 }
		resizeDepth={ 3 }
		onReady={ handleCameraStream }
		autorender={ true }
	/>)
}

nicholasio avatar Mar 27 '20 21:03 nicholasio

@rthadur @tafsiri I'm willing to contribute with a PR for this one. Is there any guidance you can give on where I should start looking at? After a quick look it seems like we need to rotate the image when in landscape mode. I assume that should be done through WebGL in a custom shader.

Any guidance is appreciated.

nicholasio avatar Mar 31 '20 17:03 nicholasio

@nicholasio Thanks for being willing to contribute. A couple of starting points to investigate.

For doing the transform in WebGL the starting point would be the renderToGLView function, you will see it calls drawTexture (which ultimately runs a shader). These would need to support taking a rotation related param. Ideally this is something that can detected outside of the cameraStream component and passed in as a prop.

Another thing that might be worth taking a quick look at, though it may not work, is whether doing a react native transform on the containing view would work. I'd eliminate this option first as it is quick to try.

tafsiri avatar Apr 01 '20 14:04 tafsiri

@tafsiri

The react native transformation fixes the preview

          <GLView
            key='camera-with-tensor-gl-view'
            style={[styles.glView, { transform: [{rotate: '90deg'}]}]}
            onContextCreate={this.onGLContextCreate}
            ref={ref => (this.glView = ref)}
          />

However, the webcam tests no longer work properly in the integration testing app. I think this rotation adjustment needs to happen early, I believe the image tensor that is yielded to the onReady callback is still being sent without the rotation.

nicholasio avatar Apr 01 '20 22:04 nicholasio

This is how it looks like in a test app I made, it was supposed to take up the whole viewport.

image

Wondering if fromTexture is where a fix should go.

nicholasio avatar Apr 02 '20 01:04 nicholasio

@tafsiri I was able to fix the camera preview by having the draw_texture_program rotate it

export function fragmentShaderSource() {
  return `#version 300 es
precision highp float;
#define PI 3.1415926535897932384626433832795

uniform sampler2D inputTexture;
in vec2 uv;
out vec4 fragColor;

vec2 rotateUV(vec2 uv, vec2 pivot, float rotation) {
  float cosa = cos(rotation);
  float sina = sin(rotation);
  uv -= pivot;
  return vec2(
      cosa * uv.x - sina * uv.y,
      cosa * uv.y + sina * uv.x
  ) + pivot;
}

void main() {
  vec4 texSample = texture( inputTexture, rotateUV(uv, vec2(0.5), -(PI / 2.)) );
  fragColor = texSample;
}`;
}

I'm also assuming this same rotation needs to happen when getting the texture with fromTexture to ensure the tensor that is sent to the onReady callback is in the correct orientation.

Would you say that writing a new webgl program is the way to go? I'm also thinking of adding a prop to the Camera Component to enable landscape mode.

Will submit a PR in the new few days.

nicholasio avatar Apr 04 '20 02:04 nicholasio

Hello can you tell me how do you run tfjs-react-native on react-native-cli project? i config and run got error image Please help me? my package.json image

duylinhdang1998 avatar Apr 04 '20 05:04 duylinhdang1998

@duylinhdang1998 looks like you need pod install on the ios folder. But please open your own issue or go to stack overflow for support.

@tafsiri On another note, this doesn't seem to be an issue on android.

nicholasio avatar Apr 04 '20 17:04 nicholasio

@tafsiri I've got a PR opened, let me know If it's on the right track. I updated the test project and was able to verify the camera rotates properly.

nicholasio avatar Apr 06 '20 12:04 nicholasio

Any update on this issue? I'm facing the same problem...

GwangjoGong avatar Nov 01 '21 16:11 GwangjoGong

Please check the related PR https://github.com/tensorflow/tfjs/pull/3041#issuecomment-953312177 , @jinjingforever is working on fix for it. Thank you

rthadur avatar Nov 01 '21 16:11 rthadur

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Nov 03 '21 22:11 google-ml-butler[bot]