@@ -21,51 +21,84 @@ import 'third_party/mediapipe/generated/mediapipe_common_bindings.dart'
21
21
/// classifier's desired behavior.
22
22
class BaseOptions extends Equatable {
23
23
/// Generative constructor that creates a [BaseOptions] instance.
24
- const BaseOptions ({this .modelAssetBuffer, this .modelAssetPath})
25
- : assert (
24
+ const BaseOptions ._({
25
+ this .modelAssetBuffer,
26
+ this .modelAssetPath,
27
+ this .modelAssetBufferCount,
28
+ required _BaseOptionsType type,
29
+ }) : assert (
26
30
! (modelAssetBuffer == null && modelAssetPath == null ),
27
31
'You must supply either `modelAssetBuffer` or `modelAssetPath`' ,
28
32
),
29
33
assert (
30
34
! (modelAssetBuffer != null && modelAssetPath != null ),
31
35
'You must only supply one of `modelAssetBuffer` and `modelAssetPath`' ,
32
- );
36
+ ),
37
+ assert (
38
+ (modelAssetBuffer == null ) == (modelAssetBufferCount == null ),
39
+ 'modelAssetBuffer and modelAssetBufferCount must only be submitted '
40
+ 'together' ,
41
+ ),
42
+ _type = type;
43
+
44
+ /// Constructor for [BaseOptions] classes using a file system path.
45
+ ///
46
+ /// In practice, this is unsupported, as assets in Flutter are bundled into
47
+ /// the build output and not available on disk. However, it can potentially
48
+ /// be helpful for testing / development purposes.
49
+ factory BaseOptions .path (String path) => BaseOptions ._(
50
+ modelAssetPath: path,
51
+ type: _BaseOptionsType .path,
52
+ );
53
+
54
+ /// Constructor for [BaseOptions] classes using an in-memory pointer to the
55
+ /// MediaPipe SDK.
56
+ ///
57
+ /// In practice, this is the only option supported for production builds.
58
+ factory BaseOptions .memory (Uint8List buffer) {
59
+ return BaseOptions ._(
60
+ modelAssetBuffer: buffer,
61
+ modelAssetBufferCount: buffer.lengthInBytes,
62
+ type: _BaseOptionsType .memory,
63
+ );
64
+ }
33
65
34
66
/// The model asset file contents as bytes;
35
67
final Uint8List ? modelAssetBuffer;
36
68
69
+ /// The size of the model assets buffer (or `0` if not set).
70
+ final int ? modelAssetBufferCount;
71
+
37
72
/// Path to the model asset file.
38
73
final String ? modelAssetPath;
39
74
75
+ final _BaseOptionsType _type;
76
+
40
77
/// Converts this pure-Dart representation into C-memory suitable for the
41
78
/// MediaPipe SDK to instantiate various classifiers.
42
79
Pointer <bindings.BaseOptions > toStruct () {
43
80
final struct = calloc< bindings.BaseOptions > ();
44
81
45
- if (modelAssetPath != null ) {
82
+ if (_type == _BaseOptionsType .path ) {
46
83
struct.ref.model_asset_path = prepareString (modelAssetPath! );
47
84
}
48
- if (modelAssetBuffer != null ) {
85
+ if (_type == _BaseOptionsType .memory ) {
49
86
struct.ref.model_asset_buffer = prepareUint8List (modelAssetBuffer! );
50
87
struct.ref.model_asset_buffer_count = modelAssetBuffer! .lengthInBytes;
51
88
}
52
89
return struct;
53
90
}
54
91
55
92
@override
56
- List <Object ?> get props => [modelAssetBuffer, modelAssetPath];
57
-
58
- /// Releases all C memory held by this [bindings.BaseOptions] struct.
59
- static void freeStruct (bindings.BaseOptions struct) {
60
- if (struct.model_asset_buffer.address != 0 ) {
61
- calloc.free (struct.model_asset_buffer);
62
- }
63
- if (struct.model_asset_path.address != 0 ) {
64
- calloc.free (struct.model_asset_path);
65
- }
66
- }
93
+ List <Object ?> get props => [
94
+ modelAssetBuffer,
95
+ modelAssetPath,
96
+ modelAssetBufferCount,
97
+ ];
67
98
}
68
99
100
+ enum _BaseOptionsType { path, memory }
101
+
69
102
/// Dart representation of MediaPipe's "ClassifierOptions" concept.
70
103
///
71
104
/// Classifier options shared across MediaPipe classification tasks.
0 commit comments