google ml kit vision plugin
a flutter plugin to use the capabilities of on-device google ml kit vision apis
usage
to use this plugin, add google_ml_vision
as a dependency in your pubspec.yaml file.
using an ml vision detector
1. create a googlevisionimage
.
create a googlevisionimage
object from your image. to create a googlevisionimage
from an image file
object:
final file imagefile = getimagefile();
final googlevisionimage visionimage = googlevisionimage.fromfile(imagefile);
2. create an instance of a detector.
final barcodedetector barcodedetector = googlevision.instance.barcodedetector();
final facedetector facedetector = googlevision.instance.facedetector();
final imagelabeler labeler = googlevision.instance.imagelabeler();
final textrecognizer textrecognizer = googlevision.instance.textrecognizer();
you can also configure all detectors, except textrecognizer
, with desired options.
final imagelabeler labeler = googlevision.instance.imagelabeler(
imagelabeleroptions(confidencethreshold: 0.75),
);
3. call detectinimage()
or processimage()
with visionimage
.
final list<barcode> barcodes = await barcodedetector.detectinimage(visionimage);
final list<face> faces = await facedetector.processimage(visionimage);
final list<imagelabel> labels = await labeler.processimage(visionimage);
final visiontext visiontext = await textrecognizer.processimage(visionimage);
4. extract data.
a. extract barcodes.
for (barcode barcode in barcodes) {
final rectangle<int> boundingbox = barcode.boundingbox;
final list<point<int>> cornerpoints = barcode.cornerpoints;
final string rawvalue = barcode.rawvalue;
final barcodevaluetype valuetype = barcode.valuetype;
// see api reference for complete list of supported types
switch (valuetype) {
case barcodevaluetype.wifi:
final string ssid = barcode.wifi.ssid;
final string password = barcode.wifi.password;
final barcodewifiencryptiontype type = barcode.wifi.encryptiontype;
break;
case barcodevaluetype.url:
final string title = barcode.url.title;
final string url = barcode.url.url;
break;
}
}
b. extract faces.
for (face face in faces) {
final rectangle<int> boundingbox = face.boundingbox;
final double roty = face.headeulerangley; // head is rotated to the right roty degrees
final double rotz = face.headeuleranglez; // head is tilted sideways rotz degrees
// if landmark detection was enabled with facedetectoroptions (mouth, ears,
// eyes, cheeks, and nose available):
final facelandmark leftear = face.getlandmark(facelandmarktype.leftear);
if (leftear != null) {
final point<double> leftearpos = leftear.position;
}
// if classification was enabled with facedetectoroptions:
if (face.smilingprobability != null) {
final double smileprob = face.smilingprobability;
}
// if face tracking was enabled with facedetectoroptions:
if (face.trackingid != null) {
final int id = face.trackingid;
}
}
c. extract labels.
for (imagelabel label in labels) {
final string text = label.text;
final string entityid = label.entityid;
final double confidence = label.confidence;
}
d. extract text.
string text = visiontext.text;
for (textblock block in visiontext.blocks) {
final rect boundingbox = block.boundingbox;
final list<offset> cornerpoints = block.cornerpoints;
final string text = block.text;
final list<recognizedlanguage> languages = block.recognizedlanguages;
for (textline line in block.lines) {
// same getters as textblock
for (textelement element in line.elements) {
// same getters as textblock
}
}
}
5. release resources with close()
.
barcodedetector.close();
facedetector.close();
labeler.close();
textrecognizer.close();
getting started
see the example
directory for a complete sample app using google machine learning.
Comments are closed.