-
Notifications
You must be signed in to change notification settings - Fork 22
Adds mediapipe_core package #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8ddef51
adds mediapipe_core package
craiglabenz 688371a
adds makefile for all packages
craiglabenz 4a2e4d8
fixes typo in Makefile
craiglabenz 8f0c39c
Apply suggestions from code review
Piinks 7b1d5ad
Update Makefile
Piinks ff83c89
Apply suggestions from code review
Piinks a39e75b
updated generated code's location and license (for 3P status)
craiglabenz 006e8ef
code review responses
craiglabenz 37ea84d
updated README
craiglabenz 61dbdac
setup sharing of base analysis_options
craiglabenz b93054b
Update packages/mediapipe-core/lib/src/containers.dart
craiglabenz 10a8589
Update packages/mediapipe-core/lib/src/containers.dart
craiglabenz e02eece
add free methods to core structs
craiglabenz 7e11eef
code review updates to options
craiglabenz 0aae904
adds publish blocker
craiglabenz 48dbf2e
Add GitHub actions for CI/CD (#14)
craiglabenz bc212e4
adds ffiwrapper to ci/cd
craiglabenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Main Branch CI | ||
|
||
# Declare default permissions as read only. | ||
permissions: read-all | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main, ffi-wrapper, ffi-wrapper-core-pkg] | ||
workflow_dispatch: | ||
schedule: | ||
- cron: "0 0 * * *" # Every day at midnight | ||
|
||
defaults: | ||
run: | ||
shell: bash | ||
|
||
jobs: | ||
flutter-tests: | ||
name: Test mediapipe_core against ${{ matrix.flutter_version }} | ||
runs-on: ${{ matrix.os }} | ||
# Skip running job on forks | ||
if: github.repository == 'google/flutter-mediapipe' | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
flutter_version: [stable, beta, master] | ||
# TODO(craiglabenz): Add `ubuntu-latest` and `windows-latest` when those artifacts exist | ||
os: [macos-latest] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: subosito/flutter-action@v2 | ||
with: | ||
channel: ${{ matrix.flutter_version }} | ||
- run: ./tool/mediapipe_ci_script_${{ matrix.flutter_version }}.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Runs the utility to pull in all header files from `google/mediapipe` | ||
headers: | ||
cd packages/build_cmd && dart bin/main.dart headers | ||
|
||
# Runs `ffigen` for all packages | ||
generate: generate_core generate_text | ||
|
||
# Runs `ffigen` for all packages, compiles the faked C artifacts, and runs all tests | ||
test: generate_text test_text generate_core test_core | ||
|
||
# Runs `ffigen` for all packages and all tests for all packages | ||
test_only: test_core test_text | ||
craiglabenz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Core --- | ||
|
||
# Runs `ffigen` for `mediapipe_core` | ||
generate_core: | ||
cd packages/mediapipe-core && dart run ffigen --config=ffigen.yaml | ||
|
||
# Runs unit tests for `mediapipe_core` | ||
test_core: | ||
cd packages/mediapipe-core && dart test | ||
|
||
# Text --- | ||
|
||
# Runs `ffigen` for `mediapipe_text` | ||
generate_text: | ||
cd packages/mediapipe-task-text && dart run ffigen --config=ffigen.yaml | ||
|
||
# Compiles the faked C artifacts for testing | ||
compile_fake_text: | ||
# Builds standalone executable | ||
cd packages/mediapipe-task-text/test/c && gcc fake_text_classifier.c -o fake_text_classifier | ||
# Builds what Flutter needs | ||
cd packages/mediapipe-task-text/test/c && gcc -static -c -fPIC *.c -o fake_text_classifier.o | ||
cd packages/mediapipe-task-text/test/c && gcc -shared -o fake_text_classifier.dylib fake_text_classifier.o | ||
|
||
# Runs `ffigen` for `mediapipe_text` and all text tests | ||
test_text: compile_fake_text test_text_only | ||
|
||
# Runs all text tests | ||
test_text_only: | ||
cd packages/mediapipe-task-text && flutter test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
include: package:lints/recommended.yaml | ||
|
||
linter: | ||
rules: | ||
- public_member_api_docs # see https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#documentation-dartdocs-javadocs-etc | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# https://dart.dev/guides/libraries/private-files | ||
# Created by `dart pub` | ||
.dart_tool/ | ||
|
||
# Avoid committing pubspec.lock for library packages; see | ||
# https://dart.dev/guides/libraries/private-files#pubspeclock. | ||
pubspec.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 0.0.1 | ||
|
||
- Initial version. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# MediaPipe Core for Flutter | ||
|
||
 | ||
