Upload New File

parent 1d463436
import 'dart:io';
import 'package:detector/common/utils/toast_helper.dart';
import 'package:detector/data/models/audio_response_model.dart';
import 'package:detector/data/models/client_model.dart';
import 'package:detector/data/models/emotion_value_bind_model.dart';
import 'package:detector/data/models/image_response_model.dart';
import 'package:detector/data/models/question_model.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:record_mp3/record_mp3.dart';
import 'package:http/http.dart' as http;
class DataProvider extends ChangeNotifier {
late ClientModel _client;
late String _recordFilePath;
late AudioResponse _audioResponse;
late ImageResponseModel _imageResponse;
late EmotionValueBind _imageResult;
int i = 0;
List<QuestionModel> _questions = [];
bool _loadingSubmit = false;
bool _paused = false;
void setClient(ClientModel client) {
_client = client;
}
void setQuestions(List<QuestionModel> questions) {
_questions = questions;
}
void setLoadingStatus(bool status) {
_loadingSubmit = status;
notifyListeners();
}
void updateAudioResponse(AudioResponse data) {
_audioResponse = data;
}
void updateImageResponse(ImageResponseModel data) {
_imageResponse = data;
EmotionValueBind _result;
List<EmotionValueBind> emotions = [];
int sadValue = 0;
int normalValue = 0;
int happyValue = 0;
int angryValue = 0;
int surpriseValue = 0;
int fearValue = 0;
double sadPredict = 0.0;
double normalPredict = 0.0;
double happyPredict = 0.0;
double angryPredict = 0.0;
double surprisePredict = 0.0;
double fearPredict = 0.0;
for (var item in _imageResponse.response) {
if (item.emotion == "Sad") {
sadValue = sadValue + 1;
sadPredict = sadPredict + item.probability;
} else if (item.emotion == "Neutral") {
normalValue = normalValue + 1;
normalPredict = normalPredict + item.probability;
} else if (item.emotion == "Surprise") {
surpriseValue = surpriseValue + 1;
surprisePredict = surprisePredict + item.probability;
} else if (item.emotion == "Happy") {
happyValue = happyValue + 1;
happyPredict = happyPredict + item.probability;
} else if (item.emotion == "Angry") {
angryValue = angryValue + 1;
angryPredict = angryPredict + item.probability;
} else if (item.emotion == "Fear") {
fearValue = fearValue + 1;
fearPredict = fearPredict + item.probability;
}
}
if (sadValue > 0) {
var _data = EmotionValueBind(
emotion: "Sad", count: sadValue, prob: sadPredict / sadValue);
emotions.add(_data);
}
if (normalValue > 0) {
var _data = EmotionValueBind(
emotion: "Neutral",
count: normalValue,
prob: normalPredict / normalValue);
emotions.add(_data);
}
if (surpriseValue > 0) {
var _data = EmotionValueBind(
emotion: "Surprise",
count: surpriseValue,
prob: surprisePredict / surpriseValue);
emotions.add(_data);
}
if (happyValue > 0) {
var _data = EmotionValueBind(
emotion: "Happy", count: happyValue, prob: happyPredict / happyValue);
emotions.add(_data);
}
if (angryValue > 0) {
var _data = EmotionValueBind(
emotion: "Angry", count: angryValue, prob: angryPredict / angryValue);
emotions.add(_data);
}
if (fearValue > 0) {
var _data = EmotionValueBind(
emotion: "Fear", count: fearValue, prob: fearPredict / fearValue);
emotions.add(_data);
}
emotions.sort((e1, e2) => e1.count - e2.count);
_result = emotions.last;
_imageResult = _result;
}
Future<bool> checkPermission() async {
if (!await Permission.microphone.isGranted) {
PermissionStatus status = await Permission.microphone.request();
if (status != PermissionStatus.granted) {
return false;
}
}
return true;
}
Future<String> getFilePath() async {
Directory storageDirectory = await getApplicationDocumentsDirectory();
String sdPath = storageDirectory.path + "/Depression Detector";
var d = Directory(sdPath);
if (!d.existsSync()) {
d.createSync(recursive: true);
}
String path = sdPath + "/sample_${i++}.mp3";
print(path);
return path;
}
void startRecord() async {
_paused = false;
bool hasPermission = await checkPermission();
if (hasPermission) {
_recordFilePath = await getFilePath();
RecordMp3.instance.start(_recordFilePath, (type) {
ToastHelper.errorToast("Audio Record Failed. Code $type");
});
} else {
ToastHelper.errorToast(
"App needs to access your microphone to record audio, please provide permission");
}
}
void pauseRecord() {
if (RecordMp3.instance.status == RecordStatus.PAUSE) {
RecordMp3.instance.resume();
_paused = false;
ToastHelper.successToast("Voice recording resumed");
} else {
RecordMp3.instance.pause();
_paused = true;
ToastHelper.successToast("Voice recording paused");
}
notifyListeners();
}
void stopRecord() {
_paused = true;
RecordMp3.instance.stop();
}
Future<String> uploadAudio() async {
try {
String url = "http://nipunanisaga.xyz/api/v1/audio_analysis";
var request = http.MultipartRequest('POST', Uri.parse(url));
request.files.add(http.MultipartFile.fromBytes(
'audio', File(_recordFilePath).readAsBytesSync(),
filename: _recordFilePath.split("/").last));
var res = await request.send();
final respStr = await res.stream.bytesToString();
return respStr;
} catch (e) {
return "failed";
}
}
Future<String> uploadImages(List<String> paths) async {
List<String> readyPaths;
try {
if (paths.length > 5) {
readyPaths = paths.take(5).toList();
} else {
readyPaths = paths;
}
String url = "http://nipunanisaga.xyz/api/v1/emotion_analysis";
var request = http.MultipartRequest('POST', Uri.parse(url));
for (var item in readyPaths) {
request.files.add(http.MultipartFile.fromBytes(
'image', File(item).readAsBytesSync(),
filename: item.split("/").last));
}
var res = await request.send();
final respStr = await res.stream.bytesToString();
print(respStr);
return respStr;
} catch (e) {
return "failed";
}
}
ClientModel get currentClient => _client;
List<QuestionModel> get questions => _questions;
bool get submitButtonLoading => _loadingSubmit;
bool get getPauseStatus => _paused;
AudioResponse get audioResponseData => _audioResponse;
EmotionValueBind get imageResult => _imageResult;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment