Skip to content

Commit bde06c9

Browse files
craiglabenzPiinks
andauthored
Adds mediapipe_vision package (#13)
* adds mediapipe_core package * adds makefile for all packages * fixes typo in Makefile * Apply suggestions from code review * Update Makefile * Apply suggestions from code review * updated generated code's location and license (for 3P status) * code review responses including: * comments * licensing * resolving testing nits * updated README * setup sharing of base analysis_options * flutter create * initial commit of vision task * Updates structure of mediapipe_vision package --------- Co-authored-by: Kate Lovett <[email protected]>
1 parent 8cc5e3f commit bde06c9

File tree

26 files changed

+1555
-73
lines changed

26 files changed

+1555
-73
lines changed

packages/mediapipe-core/lib/generated/core_symbols.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
format_version: 1.0.0
22
files:
3-
package:mediapipe_core/src/io/third_party/mediapipe/generated/mediapipe_common_bindings.dart:
4-
used-config:
3+
? package:mediapipe_core/src/io/third_party/mediapipe/generated/mediapipe_common_bindings.dart
4+
: used-config:
55
ffi-native: false
66
symbols:
77
c:@S@BaseOptions:
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:ffi';
6+
import 'package:ffi/ffi.dart';
7+
import 'package:mediapipe_core/mediapipe_core.dart';
8+
import 'third_party/mediapipe/generated/mediapipe_common_bindings.dart'
9+
as bindings;
10+
11+
/// Dart representation of MediaPipe's "Category" concept.
12+
///
13+
/// Category is a util class, that contains a category name, its display name,
14+
/// a float value as score, and the index of the label in the corresponding
15+
/// label file. Typically it's used as result of classification or detection
16+
/// tasks.
17+
///
18+
/// See more:
19+
/// * [MediaPipe's Category documentation](https://developers.google.com/mediapipe/api/solutions/java/com/google/mediapipe/tasks/components/containers/Category)
20+
class Category {
21+
/// Generative constructor that creates a [Category] instance.
22+
const Category({
23+
required this.index,
24+
required this.score,
25+
required this.categoryName,
26+
required this.displayName,
27+
});
28+
29+
/// The index of the label in the corresponding label file.
30+
final int index;
31+
32+
/// The probability score of this label category.
33+
final double score;
34+
35+
/// The label of this category object.
36+
final String? categoryName;
37+
38+
/// The display name of the label, which may be translated for different locales.
39+
final String? displayName;
40+
41+
/// Accepts a pointer to a list of structs, and a count representing the length
42+
/// of the list, and returns a list of pure-Dart [Category] instances.
43+
static List<Category> fromStructs(
44+
Pointer<bindings.Category> structs,
45+
int count,
46+
) {
47+
final categories = <Category>[];
48+
for (int i = 0; i < count; i++) {
49+
categories.add(fromStruct(structs[i]));
50+
}
51+
return categories;
52+
}
53+
54+
/// Accepts a pointer to a single struct and returns a pure-Dart [Category] instance.
55+
static Category fromStruct(bindings.Category struct) {
56+
return Category(
57+
index: struct.index,
58+
score: struct.score,
59+
categoryName: toDartString(struct.category_name),
60+
displayName: toDartString(struct.display_name),
61+
);
62+
}
63+
64+
/// Releases all C memory associated with a list of [bindings.Category] pointers.
65+
/// This method is important to call after calling [Category.fromStructs] to
66+
/// convert that C memory into pure-Dart objects.
67+
static void freeStructs(Pointer<bindings.Category> structs, int count) {
68+
int index = 0;
69+
while (index < count) {
70+
bindings.Category obj = structs[index];
71+
calloc.free(obj.category_name);
72+
calloc.free(obj.display_name);
73+
index++;
74+
}
75+
calloc.free(structs);
76+
}
77+
78+
@override
79+
String toString() => 'Category(index=$index, score=$score, '
80+
'categoryName=$categoryName, displayName=$displayName)';
81+
}
82+
83+
/// Dart representation of MediaPipe's "Classifications" concept.
84+
///
85+
/// Represents the list of classification for a given classifier head.
86+
/// Typically used as a result for classification tasks.
87+
///
88+
/// See also:
89+
/// * [MediaPipe's Classifications documentation](https://developers.google.com/mediapipe/api/solutions/java/com/google/mediapipe/tasks/components/containers/Classifications)
90+
class Classifications {
91+
/// Generative constructor that creates a [Classifications] instance.
92+
const Classifications({
93+
required this.categories,
94+
required this.headIndex,
95+
required this.headName,
96+
});
97+
98+
/// A list of Category objects which contain the actual classification
99+
/// information, including human-readable labels and probability scores.
100+
final List<Category> categories;
101+
102+
/// The index of the classifier head these entries refer to.
103+
final int headIndex;
104+
105+
/// The optional name of the classifier head, which is the corresponding
106+
/// tensor metadata name.
107+
final String? headName;
108+
109+
/// Accepts a pointer to a list of structs, and a count representing the length
110+
/// of the list, and returns a list of pure-Dart [Classifications] instances.
111+
static List<Classifications> fromStructs(
112+
Pointer<bindings.Classifications> structs,
113+
int count,
114+
) {
115+
final classifications = <Classifications>[];
116+
for (int i = 0; i < count; i++) {
117+
classifications.add(fromStruct(structs[i]));
118+
}
119+
return classifications;
120+
}
121+
122+
/// Accepts a pointer to a single struct and returns a pure-Dart [Classifications]
123+
/// instance.
124+
static Classifications fromStruct(bindings.Classifications struct) {
125+
return Classifications(
126+
categories: Category.fromStructs(
127+
struct.categories,
128+
struct.categories_count,
129+
),
130+
headIndex: struct.head_index,
131+
headName: toDartString(struct.head_name),
132+
);
133+
}
134+
135+
/// Releases all C memory associated with a list of [bindings.Classifications]
136+
/// pointers. This method is important to call after calling [Classifications.fromStructs]
137+
/// to convert that C memory into pure-Dart objects.
138+
static void freeStructs(
139+
Pointer<bindings.Classifications> structs,
140+
int count,
141+
) {
142+
int index = 0;
143+
while (index < count) {
144+
bindings.Classifications obj = structs[index];
145+
Category.freeStructs(obj.categories, obj.categories_count);
146+
calloc.free(obj.head_name);
147+
index++;
148+
}
149+
calloc.free(structs);
150+
}
151+
152+
/// Convenience getter for the first [Category] out of the [categories] list.
153+
Category? get firstCategory =>
154+
categories.isNotEmpty ? categories.first : null;
155+
156+
@override
157+
String toString() {
158+
final categoryStrings = categories.map((cat) => cat.toString()).join(', ');
159+
return 'Classification(categories=[$categoryStrings], '
160+
'headIndex=$headIndex, headName=$headName)';
161+
}
162+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:ffi';
6+
import 'dart:typed_data';
7+
import 'package:ffi/ffi.dart';
8+
9+
/// Converts a list of Dart strings into their C memory equivalent.
10+
///
11+
/// See also:
12+
/// * [prepareString]
13+
Pointer<Pointer<Char>> prepareListOfStrings(List<String> values) {
14+
final ptrArray = calloc<Pointer<Char>>(values.length);
15+
for (var i = 0; i < values.length; i++) {
16+
ptrArray[i] = prepareString(values[i]);
17+
}
18+
return ptrArray;
19+
}
20+
21+
/// Converts a single Dart string into its C memory equivalent.
22+
///
23+
/// See also:
24+
/// * [prepareListOfStrings]
25+
Pointer<Char> prepareString(String val) => val.toNativeUtf8().cast<Char>();
26+
27+
/// Converts the C memory representation of a string into a Dart [String]. If the
28+
/// supplied pointer is a null pointer, this function returns `null`.
29+
///
30+
/// See also:
31+
/// * [toDartStrings]
32+
String? toDartString(Pointer<Char> val) {
33+
if (val.address == 0) return null;
34+
return val.cast<Utf8>().toDartString();
35+
}
36+
37+
/// Converts a list of C memory representations of strings into a list of Dart
38+
/// [String]s.
39+
///
40+
/// See also:
41+
/// * [toDartString]
42+
List<String?> toDartStrings(Pointer<Pointer<Char>> val, [int? length]) =>
43+
length != null
44+
? _toDartStringsWithCount(val, length)
45+
: _toStartStringsUntilNull(val);
46+
47+
List<String?> _toStartStringsUntilNull(Pointer<Pointer<Char>> val) {
48+
final dartStrings = <String?>[];
49+
int counter = 0;
50+
while (true) {
51+
if (val[counter].address == 0) break;
52+
dartStrings.add(toDartString(val[counter]));
53+
counter++;
54+
}
55+
return dartStrings;
56+
}
57+
58+
List<String?> _toDartStringsWithCount(Pointer<Pointer<Char>> val, int length) {
59+
final dartStrings = <String?>[];
60+
int counter = 0;
61+
while (counter < length) {
62+
dartStrings.add(toDartString(val[counter]));
63+
counter++;
64+
}
65+
return dartStrings;
66+
}
67+
68+
/// Converts Dart's representation for binary data, a [Uint8List], into its C
69+
/// memory representation.
70+
Pointer<Char> prepareUint8List(Uint8List ints) {
71+
final Pointer<Uint8> ptr = calloc<Uint8>(ints.length);
72+
ptr.asTypedList(ints.length).setAll(0, ints);
73+
return ptr.cast();
74+
}
75+
76+
/// Converts a pointer to binary data in C memory to Dart's representation for
77+
/// binary data, a [Uint8List].
78+
Uint8List toUint8List(Pointer<Char> val, {int? length}) {
79+
final codeUnits = val.cast<Uint8>();
80+
if (length != null) {
81+
RangeError.checkNotNegative(length, 'length');
82+
} else {
83+
length = _length(codeUnits);
84+
}
85+
return codeUnits.asTypedList(length);
86+
}
87+
88+
// Counts the non-null bytes in a string to determine its length
89+
int _length(Pointer<Uint8> codeUnits) {
90+
var length = 0;
91+
while (codeUnits[length] != 0) {
92+
length++;
93+
}
94+
return length;
95+
}

0 commit comments

Comments
 (0)