predict method
Predicts from image
whether there is face on the image
Returns the 1 face with highest confidence if its probability os over threshold
Implementation
Map<String, dynamic>? predict(image_lib.Image image) {
final options = OptionsFace(
numClasses: 1,
numBoxes: 896,
numCoords: 16,
keypointCoordOffset: 4,
ignoreClasses: [],
scoreClippingThresh: 100.0,
minScoreThresh: 0.75,
numKeypoints: 6,
numValuesPerKeypoint: 2,
reverseOutputOrder: true,
boxCoordOffset: 0,
xScale: 128,
yScale: 128,
hScale: 128,
wScale: 128);
if (Platform.isAndroid) {
image = image_lib.copyRotate(image, -90);
image = image_lib.flipHorizontal(image);
}
final tensorImage = TensorImage(TfLiteType.float32);
tensorImage.loadImage(image);
final inputImage = getProcessedImage(tensorImage);
TensorBuffer outputFaces = TensorBufferFloat(outputsShapes[0]);
TensorBuffer outputScores = TensorBufferFloat(outputsShapes[1]);
final inputs = <Object>[inputImage.buffer];
final outputs = <int, Object>{
0: outputFaces.buffer,
1: outputScores.buffer,
};
interpreter.runForMultipleInputs(inputs, outputs);
final rawBoxes = outputFaces.getDoubleList();
final rawScores = outputScores.getDoubleList();
var detections = process(
options: options,
rawScores: rawScores,
rawBoxes: rawBoxes,
anchors: _anchors);
detections = nonMaximumSuppression(detections, threshold);
if (detections.isEmpty) {
return {};
}
final rectFaces = <Map<String, dynamic>>[];
for (var detection in detections) {
Rect? bbox;
final score = detection.score;
if (score > threshold) {
bbox = Rect.fromLTRB(
inputImage.width * detection.xMin,
inputImage.height * detection.yMin,
inputImage.width * detection.width,
inputImage.height * detection.height,
);
bbox = _imageProcessor.inverseTransformRect(
bbox, image.height, image.width);
}
rectFaces.add({'bbox': bbox, 'score': score});
}
rectFaces.sort((a, b) => b['score'].compareTo(a['score']));
return rectFaces[0];
}