Skip to content

Instantly share code, notes, and snippets.

@BoHellgren
Created January 8, 2020 16:09
Show Gist options
  • Save BoHellgren/16b8f4876cfdd5c194acc1bb139679ca to your computer and use it in GitHub Desktop.
Save BoHellgren/16b8f4876cfdd5c194acc1bb139679ca to your computer and use it in GitHub Desktop.
_classifyDog
Future<String> _classifyDog(imglib.Image croppedImage) async {
while (_tfliteBusy) await Future.delayed(Duration(milliseconds: 100));
_tfliteBusy = true;
Uint8List croppedPng = imglib.encodePng(croppedImage);
try {
File(_tempPath).deleteSync();
} catch (e) {
print(e);
}
File(_tempPath).writeAsBytesSync(croppedPng);
await Tflite.loadModel(
model: 'assets/dogs.tflite',
labels: 'assets/dog_labels.txt',
);
var resultList = await Tflite.runModelOnImage(
path: _tempPath,
numResults: 2,
threshold: 0.05,
imageMean: 127.5,
imageStd: 127.5,
);
if (resultList.length == 0) return 'Cannot determine breed';
String breed = resultList[0]["label"].replaceAll('\t', ' ').substring(10);
breed = breed[0].toUpperCase() + breed.substring(1);
String conf = (resultList[0]["confidence"] * 100).toStringAsFixed(0);
String reply = breed + ' (' + conf + '%)';
if (resultList.length > 1) {
breed = resultList[1]["label"].replaceAll('\t', ' ').substring(10);
breed = breed[0].toUpperCase() + breed.substring(1);
conf = (resultList[1]["confidence"] * 100).toStringAsFixed(0);
reply = reply + '\n' + breed + ' (' + conf + '%)';
}
await Tflite.loadModel(
model: "assets/ssd_mobilenet.tflite",
labels: "assets/ssd_mobilenet.txt");
_tfliteBusy = false;
return reply;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment