-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmainv1.dart
187 lines (171 loc) · 5.31 KB
/
mainv1.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:tflite/tflite.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:floating_action_bubble/floating_action_bubble.dart';
void main() => runApp(MaterialApp(
home: MyApp(),
debugShowCheckedModeBanner: false,
theme: ThemeData.light(),
));
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
Animation<double> _animation;
AnimationController _animationController;
List _outputs;
File _image;
bool _loading = false;
@override
void initState() {
_animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 260),
);
final curvedAnimation =
CurvedAnimation(curve: Curves.easeInOut, parent: _animationController);
_animation = Tween<double>(begin: 0, end: 1).animate(curvedAnimation);
super.initState();
super.initState();
_loading = true;
loadModel().then((value) {
setState(() {
_loading = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Nude_detection_by_Sinai'),
backgroundColor: Colors.deepPurple,
elevation: 0.0,
leading: Icon(Icons.menu),
),
body: _loading
? Container(
alignment: Alignment.center,
child: CircularProgressIndicator(),
)
: Container(
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
_image == null ? Container() : Image.file(_image),
SizedBox(
height: 20,
),
_outputs != null
? Text(
"${_outputs[0]["label"]}",
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
background: Paint()..color = Colors.deepPurple,
fontFamily: 'Roboto',
),
)
: Container()
],
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
floatingActionButton: FloatingActionBubble(
// Menu items
items: <Bubble>[
Bubble(
title: "Gallery",
iconColor: Colors.white,
bubbleColor: Colors.green,
icon: Icons.photo_album,
titleStyle: TextStyle(fontSize: 16, color: Colors.white),
onPress: () {
_animationController.reverse();
pickImage();
},
),
Bubble(
title: "Camera",
iconColor: Colors.white,
bubbleColor: Colors.green,
icon: Icons.camera_alt,
titleStyle: TextStyle(fontSize: 16, color: Colors.white),
onPress: () {
_animationController.reverse();
_imgFromCamera();
},
),
],
animation: _animation,
// On pressed change animation state
onPress: () => _animationController.isCompleted
? _animationController.reverse()
: _animationController.forward(),
iconColor: Colors.green,
icon: AnimatedIcons.menu_close));
}
pickImage() async {
// ignore: deprecated_member_use
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
if (image == null) return null;
setState(() {
_loading = true;
_image = image;
});
classifyImage(image);
}
_imgFromCamera() async {
// ignore: deprecated_member_use
var image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
_loading = true;
_image = image;
});
classifyImage(image);
}
classifyImage(File image) async {
var output = await Tflite.runModelOnImage(
path: image.path,
numResults: 2,
threshold: 0.5,
imageMean: 127.5,
imageStd: 127.5,
);
print(output);
setState(() {
_loading = false;
_outputs = output;
if (_outputs[0]['index'] == 0) {
Fluttertoast.showToast(
msg: "nudes cannot be uploaded",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.green,
textColor: Colors.white,
fontSize: 14.0);
_loading = true;
} else {
_loading = false;
Container();
}
});
}
loadModel() async {
await Tflite.loadModel(
model: "assets/classifier.tflite",
labels: "assets/labels.txt",
);
}
@override
void dispose() {
Tflite.close();
super.dispose();
}
}