Reputation: 147
Is it possible to use Google ML-Kit On-Device Text Recognition in Flutter? All of the tutorials and resources I am finding online are all firebase_ml_vision
, but I am looking for one that uses the no-cost OCR from Google ML-Kit. How would I do this in Flutter?
Upvotes: 4
Views: 7863
Reputation: 189
ML Kit for Text Recognition
Follow the installation of Google ML Kit (https://pub.dev/packages/google_ml_kit). (Flutter 2.8)
Set this thing up
bool hasImage = false;
File? image;
TextDetector textDetector = GoogleMlKit.vision.textDetector();
String? imagePath;
String scanText = '';
To get the image, use image picker: https://pub.dev/packages/image_picker
Future getImage(ImageSource source) async {
try {
final image = await ImagePicker().pickImage(source: source);
if (image == null) return;
final imageTemporary = File(image.path);
setState(() {
this.image = imageTemporary;
imagePath = imageTemporary.path;
debugPrint(imagePath);
hasImage = true;
});
} on PlatformException catch (e) {
debugPrint('Failed to pick image: $e');
}
}
Code for getting the text from the image
Future getText(String path) async {
final inputImage = InputImage.fromFilePath(path);
final RecognisedText recognisedText =
await textDetector.processImage(inputImage);
for (TextBlock block in recognisedText.blocks) {
for (TextLine line in block.lines) {
for (TextElement element in line.elements) {
setState(() {
scanText = scanText + ' ' + element.text;
debugPrint(scanText);
});
}
scanText = scanText + '\n';
}
}
}
Call the getText
function and pass the image path. getText(imagePath)
.
Upvotes: 1
Reputation: 11
Yes surely you can use this package https/pub.dev/packages/mlkit this is google's mlkit. OCR has also support for both ios and android. Happy Coding ;)
Upvotes: 1
Reputation: 173
use this package https://pub.dev/packages/camera_google_ml_vision It's exactly the same in terms of how to use firebase_ml_vision
Upvotes: 1
Reputation: 46
as @Sayan Nath said you can use mlkit
, but I think the better choice would be google_ml_kit
, the firebase team who have worked on firebase_ml_vision also recommended using it.
Upvotes: 2