Skip to content

Android Demo: Rotation on some devices broken. #9416

Closed

Description

@jubjamie

System information

  • Android:
  • TensorFlow installed from source:
  • TensorFlow version 1.0.0:
    N/A

Describe the problem

The Android Demo code seems to be a bit buggy on our Pixel C. I wanted to confirm that this isn't an issue with the TF code but it's a strange one.

The Rotation in the demo apps on our Pixel C didn't seem to work correctly. It would give the value of 3 when it should have read 90. This meant that the input was basically on it's side. I found that for it to work that this line needed to be set to 0.

sensorOrientation = rotation + screenOrientation; needed to be
sensorOrientation = 0;
However, when used in landscape mode this didn't crop around the centre of the preview image but instead it created a square from the left edge.

|------------------| ---------|                         
|                  |          |
|       CROP       |          |
|     SQUARE       |          |
|__________________|__________|

When set to 0.
The solution seemed to be to set the Orientation to 360.

|-----|------------------|----|                         
|     |                  |    |
|     |        CROP      |    |
|     |      SQUARE      |    |
|_____|__________________|____|

When set to 360.

At the time (a couple of weeks ago) I recalled that it forced some code to run that would centre it as required for some other operations however going through the code to find it I can't. I think it was related to this but it doesn't make sense as to why 360 solved the issue.

private void configureTransform(final int viewWidth, final int viewHeight) {
    final Activity activity = getActivity();
    if (null == textureView || null == previewSize || null == activity) {
      return;
    }
    final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    final Matrix matrix = new Matrix();
    final RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
    final RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
    final float centerX = viewRect.centerX();
    final float centerY = viewRect.centerY();
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
      bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
      matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
      final float scale =
          Math.max(
              (float) viewHeight / previewSize.getHeight(),
              (float) viewWidth / previewSize.getWidth());
      matrix.postScale(scale, scale, centerX, centerY);
      matrix.postRotate(90 * (rotation - 2), centerX, centerY);
    } else if (Surface.ROTATION_180 == rotation) {
      matrix.postRotate(180, centerX, centerY);
    }
    textureView.setTransform(matrix);
  }

So I guess the question is that is this happening due to some strange hardware bug with the Pixel C or some coding error. I'm leaning towards the former as this is too bizarre to be caused by code but thought i'd open the discussion out as others trying to run the app in landscape may face similar issues.

Activity

  1. drpngx commented on Apr 24, 2017

    @drpngx
    Contributor

    Maybe @andrewharp knows something about this

  2. girving commented on Apr 24, 2017

    @girving
    Contributor

    @drpngx You beat me by about 10 seconds. :)

  3. added
    stat:awaiting tensorflower
    on Apr 24, 2017
  4. drpngx commented on Apr 24, 2017

    @drpngx
    Contributor

    WFH, with fast internet :-) but I have some meeting now, will leave the rest to you :-)

  5. andrewharp commented on Apr 24, 2017

    @andrewharp
    Contributor

    @jubjamie To clarify, is the demo broken on your Pixel C even when compiling with no edits?

  6. jubjamie commented on Apr 24, 2017

    @jubjamie
    ContributorAuthor

    Yes. With no edits it had the rotation value as 3. So I went in to try and force it to be the correct value (which in portrait was 90).
    In landscape I encountered the centring problem and had to set it to 360. No problem on my 6P. Not sure if you guys have another Pixel C lying around to confirm it's not my hardware being funny?

  7. andrewharp commented on Apr 24, 2017

    @andrewharp
    Contributor

    So characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) returns 3? That really shouldn't be happening... I suppose it'd be ok to assume 90 in general if the value returned % 90 != 0.

    Regarding landscape support, this is something that would be nice to have but hasn't been high priority enough to work through all the transformations to ensure correctness. Contributions definitely welcome.

  8. added
    stat:contribution welcome
    stat:awaiting response
    and removed
    stat:awaiting tensorflower
    on Apr 24, 2017
  9. jubjamie commented on Apr 25, 2017

    @jubjamie
    ContributorAuthor

    Yup. I've seen somewhere else on here where someone's Rotation was 1. I appreciate it's not really important in the grand scheme of things. Just mildly frustrating when this is the only sample code available. Are there any more detailed tutorials or specific documentation planned that could help with getting apps up and running correctly?

  10. removed
    stat:awaiting response
    on Apr 25, 2017
  11. andrewharp commented on Apr 25, 2017

    @andrewharp
    Contributor

    We have more walkthroughs planned, but they aren't specifically meant to improve upon camera support themselves.

    You've already found #9142, and #8736 is meant to add android.hardware.Camera support for devices with wonky Camera2 behavior (like the Pixel C apparently) -- we just have to find the right discriminating features to make misbehaving devices choose the original API.

  12. jubjamie commented on Apr 25, 2017

    @jubjamie
    ContributorAuthor

    Walkthroughs for anything are very welcome! Fortunately my work doesn't really require too much android development work but I'm sure others will appreciate. I'll try and get back to this issue in a couple of days. Ta.

  13. girving commented on Jun 16, 2017

    @girving
    Contributor

    Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!

  14. Nanamare commented on Nov 2, 2017

    @Nanamare

    I solved this problem.

  15. ignacioalpiste commented on Mar 15, 2018

    @ignacioalpiste

    Hi @jubjamie,
    do you know if is it possible to make the crop square full screen?
    I would like to recognize full screen and not just a part.
    Thanks

  16. andrewharp commented on Mar 15, 2018

    @andrewharp
    Contributor

    @IgMart
    For the classifier activity you can change MAINTAIN_ASPECT_RATIO to false, in which case it will stretch the entire screen to the dimensions of the model input.

    For the detector activity it already stretches with the default SSD model. The YOLO detector changes the behavior to use a center-crop via a similar MAINTAIN_ASPECT_RATIO constant.

    In either case you can verify the image actually being sent to TensorFlow by turning on debug mode with the volume keys.

    @jubjamie This particular rotation issue should have been fixed last year -- I went through and corrected the behavior so landscape should work on most devices.

  17. annaisabel93 commented on Mar 20, 2018

    @annaisabel93

    @andrewharp landscape is not working. I have tried and tested all the workaround I found on multiple devices and nothing happened (sensorOrientation for example).
    Even with no edits, simply changing the orientation to landscape I get a cropped sceen just as @jubjamie described. Where can I try to force rotation?

  18. ivantangg commented on Apr 17, 2019

    @ivantangg

    @Nanamare Hey, may i ask how you were able to resolve the issue? I tried the 360 solution, however, it the cropped square is still staying towards left side of the screen. I am trying to stretch the preview square to fit the whole screen.

  19. erpsindhu commented on Jun 19, 2019

    @erpsindhu

    I commented out the portion in set fragment, which is calling the legacy connection fragment. Then landscape mode seems to work without any changes(except in the manifest file) - camera2api being used. (Incase, we want to retain the if..else block and want the legacy fragment to be called, then need to make changes in legacyconnectionfragment.java file corresponding to landscape mode.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stat:contribution welcometype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions