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

Removed OpenCV as dependency #91

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

estebanuri
Copy link

@estebanuri estebanuri commented Jan 29, 2025

🛠️ PR Summary

In this PR, the OpenCV dependency was removed from the project, in the quest to reduce the binary size.
Since very shallow OpenCV functionality is used, it is easy to implement it directly in cpp, avoiding importing the entire library.


📊 Key Changes

  • The OpenCV library was removed

  • Android library Native cpp code

    • CMakeLists.txt: commented OpenCV since is not used anymore.
    • types.hpp: added new file with required code extracted from OpenCV (which includes Rect_, Size_ and Point_ classes)
    • ultralytics.h: the new types.hpp was included, and the DetectedObject was redefined by changing the use of cv::Rect_ to the Rect_ class.
    • tflite_detect.cpp: changed the use of cv::Rect_ to Rect_ class:

🎯 Purpose & Impact

If we want to develop final applications, it is important to consider the final product.
Many users will think twice before installing hundreds of Megabytes on their smartphones.

The main purpose of this PR is to try to reduce the binary size.

As explained this PR removes the OpenCV dependency since the only used functionality is the class Rect_, that can be extracted into the cpp code making it minimal.

If we compile the app using flutter build apk --release:

    Running Gradle task 'assembleRelease'...                           60.7s
    ✓ Built build/app/outputs/flutter-apk/app-release.apk (487.0MB)

The resulting binary file is huge!

Actually we can create an app-bundle, so when deploying the Google Play Store will divide the required files for each abi, reducing the final binary size. To do that we can use a command like flutter build appbundle --release:

    Running Gradle task 'bundleRelease'...                             89.5s
    ✓ Built build/app/outputs/bundle/release/app-release.aab (168.1MB)
    

Still very big!

Let's see if we can find out why using the command flutter build appbundle --release --target-platform android-arm64 --analyze-size:

    app-release.aab (total compressed)                                        149 MB
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    BUNDLE-METADATA/
        com.android.tools.build.libraries                                       4 KB
        com.android.tools.build.obfuscation                                   807 KB
    base/
        assets                                                                 15 MB
        dex                                                                   573 KB
        lib                                                                   132 MB
        Dart AOT symbols accounted decompressed size                            3 MB
        package:flutter                                                       1 MB
        dart:core                                                           257 KB
        dart:typed_data                                                     159 KB
        dart:ui                                                             156 KB
        dart:async                                                           87 KB
        package:material_color_utilities                                     76 KB
        dart:convert                                                         49 KB
        dart:io                                                              39 KB
        dart:collection                                                      38 KB
        dart:isolate                                                         23 KB
        package:vector_math/
            vector_math_64.dart                                                23 KB
        package:ultralytics_yolo                                             13 KB
        dart:ffi                                                             11 KB
        package:path                                                          9 KB
        package:ultralytics_yolo_example/
            main.dart                                                           6 KB
        package:permission_handler_platform_interface                         4 KB
        dart:math                                                             2 KB
        package:path_provider_android                                         2 KB
        dart:mirrors                                                          1 KB
        package:path_provider_platform_interface                              1 KB
        manifest                                                                2 KB
        res                                                                   137 KB
        resources.pb                                                           42 KB
        root                                                                   11 KB
    META-INF/
        ANDROIDD.SF                                                            19 KB
        ANDROIDD.RSA                                                          1016 B
        MANIFEST.MF                                                            18 KB
    ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
    A summary of your AAB analysis can be found at: /Users/esteban/.flutter-devtools/aab-code-size-analysis_06.json

    To analyze your app size in Dart DevTools, run the following command:
    dart devtools --appSizeBase=aab-code-size-analysis_06.json
    ✓ Built build/app/outputs/bundle/release/app-release.aab (156.0MB)
    

As we can see the lib bundled directory is 132 MB.

The first thing I thought was, the binary file is very large because it includes the OpenCV library, but upon closer inspection it is seen that although OpenCV increases a few Megabytes, it is not the biggest problem with the size of the binary.

For example, for an arm-64 Android device, the binary reduction by removing the OpenCV dependency would be 12 MB.
image


I have read the CLA Document and I sign the CLA

Copy link

github-actions bot commented Jan 29, 2025


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@UltralyticsAssistant UltralyticsAssistant added dependencies Dependencies and packages enhancement New feature or request labels Jan 29, 2025
@UltralyticsAssistant
Copy link
Member

👋 Hello @estebanuri, thank you for submitting this awesome PR to the ultralytics/yolo-flutter-app 🚀 repository! We appreciate the time and effort you've put into optimizing the project by removing the OpenCV dependency and streamlining the binary size 👏. To ensure a smooth integration, please check the following guidelines:

  • Clearly Define the Purpose: The purpose of your PR is well-articulated with an in-depth explanation and accompanying data 🙌. If there are any associated issues, feel free to link them for better traceability!
  • Keep Your Branch Updated: Make sure your branch is synchronized with the latest main branch. You can do this by clicking the 'Update branch' button or running git pull and git merge main locally to resolve any conflicts.
  • Pass CI Checks: Verify that all Continuous Integration (CI) checks are passing. If you encounter any issues, kindly address them for a seamless review process. More details on Ultralytics CI can be found here.
  • Update Supporting Documentation: If any functionality has been updated, please ensure relevant documentation is modified to reflect the changes for future maintainers and contributors.
  • Include Minimal Reproducible Examples (MRE): If possible, include an MRE to validate the changes directly, especially in cases involving performance optimizations or library refactoring 🤓.
  • Add or Modify Tests: If applicable, ensure that sufficient test coverage is maintained for the new implementation. Confirm all tests pass before requesting a merge.
  • Sign the Contributor License Agreement (CLA): It's great that you’ve signed the CLA ✅. This ensures compliance with our open-source contribution policies.

You’ve made an excellent contribution by removing unused dependencies and improving efficiency. For further details on our contribution process, please refer to the Contributing Guide.

Please note, this is an automated response 🤖. An Ultralytics engineer will review your PR as soon as possible to provide additional insights or approval. Thank you for your dedication to Ultralytics! 🌟

@estebanuri estebanuri force-pushed the feature/strip-opencv branch from 667a28a to 7259a1a Compare January 30, 2025 11:53
@estebanuri
Copy link
Author

recheck

@estebanuri estebanuri force-pushed the feature/strip-opencv branch from e60493e to 5689855 Compare January 30, 2025 13:35
@estebanuri estebanuri force-pushed the feature/strip-opencv branch from 5689855 to 4d5e9e7 Compare January 30, 2025 13:37
@estebanuri
Copy link
Author

recheck

@pderrenger
Copy link
Member

Thank you for your PR! We'll review it shortly. For future contributions, please ensure all changes align with our Contributing Guide and maintain compatibility with Ultralytics' cross-platform vision AI workflows. While OpenCV is typically integral for computer vision tasks, we'll evaluate this platform-specific optimization carefully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Dependencies and packages enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants