Skip to content

Instantly share code, notes, and snippets.

@BoHellgren
Last active March 4, 2020 12:25
Show Gist options
  • Save BoHellgren/d730975dfc1b91a24f8cc6112c38c2fd to your computer and use it in GitHub Desktop.
Save BoHellgren/d730975dfc1b91a24f8cc6112c38c2fd to your computer and use it in GitHub Desktop.
_classifyDog MLKit version
Future<String> _classifyDog(imglib.Image img) async {
Uint8List png = imglib.encodePng(img);
List<VisionLabel> _cloudLabels =
await labelDetector.detectFromBinary(png, true);
for (int i = 0; i < _cloudLabels.length; i++) {
String foundLabel = _cloudLabels[i].label;
print(foundLabel);
if (isBreed(foundLabel)) {
String conf = (_cloudLabels[i].confidence * 100).toStringAsFixed(0);
print(foundLabel + ' ' + conf);
return (foundLabel + ' (' + conf + '%)');
}
}
return null;
}
bool isBreed(String label) {
List labelWords = label.split(' ');
for (int i = 0; i < labelWords.length; i++) {
String word = labelWords[i];
List<String> ignoreLabels = [
'Dog',
'Dog breed',
'Canidae',
'Vertebrate',
'Carnivore',
'Mammal',
'Snout',
'Puppy',
'Grass',
'Sky'
];
if (!ignoreLabels.contains(word)) {
for (int i = 0; i < breedTable.length; i++) {
String aBreed = breedTable[i][0];
if (aBreed.contains(word.toUpperCase())) return true;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment