1. Create a FirebaseVisionImage
.
Create a FirebaseVisionImage
object from your image. To create a FirebaseVisionImage
from an image File
object:
final File imageFile = getImageFile();
final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(imageFile);
2. Create an instance of a detector.
final BarcodeDetector barcodeDetector = FirebaseVision.instance.barcodeDetector();
final FaceDetector faceDetector = FirebaseVision.instance.faceDetector();
final ImageLabeler labeler = FirebaseVision.instance.imageLabeler();
final ImageLabeler cloudLabeler = FirebaseVision.instance.cloudImageLabeler();
final TextRecognizer textRecognizer = FirebaseVision.instance.textRecognizer();
final TextRecognizer cloudTextRecognizer = FirebaseVision.instance.cloudTextRecognizer();
final DocumentTextRecognizer cloudDocumentTextRecognizer = FirebaseVision.instance.cloudDocumentTextRecognizer();
You can also configure all detectors, except on-device TextRecognizer
, with desired options.
final ImageLabeler labeler = FirebaseVision.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 List<ImageLabel> cloudLabels = await cloudLabeler.processImage(visionImage);
final VisionText visionText = await textRecognizer.processImage(visionImage);
final VisionText visionText = await cloudTextRecognizer.processImage(visionImage);
final VisionDocumentText visionDocumentText = await cloudDocumentTextRecognizer.processImage(visionImage);
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
}
}
}
d. Extract document text.
String text = visionDocumentText.text;
for (DocumentTextBlock block in visionDocumentText.blocks) {
final Rect boundingBox = block.boundingBox;
final String text = block.text;
final List<RecognizedLanguage> languages = block.recognizedLanguages;
final DocumentTextRecognizedBreak = block.recognizedBreak;
for (DocumentTextParagraph paragraph in block.paragraphs) {
// Same getters as DocumentTextBlock
for (DocumentTextWord word in paragraph.words) {
// Same getters as DocumentTextBlock
for (DocumentTextSymbol symbol in word.symbols) {
// Same getters as DocumentTextBlock
}
}
}
}
5. Release resources with close()
.
barcodeDetector.close();
cloudLabeler.close();
faceDetector.close();
labeler.close();
textRecognizer.close();
documentTextRecognizer.close();