-
Notifications
You must be signed in to change notification settings - Fork 23
Language Detector task #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
10990cf
Adds mediapipe_core package (#11)
craiglabenz 7850d32
Add utility to collect headers from google/mediapipe (#10)
craiglabenz 6a7dacb
[FFI] MediaPipe SDKs finder automation (#16)
craiglabenz 8980132
Adds mediapipe_text package (#12)
craiglabenz 4c159a7
Native Assets CI fix (#20)
craiglabenz 4304801
Text Embedding task (#21)
craiglabenz 3fc2d11
updated and re-ran generators
craiglabenz e01f26b
fixed embedding header file and bindings
craiglabenz ce42365
adds text embedding classes to text pkg
craiglabenz d36976b
moved worker dispose method to base class
craiglabenz f0344b9
class hierarchy improvements
craiglabenz 992332b
cleaned up dispose methods
craiglabenz 4ac6d20
initial commit of language detection task
craiglabenz b9b6c89
finishes language detection impl
craiglabenz bed398a
adds language detection demo
craiglabenz 73bada3
backfilling improvements to classification and embedding
craiglabenz a392f83
adds language detection tests
craiglabenz 4b742a3
add new model download to CI script
craiglabenz 65cc969
fixes stale classification widget test, adds language detection widge…
craiglabenz 078fe62
Merge branch 'main' into ffi-wrapper-language-detection
craiglabenz d81b148
copied latest headers from mediapipe
craiglabenz d3ac430
Update packages/mediapipe-task-text/lib/src/io/tasks/language_detecti…
craiglabenz fad589c
comments / documentation improvements
craiglabenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
158 changes: 158 additions & 0 deletions
158
packages/mediapipe-task-text/example/lib/language_detection_demo.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
import 'dart:typed_data'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:getwidget/getwidget.dart'; | ||
import 'package:mediapipe_text/mediapipe_text.dart'; | ||
import 'enumerate.dart'; | ||
|
||
class LanguageDetectionDemo extends StatefulWidget { | ||
const LanguageDetectionDemo({super.key, this.detector}); | ||
|
||
final LanguageDetector? detector; | ||
|
||
@override | ||
State<LanguageDetectionDemo> createState() => _LanguageDetectionDemoState(); | ||
} | ||
|
||
class _LanguageDetectionDemoState extends State<LanguageDetectionDemo> | ||
with AutomaticKeepAliveClientMixin<LanguageDetectionDemo> { | ||
final TextEditingController _controller = TextEditingController(); | ||
final Completer<LanguageDetector> _completer = Completer<LanguageDetector>(); | ||
final results = <Widget>[]; | ||
String? _isProcessing; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_controller.text = 'Quiero agua, por favor'; | ||
_initDetector(); | ||
} | ||
|
||
Future<void> _initDetector() async { | ||
if (widget.detector != null) { | ||
return _completer.complete(widget.detector!); | ||
} | ||
|
||
ByteData? bytes = await DefaultAssetBundle.of(context) | ||
.load('assets/language_detector.tflite'); | ||
|
||
final detector = LanguageDetector( | ||
LanguageDetectorOptions.fromAssetBuffer( | ||
bytes.buffer.asUint8List(), | ||
), | ||
); | ||
_completer.complete(detector); | ||
bytes = null; | ||
} | ||
|
||
void _prepareForDetection() { | ||
setState(() { | ||
_isProcessing = _controller.text; | ||
results.add(const CircularProgressIndicator.adaptive()); | ||
}); | ||
} | ||
|
||
Future<void> _detect() async { | ||
_prepareForDetection(); | ||
_completer.future.then((detector) async { | ||
final result = await detector.detect(_controller.text); | ||
_showDetectionResults(result); | ||
result.dispose(); | ||
}); | ||
} | ||
|
||
void _showDetectionResults(LanguageDetectorResult result) { | ||
setState( | ||
() { | ||
results.last = Card( | ||
key: Key('prediction-"$_isProcessing" ${results.length}'), | ||
margin: const EdgeInsets.all(10), | ||
child: Column( | ||
children: [ | ||
Padding( | ||
padding: const EdgeInsets.all(10), | ||
child: Text(_isProcessing!), | ||
), | ||
Padding( | ||
padding: const EdgeInsets.all(10.0), | ||
child: Wrap( | ||
children: <Widget>[ | ||
...result.predictions | ||
.enumerate<Widget>( | ||
(prediction, index) => _languagePrediction( | ||
prediction, | ||
predictionColors[index], | ||
), | ||
// Take first 4 because the model spits out dozens of | ||
// astronomically low probability language predictions | ||
max: predictionColors.length, | ||
) | ||
.toList(), | ||
], | ||
), | ||
), | ||
], | ||
), | ||
); | ||
_isProcessing = null; | ||
}, | ||
); | ||
} | ||
|
||
static final predictionColors = <Color>[ | ||
Colors.blue[300]!, | ||
Colors.orange[300]!, | ||
Colors.green[300]!, | ||
Colors.red[300]!, | ||
]; | ||
|
||
Widget _languagePrediction(LanguagePrediction prediction, Color color) { | ||
return Padding( | ||
padding: const EdgeInsets.only(right: 8), | ||
child: GFButton( | ||
onPressed: null, | ||
text: '${prediction.languageCode} :: ' | ||
'${prediction.probability.roundTo(8)}', | ||
shape: GFButtonShape.pills, | ||
color: color, | ||
), | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
super.build(context); | ||
return Scaffold( | ||
body: SafeArea( | ||
child: Padding( | ||
padding: const EdgeInsets.all(16.0), | ||
child: SingleChildScrollView( | ||
child: Column( | ||
children: <Widget>[ | ||
TextField(controller: _controller), | ||
...results.reversed, | ||
], | ||
), | ||
), | ||
), | ||
), | ||
floatingActionButton: FloatingActionButton( | ||
onPressed: | ||
_isProcessing != null && _controller.text != '' ? null : _detect, | ||
child: const Icon(Icons.search), | ||
), | ||
); | ||
} | ||
|
||
@override | ||
bool get wantKeepAlive => true; | ||
} | ||
|
||
extension on double { | ||
double roundTo(int decimalPlaces) => | ||
double.parse(toStringAsFixed(decimalPlaces)); | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.