|
| 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 | +} |
0 commit comments