Skip to content

Android Demo DetectorActivity landscape orientation problem #9412

Closed

Description

@anonym24

I'm trying to change the orientation to landscape:

private static final Size DESIRED_PREVIEW_SIZE = new Size(1280, 720);
private static final boolean USE_YOLO = true

        <activity android:name="org.tensorflow.demo.DetectorActivity"
                  android:screenOrientation="landscape"
                  android:label="@string/activity_name_detection">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

But it draws detected rects wrongly.
How to fix it?

Activity

  1. added
    stat:contribution welcome
    on Apr 24, 2017
  2. jubjamie commented on Apr 24, 2017

    @jubjamie
    Contributor

    By any chance are you referring to the fact that it draws them correctly in the debug preview but off to the side and squished in the main view?

  3. anonym24 commented on Apr 24, 2017

    @anonym24
    Author

    Actually in the debug preview there is also nothing.
    And in the main view, for example on my screenshot 2, we can see false (train) detected object (on the left)

    portrait
    portrait

    landscape
    landscape

  4. jubjamie commented on Apr 24, 2017

    @jubjamie
    Contributor

    If you look at the preview screen bottom right you will see that your input is coming through on it's side. That debug preview screen is what goes through the model. As it is on it's side the model will not preform as expected. That's why in landscape you're getting nonsense predictions. Almost all image recognition problems are NOT rotation invariant.

    On another note, you'll notice that the blue train box is being drawn on a part of the image that isn't in the preview that goes through the model. That's because the rectangle drawing functions don't work correctly when the app is changed to landscape. I am also struggling with this issue and hope @andrewharp is able to comment on that particular issue. (Probably not a bug but rather me being too rubbish at understanding the source code),

  5. anonym24 commented on Apr 24, 2017

    @anonym24
    Author

    I tried this:

    public class DetectorActivity extends CameraActivity implements OnImageAvailableListener {
     
     ...
     
     @Override
      public void onPreviewSizeChosen(final Size size, final int rotation) {
        
    	...
    	
        sensorOrientation = screenOrientation;
        //sensorOrientation = rotation + screenOrientation;
    

    screenshot_2017-04-24-19-53-20-430_org tensorflow demo

    So debug preview looks fine now

    So now it's only main view.
    I'm not sure where to look, but I guess here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowYoloDetector.java#L205

  6. jubjamie commented on Apr 24, 2017

    @jubjamie
    Contributor

    Yes that is the way to force the rotation. I also found problems with the rotation. I guess this is kind of linked with #9416 that @andrewharp has been assisting with.

    Regarding the boxes on the main view, it is on my todo list to try and figure out but will be a few weeks away if anyone else has any ideas?

  7. anonym24 commented on Apr 29, 2017

    @anonym24
    Author

    I tried to force rotation in the different part of code.
    Rects on the main view looks better now (I guess it shows normal width/height of every detected car now),
    but they are still at the wrong position..

    https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/DetectorActivity.java#L297

    rgbFrameBitmap.setPixels(rgbBytes, 0, previewWidth, 0, 0, previewWidth, previewHeight);
        final Canvas canvas = new Canvas(croppedBitmap);
        canvas.drawBitmap(rgbFrameBitmap, frameToCropTransform, null);
        croppedBitmap = rotateBitmap(croppedBitmap, -90); // here forced rotation
    
    public static Bitmap rotateBitmap(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
      }
    

    screenshot_2017-04-30-00-01-16-782_org tensorflow demo

  8. solix commented on Jul 27, 2017

    @solix

    I am also struggling with landscape mode for drawing correct rectangles around objects. Debug looks totally valid only preview side is remaining. any progress on this?

  9. bobeo commented on Sep 29, 2017

    @bobeo

    I have the same issue. Any luck till recently?

  10. Nanamare commented on Nov 2, 2017

    @Nanamare

    I solved this problem.

  11. bobeo commented on Nov 2, 2017

    @bobeo

    @Nanamare can you tell us how you solved the issue?

  12. andrewharp commented on Nov 2, 2017

    @andrewharp
    Contributor

    @Nanamare We'd love to have this as a contribution, if you'd be up for submitting a pull request!

  13. anonym24 commented on Nov 2, 2017

    @anonym24
    Author

    @Nanamare great, if the problem was related to List<Recognition> recognizeImage(final Bitmap bitmap) method (https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowYoloDetector.java#L134)

    then maybe you understand code pretty well and could also help with Yolo detector in Java for Windows/Ubuntu

    I already made a project in IntelliJ IDEA, I posted a result and full java classes from the project in my question https://stackoverflow.com/questions/47077983/yolo-detector-in-java
    I got some negative votes, but I don't care:)

    I'm just doing this for fun, it's just cool that we can run Tensorflow just everywhere and using different programming frameworks, languages, devices
    and Java is powerful for end-products - works on Windows/Ubuntu/Mac

  14. andrewharp commented on Nov 2, 2017

    @andrewharp
    Contributor

    I've made a fix for this internally, should show up in the next push. You'll still have to manually change the orientation of the Activity in AndroidManifest.xml, but everything else should just work afterwards.

  15. bobeo commented on Nov 4, 2017

    @bobeo

    @gunan @andrewharp will this work with screenOrientation="sensor" or screenOrientation="fullSensor"?

    Thanks.

  16. anonym24 commented on Dec 4, 2017

    @anonym24
    Author

    I downloaded the latest android example project and set android:screenOrientation="landscape"

    debug is ok, but still something wrong with main ui view and its orientation:

    TF Detect (SSD-Mobilene) Activity

    1

    p.s. I fixed java project (windows/ubuntu) for tensorflow yolo2/tiny yolo detector: https://stackoverflow.com/a/47632412/7767664

  17. mfp305629415 commented on Jan 9, 2018

    @mfp305629415

    Have you solved it, please?

  18. annaisabel93 commented on Mar 16, 2018

    @annaisabel93

    @andrewharp I changed the orientation but it stays just as @anonym24 described... Any idea on how to fix this?

  19. prasanna532 commented on Aug 14, 2018

    @prasanna532

    I am having some issue camera is opening only 70% to the screen Remaining 30% of the screen is black. Any suggestion or solution how to fix that?

  20. shruti-techindustan commented on Nov 15, 2018

    @shruti-techindustan

    Hi

    I have integrated tensor flow with YOLO in android. and I want to detect the objects in landscape and I also want camera preview in full screen.
    If I change the camera preview size. Object detection not working. Please help me.

    I have changed to support in landscape mode by changing
    sensorOrientation = screenOrientation;

    Change the camera preview size by adding
    private static Size DESIRED_PREVIEW_SIZE = new Size(1280, 720);
    and I have commented that method chooseOptimalSize() to show camera in full screen.

    Please help me to show the preview of a camera in full screen and also support for a landscape to detect the objects

    Thanks
    Shruti

  21. prasanna532 commented on Nov 16, 2018

    @prasanna532

    @shruti-mahajan
    change your mobile setting to auto rotate then it will detect the objects in any mode.

  22. shruti-techindustan commented on Nov 21, 2018

    @shruti-techindustan

    Ok, Thanks

  23. shruti-techindustan commented on Nov 21, 2018

    @shruti-techindustan

    Hi

    I want to get the distance of detected objects. Please guide me how to get the distance of detected objects. Actually, I already searched on the internet and I didn't get any idea.
    Please help me.

    Thanks

  24. anonym24 commented on Nov 21, 2018

    @anonym24
    Author

    @shruti-mahajan you should not ask for help for such tasks here, it's not about TensorFlow at all.

  25. michaelzenz commented on Feb 19, 2019

    @michaelzenz

    Just pulled the latest demo, still cannott change to landscape mode by simply changing the screenOrientation in manifest.xml, and change to auto rotate didn`t work for me @prasanna532

    Here`s my work around:

    In DetectorActivity:
    sensorOrientation = 0 - getScreenOrientation();//here change rotation to 0

    In MultiBoxTracker:

    final boolean rotated = sensorOrientation % 180 != 90;//here change == to !=
    
    frameToCanvasMatrix =
    ImageUtils.getTransformationMatrix(
            frameWidth,
            frameHeight,
            (int) (multiplier * (rotated ? frameHeight : frameWidth)),
            (int) (multiplier * (rotated ? frameWidth : frameHeight)),
            sensorOrientation+90,//here add 90 to degree
            false);
    
    
    borderedText.drawText(canvas, trackedPos.left + cornerSize, trackedPos.top, labelString,90);//here draw from top, and modify the drawText function in env/BorderedText
    

    In env/BorderedText:

    //Add a new drawText function
    public void drawText(final Canvas canvas, final float posX, final float posY, final String text, float angle) 
    {
        if(angle != 0){
            canvas.rotate(angle, posX, posY);
        }
        canvas.drawText(text, posX, posY, exteriorPaint);
        canvas.drawText(text, posX, posY, interiorPaint);
        if(angle != 0){
            canvas.rotate(-angle, posX, posY);
        }
        
    }
    
  26. 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.)

  27. raymondsutjiadi commented on Jul 10, 2019

    @raymondsutjiadi

    I have the same problem with newest example file. Please guide me how to change the screen orientation to landscape. I have tried all suggested modification here, but it does not work.

  28. mrprimle commented on Dec 29, 2020

    @mrprimle

    I am having some issue camera is opening only 70% to the screen Remaining 30% of the screen is black. Any suggestion or solution on how to fix that?

    I've solved the landscape orientation problem thanks to @MICHAEL-ZENZ. However, the issue of 30% black rectangle @prasanna532 at the bottom stayed in both landscape and vertical orientations. Please, need help.

    Screenshot_2020-12-29-13-13-37-219_org tensorflow lite examples detection

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 welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions