Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
Text scanning in realtime with newer firebase mlkit vision package, b…
Browse files Browse the repository at this point in the history
…ug to be inspected
  • Loading branch information
lewix committed Jan 10, 2019
1 parent 9e50e6c commit 679c744
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 138 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ build/
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

# Firebase
google-services.json
GoogleService-Info.plist
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# mlkit_ocr_realtime
# Flutter OCR realtime

A new Flutter application to show mlkit ocr feature

Expand Down
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "cloud.llabs.flutter.mlkit.ocr"
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 27
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
14 changes: 7 additions & 7 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http:https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSMicrophoneUsageDescription</key>
<string>Apps needs this permission</string>
<key>NSCameraUsageDescription</key>
<string>Apps needs this permission</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Apps needs this permission</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
Expand All @@ -17,7 +11,7 @@
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>mlkit_ocr_realtime</string>
<string>Flutter OCR</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
Expand All @@ -26,8 +20,14 @@
<string>????</string>
<key>CFBundleVersion</key>
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSCameraUsageDescription</key>
<string>Can I use the camera please?</string>
<key>NSMicrophoneUsageDescription</key>
<string>Can I use the mic please?</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
269 changes: 142 additions & 127 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,156 +1,171 @@
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:camera/camera.dart';
import 'dart:async';
import 'package:firebase_ml_vision/firebase_ml_vision.dart';

void main() => runApp(MyApp());
List<CameraDescription> cameras;

class MyApp extends StatelessWidget {
// This widget is the root of your application.
Future<void> main() async {
cameras = await availableCameras();
runApp(OcrApp());
}

class OcrApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
title: "Flutter OCR",
home: Scaffold(
appBar: AppBar(
title: Text("Flutter OCR"),
),
body: CameraPage(),
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
class CameraPage extends StatefulWidget {
@override
_CameraAppState createState() => _CameraAppState();
}

final String title;
class _CameraAppState extends State<CameraPage> {
CameraController controller;
bool _isScanBusy = false;

@override
_MyHomePageState createState() => _MyHomePageState();
}
void initState() {
super.initState();
controller = CameraController(cameras[0], ResolutionPreset.medium);
controller.initialize().then((_) {
if (!mounted) {
return;
}

void _scanText() async {
// controller.startImageStream((CameraImage availableImage) {
// controller.stopImageStream();
// _scanText(availableImage);
// });

if (Platform.isAndroid){
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft
]);
setState(() {});
});
}

var imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
@override
void dispose() {
controller?.dispose();
super.dispose();
}

if (Platform.isAndroid){
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp
]);
@override
Widget build(BuildContext context) {
if (!controller.value.isInitialized) {
return Container();
}
return Column(
children: [
Expanded(
child: _cameraPreviewWidget()
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
MaterialButton(
child: Text("Start Scanning"),
textColor: Colors.white,
color: Colors.blue,
onPressed: () async {
await controller.startImageStream((CameraImage availableImage) {
//controller.stopImageStream();

if (!_isScanBusy)
_scanText(availableImage);
});
}
),
MaterialButton(
child: Text("Stop Scanning"),
textColor: Colors.white,
color: Colors.red,
onPressed: () async => await controller.stopImageStream()
)
]
)
]
);

}


final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(imageFile);
final TextRecognizer textRecognizer = FirebaseVision.instance.textRecognizer();
final VisionText visionText = await textRecognizer.processImage(visionImage);
Widget _cameraPreviewWidget() {
if (controller == null || !controller.value.isInitialized) {
return const Text(
'Tap a camera',
style: TextStyle(
color: Colors.white,
fontSize: 24.0,
fontWeight: FontWeight.w900,
),
);
} else {
return AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: CameraPreview(controller),
);
}
}

void _scanText(CameraImage availableImage) async {
_isScanBusy = true;

print(visionText.text);
for (TextBlock block in visionText.blocks) {
print("scanning!...");

// final Rectangle<int> boundingBox = block.boundingBox;
// final List<Point<int>> cornerPoints = block.cornerPoints;
print(block.text);
final List<RecognizedLanguage> languages = block.recognizedLanguages;
final FirebaseVisionImageMetadata metadata =
FirebaseVisionImageMetadata(
rawFormat: 35,
size: const Size(1.0, 1.0),
planeData: <FirebaseVisionImagePlaneMetadata>[
FirebaseVisionImagePlaneMetadata(
bytesPerRow: 1000,
height: 480,
width: 480,
),
],
);

for (TextLine line in block.lines) {
// Same getters as TextBlock
print(line.text);
for (TextElement element in line.elements) {
// final FirebaseVisionImageMetadata metadata = FirebaseVisionImageMetadata(
// rawFormat: availableImage.format.raw,
// size: Size(1.0, 1.0),
// planeData: <FirebaseVisionImagePlaneMetadata>[
// FirebaseVisionImagePlaneMetadata(
// bytesPerRow: availableImage.planes[0].bytesPerRow,
// height: availableImage.planes[0].height,
// width: availableImage.planes[0].width,
// ),
// ],
// );

final FirebaseVisionImage visionImage = FirebaseVisionImage.fromBytes(availableImage.planes[0].bytes, metadata);
final TextRecognizer textRecognizer = FirebaseVision.instance.textRecognizer();
final VisionText visionText = await textRecognizer.processImage(visionImage);

print(visionText.text);
for (TextBlock block in visionText.blocks) {
// final Rectangle<int> boundingBox = block.boundingBox;
// final List<Point<int>> cornerPoints = block.cornerPoints;
print(block.text);
final List<RecognizedLanguage> languages = block.recognizedLanguages;

for (TextLine line in block.lines) {
// Same getters as TextBlock
print(element.text);
print(line.text);
for (TextElement element in line.elements) {
// Same getters as TextBlock
print(element.text);
}
}
}
}
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
_scanText();
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
_isScanBusy = false;
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ environment:
dependencies:
flutter:
sdk: flutter
image_picker: ^0.4.12
camera: ^0.2.9
firebase_ml_vision: ^0.2.1

# The following adds the Cupertino Icons font to your application.
Expand Down
2 changes: 1 addition & 1 deletion test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:mlkit_ocr_realtime/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
await tester.pumpWidget(CameraApp());

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down

0 comments on commit 679c744

Please sign in to comment.