Merge branch '8-add-tflite-support' into 'main'
Resolve "Add tflite support" Closes #8 See merge request easy-learn-aic/app!5
This commit is contained in:
commit
1fefd8399e
9 changed files with 164 additions and 59 deletions
|
@ -44,7 +44,7 @@ android {
|
|||
defaultConfig {
|
||||
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
|
||||
applicationId "com.example.easy_learn"
|
||||
minSdkVersion 16
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 30
|
||||
versionCode flutterVersionCode.toInteger()
|
||||
versionName flutterVersionName
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.easy_learn">
|
||||
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<application
|
||||
android:label="easy_learn"
|
||||
android:icon="@mipmap/ic_launcher">
|
||||
|
|
12
assets/datasets/label.txt
Normal file
12
assets/datasets/label.txt
Normal file
|
@ -0,0 +1,12 @@
|
|||
_silence_
|
||||
_unknown_
|
||||
yes
|
||||
no
|
||||
up
|
||||
down
|
||||
left
|
||||
right
|
||||
on
|
||||
off
|
||||
stop
|
||||
go
|
BIN
assets/datasets/model.tflite
Normal file
BIN
assets/datasets/model.tflite
Normal file
Binary file not shown.
6
lib/config.dart
Normal file
6
lib/config.dart
Normal file
|
@ -0,0 +1,6 @@
|
|||
const String model = 'assets/datasets/model.tflite';
|
||||
const String label = 'assets/datasets/label.txt';
|
||||
const String inputType = 'decodedWav';
|
||||
const int sampleRate = 16000;
|
||||
const int recordingLength = 16000;
|
||||
const int bufferSize = 2000;
|
|
@ -1,8 +1,14 @@
|
|||
import 'package:easy_learn/view/widgets/list_contents_box.dart';
|
||||
import 'package:easy_learn/view/widgets/score_board.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:tflite_audio/tflite_audio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:easy_learn/view/widgets/custom_button.dart';
|
||||
import 'package:easy_learn/view/widgets/score_board.dart';
|
||||
import 'package:easy_learn/view/widgets/list_contents_box.dart';
|
||||
import 'package:easy_learn/config.dart' show model, label, inputType, sampleRate, recordingLength, bufferSize;
|
||||
|
||||
|
||||
class Content extends StatefulWidget {
|
||||
const Content({Key? key, required this.category}) : super(key: key);
|
||||
|
@ -14,64 +20,126 @@ class Content extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _ContentState extends State<Content> {
|
||||
final isRecording = ValueNotifier<bool>(false);
|
||||
Stream<Map<dynamic, dynamic>>? result;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
TfliteAudio.loadModel(
|
||||
// numThreads: this.numThreads,
|
||||
// isAsset: this.isAsset,
|
||||
model: model,
|
||||
label: label,
|
||||
);
|
||||
}
|
||||
|
||||
void getResult() {
|
||||
result = TfliteAudio.startAudioRecognition(
|
||||
inputType: inputType,
|
||||
sampleRate: sampleRate,
|
||||
recordingLength: recordingLength,
|
||||
bufferSize: bufferSize,
|
||||
);
|
||||
|
||||
result ?.listen(
|
||||
(event) => log(event.toString())
|
||||
).onDone(() => isRecording.value = false);
|
||||
}
|
||||
|
||||
String showResult(AsyncSnapshot snapshot, String key) => snapshot.hasData ? snapshot.data[key].toString() : 'null ';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Container(
|
||||
decoration: const BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage('assets/images/content_bg.png'),
|
||||
fit: BoxFit.cover
|
||||
)
|
||||
),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
body: StreamBuilder<Map<dynamic, dynamic>>(
|
||||
stream: result,
|
||||
builder: (BuildContext context, AsyncSnapshot<Map<dynamic, dynamic>> snapshot) {
|
||||
dynamic scoreBoard;
|
||||
|
||||
switch (snapshot.connectionState) {
|
||||
case ConnectionState.none:
|
||||
scoreBoard = const ScoreBoard(score: "-",);
|
||||
break;
|
||||
case ConnectionState.waiting:
|
||||
scoreBoard = const ScoreBoard(score: "-",);
|
||||
break;
|
||||
default:
|
||||
scoreBoard = ScoreBoard(score: showResult(snapshot, 'recognitionResult'),);
|
||||
}
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage('assets/images/content_bg.png'),
|
||||
fit: BoxFit.cover
|
||||
)
|
||||
),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
Container(
|
||||
margin: const EdgeInsets.only(left: 10.0, bottom: 60.0),
|
||||
child: CustomButton(name: 'close', size: 60.0, navigator: () {
|
||||
Navigator.pop(context);
|
||||
},)
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Container(
|
||||
margin: const EdgeInsets.only(left: 10.0, bottom: 60.0),
|
||||
child: CustomButton(name: 'close', size: 60.0, navigator: () {
|
||||
Navigator.pop(context);
|
||||
},)
|
||||
),
|
||||
scoreBoard,
|
||||
Container(
|
||||
margin: const EdgeInsets.only(right: 10.0, bottom: 60.0),
|
||||
child: CustomButton(name: 'sound', size: 60.0, navigator: () {},)
|
||||
),
|
||||
],
|
||||
),
|
||||
const ScoreBoard(),
|
||||
Container(
|
||||
margin: const EdgeInsets.only(right: 10.0, bottom: 60.0),
|
||||
child: CustomButton(name: 'sound', size: 60.0, navigator: () {},)
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
Container(
|
||||
margin: const EdgeInsets.only(left: 5.0),
|
||||
child: CustomButton(name: 'previous', size: 100.0, navigator: () {},)
|
||||
),
|
||||
const SizedBox(
|
||||
width: 180,
|
||||
height: 180,
|
||||
child: ListContentsBox(
|
||||
imagePath: 'assets/images/list_contents_animals.png',
|
||||
title: 'Animals'
|
||||
),
|
||||
),
|
||||
Container(
|
||||
margin: const EdgeInsets.only(right: 5.0),
|
||||
child: CustomButton(name: 'next', size: 100.0, navigator: () {})
|
||||
)
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ValueListenableBuilder(
|
||||
valueListenable: isRecording,
|
||||
builder: (context, value, widget) {
|
||||
if (value == false) {
|
||||
return CustomButton(name: 'microphone', size: 190.0, navigator: () {
|
||||
isRecording.value = true;
|
||||
setState(() {
|
||||
getResult();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return CustomButton(name: 'microphone', size: 190.0, navigator: () {
|
||||
TfliteAudio.stopAudioRecognition();
|
||||
});
|
||||
}
|
||||
}
|
||||
)
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
Container(
|
||||
margin: const EdgeInsets.only(left: 5.0),
|
||||
child: CustomButton(name: 'previous', size: 100.0, navigator: () {},)
|
||||
),
|
||||
const SizedBox(
|
||||
width: 180,
|
||||
height: 180,
|
||||
child: ListContentsBox(
|
||||
imagePath: 'assets/images/list_contents_animals.png',
|
||||
title: 'Animals'
|
||||
),
|
||||
),
|
||||
Container(
|
||||
margin: const EdgeInsets.only(right: 5.0),
|
||||
child: CustomButton(name: 'next', size: 100.0, navigator: () {})
|
||||
)
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
CustomButton(name: 'microphone', size: 190.0, navigator: () {},)
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ScoreBoard extends StatelessWidget {
|
||||
const ScoreBoard({Key? key}) : super(key: key);
|
||||
const ScoreBoard({Key? key, required this.score}) : super(key: key);
|
||||
|
||||
final String score;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
@ -13,6 +16,13 @@ class ScoreBoard extends StatelessWidget {
|
|||
image: AssetImage('assets/images/content_score_board.png'),
|
||||
)
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Text(score, style: const TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold))
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -156,6 +156,13 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.4.2"
|
||||
tflite_audio:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: tflite_audio
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.1.8+1"
|
||||
typed_data:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -172,3 +179,4 @@ packages:
|
|||
version: "2.1.0"
|
||||
sdks:
|
||||
dart: ">=2.12.0 <3.0.0"
|
||||
flutter: ">=1.10.0"
|
||||
|
|
|
@ -31,13 +31,9 @@ dependencies:
|
|||
flutter:
|
||||
sdk: flutter
|
||||
page_transition: ^2.0.2
|
||||
tflite_audio: ^0.1.8+1
|
||||
|
||||
dev_dependencies:
|
||||
# The "flutter_lints" package below contains a set of recommended lints to
|
||||
# encourage good coding practices. The lint set provided by the package is
|
||||
# activated in the `analysis_options.yaml` file located at the root of your
|
||||
# package. See that file for information about deactivating specific lint
|
||||
# rules and activating additional ones.
|
||||
flutter_lints: ^1.0.0
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
@ -55,6 +51,7 @@ flutter:
|
|||
assets:
|
||||
- assets/images/
|
||||
- assets/images/buttons/
|
||||
- assets/datasets/
|
||||
# An image asset can refer to one or more resolution-specific "variants", see
|
||||
# https://flutter.dev/assets-and-images/#resolution-aware.
|
||||
# For details regarding adding assets from package dependencies, see
|
||||
|
|
Loading…
Add table
Reference in a new issue