Resolve "Add tflite support" #13
2 changed files with 126 additions and 52 deletions
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 'dart:async';
|
||||||
import 'package:easy_learn/view/widgets/score_board.dart';
|
import 'dart:developer';
|
||||||
|
|
||||||
|
import 'package:tflite_audio/tflite_audio.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'package:easy_learn/view/widgets/custom_button.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 {
|
class Content extends StatefulWidget {
|
||||||
const Content({Key? key, required this.category}) : super(key: key);
|
const Content({Key? key, required this.category}) : super(key: key);
|
||||||
|
@ -14,64 +20,126 @@ class Content extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ContentState extends State<Content> {
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: Container(
|
body: StreamBuilder<Map<dynamic, dynamic>>(
|
||||||
decoration: const BoxDecoration(
|
stream: result,
|
||||||
image: DecorationImage(
|
builder: (BuildContext context, AsyncSnapshot<Map<dynamic, dynamic>> snapshot) {
|
||||||
image: AssetImage('assets/images/content_bg.png'),
|
dynamic scoreBoard;
|
||||||
fit: BoxFit.cover
|
|
||||||
)
|
switch (snapshot.connectionState) {
|
||||||
),
|
case ConnectionState.none:
|
||||||
child: Column(
|
scoreBoard = const ScoreBoard(score: "-",);
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
break;
|
||||||
children: [
|
case ConnectionState.waiting:
|
||||||
Row(
|
scoreBoard = const ScoreBoard(score: "-",);
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
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: [
|
children: [
|
||||||
Container(
|
Row(
|
||||||
margin: const EdgeInsets.only(left: 10.0, bottom: 60.0),
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
child: CustomButton(name: 'close', size: 60.0, navigator: () {
|
children: [
|
||||||
Navigator.pop(context);
|
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(),
|
Row(
|
||||||
Container(
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||||
margin: const EdgeInsets.only(right: 10.0, bottom: 60.0),
|
children: [
|
||||||
child: CustomButton(name: 'sound', size: 60.0, navigator: () {},)
|
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: () {},)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue