Last active
March 4, 2020 12:25
-
-
Save BoHellgren/d730975dfc1b91a24f8cc6112c38c2fd to your computer and use it in GitHub Desktop.
_classifyDog MLKit version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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