ultralytics / yolo-flutter-app

A Flutter plugin for Ultralytics YOLO computer vision models
https://ultralytics.com
GNU Affero General Public License v3.0
66 stars 24 forks source link

I CANT RUN THE EXAMPLE APP #38

Open hadesgleizos opened 2 months ago

hadesgleizos commented 2 months ago

I have been having a problem trying to run the example app from here, i used git clone to get the app and it is just white bg and when i click the abc icon i get this error: first

This is the logcat: 2024-07-07 14:10:08.811 6274-6274 MethodChan...ytics_yolo com...tics.ultralytics_yolo_example E Failed to handle method call java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.camera.lifecycle.ProcessCameraProvider.unbindAll()' on a null object reference at com.ultralytics.ultralytics_yolo.CameraPreview.bindPreview(CameraPreview.java:79) at com.ultralytics.ultralytics_yolo.CameraPreview.setCameraFacing(CameraPreview.java:97) at com.ultralytics.ultralytics_yolo.MethodCallHandler.setLensDirection(MethodCallHandler.java:260) at com.ultralytics.ultralytics_yolo.MethodCallHandler.onMethodCall(MethodCallHandler.java:95) at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7959) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942) 2024-07-07 14:10:08.817 6274-6300 flutter com...tics.ultralytics_yolo_example E [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'void androidx.camera.lifecycle.ProcessCameraProvider.unbindAll()' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.camera.lifecycle.ProcessCameraProvider.unbindAll()' on a null object reference at com.ultralytics.ultralytics_yolo.CameraPreview.bindPreview(CameraPreview.java:79) at com.ultralytics.ultralytics_yolo.CameraPreview.setCameraFacing(CameraPreview.java:97) at com.ultralytics.ultralytics_yolo.MethodCallHandler.setLensDirection(MethodCallHandler.java:260) at com.ultralytics.ultralytics_yolo.MethodCallHandler.onMethodCall(MethodCallHandler.java:95) at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7959) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942) )

0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)

                                                                                                #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
                                                                                                <asynchronous suspension>
                                                                                                #2      UltralyticsYoloCameraController.toggleLensDirection (package:ultralytics_yolo/camera_preview/ultralytics_yolo_camera_controller.dart:48:5)
                                                                                                <asynchronous suspension>
basharatahmed-ab commented 2 months ago

I've encountered a similar issue in my project. After examining the problem, I realized that I had forgotten to add the assets in the "pubspec.yaml" file. I recommend double-checking your "pubspec.yaml" file to ensure that all necessary assets are included. Once I added the missing assets, the issue was resolved, and everything is now working fine.

pderrenger commented 2 months ago

@basharatahmed-ab hi there,

Thank you for reaching out and providing detailed information about the issue you're facing. It looks like the error is related to the camera binding process in the CameraPreview class. Here are a few steps you can take to troubleshoot and potentially resolve this issue:

  1. Check pubspec.yaml: As mentioned by another user, ensure that all necessary assets and dependencies are correctly listed in your pubspec.yaml file. Missing assets can sometimes cause unexpected behavior.

  2. Update Dependencies: Ensure that you are using the latest versions of all dependencies. Run flutter pub get to fetch the latest packages and verify that there are no version conflicts.

  3. Camera Permissions: Verify that you have the necessary camera permissions set in your AndroidManifest.xml file. You should have the following permissions:

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  4. Reproducible Example: If the issue persists, it would be very helpful if you could provide a minimum reproducible example. This will allow us to better understand and diagnose the problem. You can find more information on how to create a reproducible example here.

  5. Code Review: Double-check the code where the camera is being initialized and bound. Ensure that the ProcessCameraProvider is properly initialized before calling unbindAll().

Here’s a small snippet to ensure the ProcessCameraProvider is correctly initialized:

ProcessCameraProvider cameraProvider = ProcessCameraProvider.getInstance(context).get();
if (cameraProvider != null) {
    cameraProvider.unbindAll();
    // Continue with binding the camera
} else {
    // Handle the case where cameraProvider is null
}

If you continue to experience issues, please share more details or a reproducible example, and we’ll be happy to assist further.

github-actions[bot] commented 1 month ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐

huats commented 2 weeks ago

I am getting the exact same error. Can you explain what you changed to your pubspec.yaml to add the assets ? I have tried with the declaration of the assets/ directory only (like it is in the code) and by adding

- assets/yolov8n_int8.tflite
- assets/metadata.yaml
- assets/yolov8n-cls.mlmodel

Please also note that I have added the requirement permissions (well at least I think I did) to the AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
glenn-jocher commented 1 week ago

@huats ensure your pubspec.yaml includes the assets like this:

flutter:
  assets:
    - assets/yolov8n_int8.tflite
    - assets/metadata.yaml
    - assets/yolov8n-cls.mlmodel

Also, confirm that your AndroidManifest.xml has the necessary permissions. If the issue persists, try updating to the latest package version.

dtdo90 commented 1 week ago

Hi. Thank you for your reply. I am getting a similar issue. How can we get the metadata.yaml file?

huats commented 1 week ago

@huats ensure your pubspec.yaml includes the assets like this:

