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

[WIP] FFI Implementation #7

Closed
wants to merge 0 commits into from
Closed

[WIP] FFI Implementation #7

wants to merge 0 commits into from

Conversation

craiglabenz
Copy link
Collaborator

@craiglabenz craiglabenz commented Aug 17, 2023

Summary

Adds a MediaPipe implementation that talks directly to C++ via FFI. (Technically, indirectly and through a C intermediary, but still fairly directly in comparison to method channels.)

C bridge code uses Dart's native FFI functionality and ffigen to automatically generate the required bindings from C headers pulled from the MediaPipe repository.

TODOs

Note: This is a feature / release branch, which means only fully reviewed changes are able to land here via their own PRs.

TESTING

To run this PR locally, complete the following steps (on macOS):

  1. In a checkout of google/mediapipe with the latest internal changes pulled down, run this build command:
bazel build --config darwin_arm64 --strip always -c opt --define MEDIAPIPE_DISABLE_GPU=1 mediapipe/tasks/c/text/text_classifier:libtext_classifier.dylib
  1. Copy the resulting libtext_classifier.dylib file (found in the bazel-out directory) to this repository at the packages/mediapipe-task-text/examples/assets directory.

Note: This will move up a level to mediapipe-task-text/assets after #9.

  1. Download bert_classifier.tflite and place it in the same directory.

  2. Run the example app and click the floating action button to classify text.

@google google deleted a comment from PaulTR Aug 23, 2023
@craiglabenz craiglabenz marked this pull request as draft August 23, 2023 15:55
Copy link
Member

@Piinks Piinks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite finished reviewing, should be able to return this evening or tomorrow morning. :)
Mainly some context questions below.

This is still a draft, so still plenty of TODOs and documentation to fill in.

[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note to self to come back and review this when it;s filled out. :)

packages/mediapipe-core/lib/src/task_options.dart Outdated Show resolved Hide resolved
'You must supply either `modelAssetBuffer` or `modelAssetPath`',
),
assert(
!(modelAssetBuffer != null && modelAssetPath != null),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this ensures both are not provided.

Might be worth unpacking both into a single assertion with a function body so you could use control flow for better readability and error messaging.

}

class ClassifierOptions extends Equatable {
const ClassifierOptions({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there requirements that any of these values are set? What happens if they all are null?

description: A starting point for Dart libraries or applications.
version: 1.0.0

repository: https://github.com/google/flutter-mediapipe
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an issue tracker? In flutter/packages we usually include an issue_tracker


analyzer:
exclude:
- "lib/src/mediapipe_common_bindings.dart"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why for do we exclude this?

Copy link
Collaborator Author

@craiglabenz craiglabenz Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's generated code, so occasionally it will fall behind our latest best practices in flutter_lint and cause IDE analysis server noise.


analyzer:
exclude:
- "lib/src/mediapipe_text_bindings.dart"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same "its generated code" answer :)

// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary if it is excluded in the analysis options?

// Generated by `package:ffigen`.
// ignore_for_file: type=lint
import 'dart:ffi' as ffi;
import 'package:mediapipe_core/src/mediapipe_common_bindings.dart' as imp1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is imp1 short for?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's "implementation one", but this is generated code.


typedef TextClassifierResult = imp1.ClassificationResult;

const int true1 = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these super fun values? Maybe I am missing context

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the context is that C is bad at booleans 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants