Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Demo DetectorActivity landscape orientation problem #9412

Closed
anonym24 opened this issue Apr 24, 2017 · 27 comments
Closed

Android Demo DetectorActivity landscape orientation problem #9412

anonym24 opened this issue Apr 24, 2017 · 27 comments
Labels
stat:contribution welcome Status - Contributions welcome

Comments

@anonym24
Copy link

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?

@andrewharp andrewharp added the stat:contribution welcome Status - Contributions welcome label Apr 24, 2017
@jubjamie
Copy link
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?

@anonym24
Copy link
Author

anonym24 commented Apr 24, 2017

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

@jubjamie
Copy link
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),

@anonym24
Copy link
Author

anonym24 commented Apr 24, 2017

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

@jubjamie
Copy link
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?

@anonym24
Copy link
Author

anonym24 commented Apr 29, 2017

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

@solix
Copy link

solix commented Jul 27, 2017

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?

@bobeo
Copy link

bobeo commented Sep 29, 2017

I have the same issue. Any luck till recently?

@Nanamare
Copy link

Nanamare commented Nov 2, 2017

I solved this problem.

@bobeo
Copy link

bobeo commented Nov 2, 2017

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

@andrewharp
Copy link
Contributor

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

@anonym24
Copy link
Author

anonym24 commented Nov 2, 2017

@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

@andrewharp
Copy link
Contributor

andrewharp commented Nov 2, 2017

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.

@gunan gunan closed this as completed in bfd3983 Nov 4, 2017
@bobeo
Copy link

bobeo commented Nov 4, 2017

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

Thanks.

@anonym24
Copy link
Author

anonym24 commented Dec 4, 2017

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

@mfp305629415
Copy link

Have you solved it, please?

@annaisabel93
Copy link

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

@prasanna532
Copy link

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?

@shruti-techindustan
Copy link

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

@prasanna532
Copy link

prasanna532 commented Nov 16, 2018

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

@shruti-techindustan
Copy link

Ok, Thanks

@shruti-techindustan
Copy link

shruti-techindustan commented Nov 21, 2018

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

@anonym24
Copy link
Author

anonym24 commented Nov 21, 2018

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

@michaelzenz
Copy link

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);
    }
    
}

@erpsindhu
Copy link

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.)

@raymondsutjiadi
Copy link

raymondsutjiadi commented Jul 10, 2019

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.

@mrprimle
Copy link

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
Labels
stat:contribution welcome Status - Contributions welcome
Projects
None yet
Development

No branches or pull requests