flutter:
  assets:
    - assets/yolov8n_int8.tflite
    - assets/metadata.yaml
    - assets/yolov8n-cls.mlmodel

Also, confirm that your AndroidManifest.xml has the necessary permissions. If the issue persists, try updating to the latest package version.

All of that is already present.

Here is my AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ultralytics.ultralytics_yolo_example">

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

   <application
        android:label="ultralytics_yolo_example"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">

        <uses-library android:name="libOpenCL.so"
            android:required="false"/>

        <uses-library android:name="libOpenCL-pixel.so"
            android:required="false"/>

        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

I am using code available at the latest commit in the main branch.

glenn-jocher commented 1 week ago

@huats the metadata.yaml file should be part of the model package you downloaded. If it's missing, ensure you've downloaded the complete package from the official source. If the issue continues, please verify that all files are correctly placed and accessible in your project structure.

huats commented 1 week ago

@glenn-jocher I wasn't the one mentioning a missing metadata.yaml :)

Here is mine (the one from the repo)

description: Ultralytics YOLOv8n model trained on coco.yaml
author: Ultralytics
license: AGPL-3.0 https://ultralytics.com/license
date: "2023-11-30T10:16:15.604065"
version: 8.0.215
stride: 32
task: detect
batch: 1
imgsz:
  - 320
  - 320
names:
  0: person
  1: bicycle
  2: car
  3: motorcycle
  4: airplane
  5: bus
  6: train
  7: truck
  8: boat
  9: traffic light
  10: fire hydrant
  11: stop sign
  12: parking meter
  13: bench
  14: bird
  15: cat
  16: dog
  17: horse
  18: sheep
  19: cow
  20: elephant
  21: bear
  22: zebra
  23: giraffe
  24: backpack
  25: umbrella
  26: handbag
  27: tie
  28: suitcase
  29: frisbee
  30: skis
  31: snowboard
  32: sports ball
  33: kite
  34: baseball bat
  35: baseball glove
  36: skateboard
  37: surfboard
  38: tennis racket
  39: bottle
  40: wine glass
  41: cup
  42: fork
  43: knife
  44: spoon
  45: bowl
  46: banana
  47: apple
  48: sandwich
  49: orange
  50: broccoli
  51: carrot
  52: hot dog
  53: pizza
  54: donut
  55: cake
  56: chair
  57: couch
  58: potted plant
  59: bed
  60: dining table
  61: toilet
  62: tv
  63: laptop
  64: mouse
  65: remote
  66: keyboard
  67: cell phone
  68: microwave
  69: oven
  70: toaster
  71: sink
  72: refrigerator
  73: book
  74: clock
  75: vase
  76: scissors
  77: teddy bear
  78: hair drier
  79: toothbrush
dtdo90 commented 1 week ago

Thank you @huats for sharing the file content!

huats commented 1 week ago

@glenn-jocher first thanks for trying to help all of us ! It is really appreciated !

You told us to update to a newer version but I am using the latest commit on the main branch.

So basically I have simply clone the repo here, went to the app directory and tried to launch it used Android Studio. Since the start of the app was crashing I had to make a few changes (very little...). Here is my current diff to the branch (Yes I have tried many updates)

diff.txt

The file CameraPreview.java is not in the app directory it is untouched from the repo and can be seen here

glenn-jocher commented 1 week ago

Thank you for sharing the details. Please ensure all dependencies are up-to-date and verify your changes align with the latest documentation. If the issue persists, consider checking for any device-specific compatibility issues.

huats commented 1 week ago

Thanks for your feedback. I am up-to-date, so far I am aligned with the doc too. I have tested on the AndroidStudio Pixel's emulator and my personal pixel 7

Can you please just confirm me that you have tested the current version of. example application yourself and that it is working ?

glenn-jocher commented 1 week ago

Thank you for your patience. The example application should work with the latest version. Please ensure all dependencies are updated. If issues persist, it might be device-specific.

hanspres1999 commented 1 week ago

Thanks for your feedback. I am up-to-date, so far I am aligned with the doc too. I have tested on the AndroidStudio Pixel's emulator and my personal pixel 7

Can you please just confirm me that you have tested the current version of. example application yourself and that it is working ?

I was at the same stage as you are. Showed me the same errors despite following all the requested changes to the source code and it didnt work until I read Issue #33 Simply make the changes where @fransay suggests in the first part to remove storage permissions, and that worked for me. The second part has already been integrated in the latest version so you wont need to do that.

Hope it helps.

huats commented 1 week ago

Oh wow @hanspres1999 you just saved my day/week !!! I can confirm that the elements you mentioned fixed the issue I had.

I will create a PR so that others don't experience that in the future !

hanspres1999 commented 1 week ago

Looks like you can go ahead and finally close the issue

glenn-jocher commented 1 week ago

Great to hear the issue is resolved! Feel free to close it if everything is working smoothly now.

huats commented 1 week ago

I wasn't the one who opened it, and I feel I "stole" it a bit :) I have created a PR that fixes it, might be worth to merge it so that others won't face the same issue as I did...

glenn-jocher commented 1 week ago

@huats thank you for contributing the PR! We'll review it soon to ensure others benefit from your fix.