Skip to content

firebase_ml_vision example sensor orientation #2793

@wz2b

Description

@wz2b

On a Pixel 3XL, the firebase vision example doesn't work very well. With no errors, it doesn't decode barcodes out of the box, and the 'camera preview' example doesn't find anything.

To reproduce this, you just have to build this example and run it on a Google Pixel 3XL.

There is a lot of advice about this problem on stackoverflow and other places. One thing that seems to improve the barcode scanning example is to do this:

    defaultTargetPlatform == TargetPlatform.android
        ? ResolutionPreset.high // was medium
        : ResolutionPreset.low;

I can verify that does indeed improve performance, but I don't think it's doing so the right way. My first clue to this is that if you run "Material Barcode Scanner" and hold the phone in portrait mode, it scans with MUCH better reliability if you hold the bar code sideways. You can watch it perform by looking at the bottom where it says "Hold the bar code closer".

As a second example, if you run the Camera Preview Scanner and put it into barcode mode, it draws boxes around where it thinks it sees an area to decode. Again holding the phone vertically (portrait), it doesn't see much. Turn the barcode sideways, and it detects it with a long, skinny box that's "tall" rather than "wide".

I think something has the orientation wrong. There are a number of references like This that explain why orientation is so confusing. Orientation is really talking about the sensor's orientation, not the image. If the sensor is 90 degrees off, then in landscape you might have to rotate 90 + 90 (or possibly 90 - 90) to get the image upright.

Here's an example of that with the barcode detector selected. If I turn the bottle sideways, it doesn't detect ANYTHING. If I turn it the way shown, you can see that it does.

image

Now here's where it gets even stranger. It doesn't detect any text at all in ResolutionPreset.medium (the default for android). But if I change that to Resolution.high it does. But only in the correct (horizontal, normal, whatever you want to call it) orientation. In other words, in 'high' resolution, text recognition works fine. On this device, at least, there doesn't seem to be any performance penalty for selecting 'high' resolution.

image

I can fix all these problems with various hard-coding but that's a terrible way to make something that should, in theory, work everywhere. And like I said, these kinds of problems haunted me back in Kotlin as well. But in the context of this project, you have a great example that mostly works out-of-the-box, so i'm wondering if, rather than wait for firebase to fix these issues, we can just make this example more reliable.

Thoughts?

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^0.4.0+9
  firebase_ml_vision: ^0.9.4
  firebase_analytics: ^5.0.2
  firebase_auth: ^0.14.0+5
  cloud_firestore: ^0.12.9+5
  camera: ^0.5.8+2      /* I tried several different versions with same results */
  image_picker: '0.6.7+2'
  path_provider: '1.6.10'
  cupertino_icons: ^0.1.3
[√] Flutter (Channel stable, v1.17.1, on Microsoft Windows [Version 10.0.19041.329], locale en-US)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[√] Android Studio (version 3.6)
[√] IntelliJ IDEA Ultimate Edition (version 2019.3)
[√] VS Code (version 1.45.1)
[√] Connected device (1 available)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions