@@ -9,11 +9,12 @@ import 'package:tflite_flutter_helper/tflite_flutter_helper.dart';
9
9
10
10
import 'stats.dart' ;
11
11
12
+ /// Classifier
12
13
class Classifier {
13
14
/// Instance of Interpreter
14
15
Interpreter _interpreter;
15
16
16
- /// Instance of loaded labels
17
+ /// Labels file loaded as list
17
18
List <String > _labels;
18
19
19
20
static const String MODEL_FILE_NAME = "detect.tflite" ;
@@ -79,6 +80,7 @@ class Classifier {
79
80
}
80
81
}
81
82
83
+ /// Pre-process the image
82
84
TensorImage getProcessedImage (TensorImage inputImage) {
83
85
padSize = max (inputImage.height, inputImage.width);
84
86
if (imageProcessor == null ) {
@@ -91,7 +93,8 @@ class Classifier {
91
93
return inputImage;
92
94
}
93
95
94
- List predict (imageLib.Image image) {
96
+ /// Runs object detection on the input image
97
+ Map <String , dynamic > predict (imageLib.Image image) {
95
98
var predictStartTime = DateTime .now ().millisecondsSinceEpoch;
96
99
97
100
if (_interpreter == null ) {
@@ -110,16 +113,16 @@ class Classifier {
110
113
var preProcessElapsedTime =
111
114
DateTime .now ().millisecondsSinceEpoch - preProcessStart;
112
115
113
- // Inputs object for runForMultipleInputs
114
- // Use [TensorImage.buffer] or [TensorBuffer.buffer] to pass by reference
115
- List <Object > inputs = [inputImage.buffer];
116
-
117
116
// TensorBuffers for output tensors
118
117
TensorBuffer outputLocations = TensorBufferFloat (_outputShapes[0 ]);
119
118
TensorBuffer outputClasses = TensorBufferFloat (_outputShapes[1 ]);
120
119
TensorBuffer outputScores = TensorBufferFloat (_outputShapes[2 ]);
121
120
TensorBuffer numLocations = TensorBufferFloat (_outputShapes[3 ]);
122
121
122
+ // Inputs object for runForMultipleInputs
123
+ // Use [TensorImage.buffer] or [TensorBuffer.buffer] to pass by reference
124
+ List <Object > inputs = [inputImage.buffer];
125
+
123
126
// Outputs map
124
127
Map <int , Object > outputs = {
125
128
0 : outputLocations.buffer,
@@ -129,6 +132,8 @@ class Classifier {
129
132
};
130
133
131
134
var inferenceTimeStart = DateTime .now ().millisecondsSinceEpoch;
135
+
136
+ // run inference
132
137
_interpreter.runForMultipleInputs (inputs, outputs);
133
138
134
139
var inferenceTimeElapsed =
@@ -137,8 +142,6 @@ class Classifier {
137
142
// Maximum number of results to show
138
143
int resultsCount = min (NUM_RESULTS , numLocations.getIntValue (0 ));
139
144
140
- List <Recognition > recognitions = [];
141
-
142
145
// Using labelOffset = 1 as ??? at index 0
143
146
int labelOffset = 1 ;
144
147
@@ -153,15 +156,23 @@ class Classifier {
153
156
width: INPUT_SIZE ,
154
157
);
155
158
159
+ List <Recognition > recognitions = [];
160
+
156
161
for (int i = 0 ; i < resultsCount; i++ ) {
157
- var label = _labels. elementAt (outputClasses. getIntValue (i) + labelOffset);
162
+ // Prediction score
158
163
var score = outputScores.getDoubleValue (i);
159
164
160
- // inverse of rect
161
- Rect transformedRect = imageProcessor. inverseTransformRect (
162
- locations[i], image.height, image.width );
165
+ // Label string
166
+ var labelIndex = outputClasses. getIntValue (i) + labelOffset;
167
+ var label = _labels. elementAt (labelIndex );
163
168
164
169
if (score > THRESHOLD ) {
170
+ // inverse of rect
171
+ // [locations] corresponds to the image size 300 X 300
172
+ // inverseTransformRect transforms it our [inputImage]
173
+ Rect transformedRect = imageProcessor.inverseTransformRect (
174
+ locations[i], image.height, image.width);
175
+
165
176
recognitions.add (
166
177
Recognition (i, label, score, transformedRect),
167
178
);
@@ -171,16 +182,13 @@ class Classifier {
171
182
var predictElapsedTime =
172
183
DateTime .now ().millisecondsSinceEpoch - predictStartTime;
173
184
174
- // print(
175
- // 'Classifier.predict | Pre-process: $preProcessElapsedTime ms | Inference: $inferenceTimeElapsed ms | Total: $predictElapsedTime');
176
-
177
- return [
178
- recognitions,
179
- Stats (
185
+ return {
186
+ "recognitions" : recognitions,
187
+ "stats" : Stats (
180
188
totalPredictTime: predictElapsedTime,
181
189
inferenceTime: inferenceTimeElapsed,
182
190
preProcessingTime: preProcessElapsedTime)
183
- ] ;
191
+ } ;
184
192
}
185
193
186
194
/// Gets the interpreter instance
0 commit comments