|
||
A Flutter plugin to use the MediaPipe Core API, which enables multiple Mediapipe tasks. | ||
|
||
To learn more about MediaPipe, please visit the [MediaPipe website](https://developers.google.com/mediapipe) | ||
|
||
## Getting Started | ||
|
||
To get started with MediaPipe, please [see the documentation](https://developers.google.com/mediapipe/solutions/guide). | ||
|
||
<!-- ASPIRATIONAL | ||
## Usage | ||
|
||
To use this plugin, please visit the [Core Usage documentation](https://github.com/google/flutter-mediapipe#Usage) | ||
--> | ||
|
||
## Issues and feedback | ||
|
||
Please file Flutter-MediaPipe specific issues, bugs, or feature requests in our [issue tracker](https://github.com/google/flutter-mediapipe/issues/new). | ||
|
||
Issues that are specific to Flutter can be filed in the [Flutter issue tracker](https://github.com/flutter/flutter/issues/new). | ||
|
||
To contribute a change to this plugin, | ||
please review our [contribution guide](https://github.com/google/flutter-mediapipe/blob/master/CONTRIBUTING.md) | ||
and open a [pull request](https://github.com/google/flutter-mediapipe/pulls). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
include: ../analysis_options.yaml | ||
|
||
Piinks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
analyzer: | ||
exclude: | ||
- "**/mediapipe_common_bindings.dart" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: "MediaPipeCommonBindings" | ||
description: "Bindings for shared MediaPipe structs common across many tasks" | ||
output: | ||
craiglabenz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bindings: "lib/src/third_party/mediapipe/generated/mediapipe_common_bindings.dart" | ||
symbol-file: | ||
output: "package:mediapipe_core/generated/core_symbols.yaml" | ||
import-path: "package:mediapipe_core/src/third_party/mediapipe/generated/mediapipe_common_bindings.dart" | ||
headers: | ||
entry-points: | ||
- "third_party/mediapipe/tasks/c/**" | ||
preamble: | | ||
/* Copyright 2023 The MediaPipe Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
ffi-native: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
format_version: 1.0.0 | ||
files: | ||
package:mediapipe_core/src/third_party/mediapipe/generated/mediapipe_common_bindings.dart: | ||
used-config: | ||
ffi-native: false | ||
symbols: | ||
c:@S@BaseOptions: | ||
name: BaseOptions | ||
c:@S@Category: | ||
name: Category | ||
c:@S@ClassificationResult: | ||
name: ClassificationResult | ||
c:@S@Classifications: | ||
name: Classifications | ||
c:@S@ClassifierOptions: | ||
name: ClassifierOptions | ||
c:@S@__darwin_pthread_handler_rec: | ||
name: __darwin_pthread_handler_rec | ||
c:@S@_opaque_pthread_attr_t: | ||
name: _opaque_pthread_attr_t | ||
c:@S@_opaque_pthread_cond_t: | ||
name: _opaque_pthread_cond_t | ||
c:@S@_opaque_pthread_condattr_t: | ||
name: _opaque_pthread_condattr_t | ||
c:@S@_opaque_pthread_mutex_t: | ||
name: _opaque_pthread_mutex_t | ||
c:@S@_opaque_pthread_mutexattr_t: | ||
name: _opaque_pthread_mutexattr_t | ||
c:@S@_opaque_pthread_once_t: | ||
name: _opaque_pthread_once_t | ||
c:@S@_opaque_pthread_rwlock_t: | ||
name: _opaque_pthread_rwlock_t | ||
c:@S@_opaque_pthread_rwlockattr_t: | ||
name: _opaque_pthread_rwlockattr_t | ||
c:@S@_opaque_pthread_t: | ||
name: _opaque_pthread_t | ||
c:@UA@__mbstate_t: | ||
name: __mbstate_t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
/// Package containing core dependencies for MediaPipe's text, vision, and | ||
/// audio-based tasks. | ||
library; | ||
craiglabenz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export 'src/containers.dart' show Category, Classifications; | ||
export 'src/ffi_utils.dart'; | ||
export 'src/task_options.dart' show BaseOptions, ClassifierOptions; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:ffi'; | ||
import 'package:ffi/ffi.dart'; | ||
import 'package:mediapipe_core/mediapipe_core.dart'; | ||
import 'third_party/mediapipe/generated/mediapipe_common_bindings.dart' | ||
as bindings; | ||
|
||
/// Dart representation of MediaPipe's "Category" concept. | ||
/// | ||
/// Category is a util class, that contains a [categoryName], its [displayName], | ||
/// a float value as [score], and the [index] of the label in the corresponding | ||
/// label file. Typically it's used as result of classification or detection | ||
/// tasks. | ||
/// | ||
/// See more: | ||
/// * [MediaPipe's Category documentation](https://developers.google.com/mediapipe/api/solutions/java/com/google/mediapipe/tasks/components/containers/Category) | ||
class Category { | ||
craiglabenz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Generative constructor that creates a [Category] instance. | ||
const Category({ | ||
required this.index, | ||
required this.score, | ||
required this.categoryName, | ||
required this.displayName, | ||
}); | ||
|
||
/// The index of the label in the corresponding label file. | ||
final int index; | ||
|
||
/// The probability score of this label category. | ||
final double score; | ||
|
||
/// The label of this category object. | ||
final String? categoryName; | ||
|
||
/// The display name of the label, which may be translated for different locales. | ||
final String? displayName; | ||
|
||
/// Accepts a pointer to a list of structs, and a count representing the length | ||
/// of the list, and returns a list of pure-Dart [Category] instances. | ||
static List<Category> fromStructs( | ||
Pointer<bindings.Category> structs, | ||
int count, | ||
) { | ||
final categories = <Category>[]; | ||
for (int i = 0; i < count; i++) { | ||
categories.add(fromStruct(structs[i])); | ||
} | ||
return categories; | ||
} | ||
|
||
/// Accepts a pointer to a single struct and returns a pure-Dart [Category] instance. | ||
static Category fromStruct(bindings.Category struct) { | ||
return Category( | ||
index: struct.index, | ||
score: struct.score, | ||
categoryName: toDartString(struct.category_name), | ||
displayName: toDartString(struct.display_name), | ||
); | ||
} | ||
|
||
/// Releases all C memory associated with a list of [bindings.Category] pointers. | ||
/// This method is important to call after calling [Category.fromStructs] to | ||
/// convert that C memory into pure-Dart objects. | ||
static void freeStructs(Pointer<bindings.Category> structs, int count) { | ||
int index = 0; | ||
while (index < count) { | ||
bindings.Category obj = structs[index]; | ||
calloc.free(obj.category_name); | ||
calloc.free(obj.display_name); | ||
index++; | ||
} | ||
calloc.free(structs); | ||
} | ||
|
||
@override | ||
String toString() => 'Category(index=$index, score=$score, ' | ||
'categoryName=$categoryName, displayName=$displayName)'; | ||
} | ||
|
||
/// Dart representation of MediaPipe's "Classifications" concept. | ||
/// | ||
/// Represents the list of classification for a given classifier head. | ||
/// Typically used as a result for classification tasks. | ||
/// | ||
/// See also: | ||
/// * [MediaPipe's Classifications documentation](https://developers.google.com/mediapipe/api/solutions/java/com/google/mediapipe/tasks/components/containers/Classifications) | ||
class Classifications { | ||
/// Generative constructor that creates a [Classifications] instance. | ||
const Classifications({ | ||
required this.categories, | ||
required this.headIndex, | ||
required this.headName, | ||
}); | ||
|
||
/// A list of [Category] objects which contain the actual classification | ||
/// information, including human-readable labels and probability scores. | ||
final List<Category> categories; | ||
|
||
/// The index of the classifier head these entries refer to. | ||
final int headIndex; | ||
|
||
/// The optional name of the classifier head, which is the corresponding | ||
/// tensor metadata name. | ||
final String? headName; | ||
|
||
/// Accepts a pointer to a list of structs, and a count representing the length | ||
/// of the list, and returns a list of pure-Dart [Classifications] instances. | ||
static List<Classifications> fromStructs( | ||
Pointer<bindings.Classifications> structs, | ||
int count, | ||
) { | ||
final classifications = <Classifications>[]; | ||
for (int i = 0; i < count; i++) { | ||
classifications.add(fromStruct(structs[i])); | ||
} | ||
return classifications; | ||
} | ||
|
||
/// Accepts a pointer to a single struct and returns a pure-Dart [Classifications] | ||
/// instance. | ||
static Classifications fromStruct(bindings.Classifications struct) { | ||
return Classifications( | ||
categories: Category.fromStructs( | ||
struct.categories, | ||
struct.categories_count, | ||
), | ||
headIndex: struct.head_index, | ||
headName: toDartString(struct.head_name), | ||
); | ||
} | ||
|
||
/// Releases all C memory associated with a list of [bindings.Classifications] | ||
/// pointers. This method is important to call after calling [Classifications.fromStructs] | ||
/// to convert that C memory into pure-Dart objects. | ||
static void freeStructs( | ||
Pointer<bindings.Classifications> structs, | ||
int count, | ||
) { | ||
int index = 0; | ||
while (index < count) { | ||
bindings.Classifications obj = structs[index]; | ||
Category.freeStructs(obj.categories, obj.categories_count); | ||
calloc.free(obj.head_name); | ||
index++; | ||
} | ||
calloc.free(structs); | ||
} | ||
|
||
/// Convenience getter for the first [Category] out of the [categories] list. | ||
Category? get firstCategory => | ||
categories.isNotEmpty ? categories.first : null; | ||
|
||
@override | ||
String toString() { | ||
final categoryStrings = categories.map((cat) => cat.toString()).join(', '); | ||
return 'Classification(categories=[$categoryStrings], ' | ||
'headIndex=$headIndex, headName=$headName)'; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.