-
Notifications
You must be signed in to change notification settings - Fork 225
/
face_detect.dart
227 lines (200 loc) · 7.03 KB
/
face_detect.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:mlkit/mlkit.dart';
import 'dart:async';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
File _file;
List<VisionFace> _face = <VisionFace>[];
VisionFaceDetectorOptions options = new VisionFaceDetectorOptions(
modeType: VisionFaceDetectorMode.Accurate,
landmarkType: VisionFaceDetectorLandmark.All,
classificationType: VisionFaceDetectorClassification.All,
minFaceSize: 0.15,
isTrackingEnabled: true);
FirebaseVisionFaceDetector detector = FirebaseVisionFaceDetector.instance;
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Face Detection Firebase'),
),
body: showBody(_file),
floatingActionButton: new FloatingActionButton(
onPressed: () async {
var file = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_file = file;
});
var face =
await detector.detectFromBinary(_file?.readAsBytesSync(), options);
setState(() {
if (face.isEmpty) {
print('No face detected');
} else {
_face = face;
}
});
},
child: new Icon(Icons.tag_faces),
),
),
);
}
Widget showBody(File file) {
return new Container(
child: new Stack(
children: <Widget>[
_buildImage(),
_showDetails(_face),
],
),
);
}
Widget _buildImage() {
return new SizedBox(
height: 500.0,
child: new Center(
child: _file == null
? new Text('Select image using Floating Button...')
: new FutureBuilder<Size>(
future: _getImageSize(Image.file(_file, fit: BoxFit.fitWidth)),
builder: (BuildContext context, AsyncSnapshot<Size> snapshot) {
if (snapshot.hasData) {
return Container(
foregroundDecoration:
TextDetectDecoration(_face, snapshot.data),
child: Image.file(_file, fit: BoxFit.fitWidth));
} else {
return new Text('Please wait...');
}
},
),
),
);
}
}
class TextDetectDecoration extends Decoration {
final Size _originalImageSize;
final List<VisionFace> _texts;
TextDetectDecoration(List<VisionFace> texts, Size originalImageSize)
: _texts = texts,
_originalImageSize = originalImageSize;
@override
BoxPainter createBoxPainter([VoidCallback onChanged]) {
return new _TextDetectPainter(_texts, _originalImageSize);
}
}
Future _getImageSize(Image image) {
Completer<Size> completer = new Completer<Size>();
image.image.resolve(new ImageConfiguration()).addListener(
(ImageInfo info, bool _) => completer.complete(
Size(info.image.width.toDouble(), info.image.height.toDouble())));
return completer.future;
}
Widget _showDetails(List<VisionFace> faceList) {
if (faceList == null || faceList.length == 0) {
return new Text('', textAlign: TextAlign.center);
}
return new Container(
child: new ListView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: faceList.length,
itemBuilder: (context, i) {
checkData(faceList);
return _buildRow(
faceList[0].hasLeftEyeOpenProbability,
faceList[0].headEulerAngleY,
faceList[0].headEulerAngleZ,
faceList[0].leftEyeOpenProbability,
faceList[0].rightEyeOpenProbability,
faceList[0].smilingProbability,
faceList[0].trackingID);
},
),
);
}
//For checking and printing different variables from Firebase
void checkData(List<VisionFace> faceList) {
final double uncomputedProb = -1.0;
final int uncompProb = -1;
for (int i = 0; i < faceList.length; i++) {
Rect bounds = faceList[i].rect;
print('Rectangle : $bounds');
VisionFaceLandmark landmark =
faceList[i].getLandmark(FaceLandmarkType.LeftEar);
if (landmark != null) {
VisionPoint leftEarPos = landmark.position;
print('Left Ear Pos : $leftEarPos');
}
if (faceList[i].smilingProbability != uncomputedProb) {
double smileProb = faceList[i].smilingProbability;
print('Smile Prob : $smileProb');
}
if (faceList[i].rightEyeOpenProbability != uncomputedProb) {
double rightEyeOpenProb = faceList[i].rightEyeOpenProbability;
print('RightEye Open Prob : $rightEyeOpenProb');
}
if (faceList[i].trackingID != uncompProb) {
int tID = faceList[i].trackingID;
print('Tracking ID : $tID');
}
}
}
/*
HeadEulerY : Head is rotated to right by headEulerY degrees
HeadEulerZ : Head is tilted sideways by headEulerZ degrees
LeftEyeOpenProbability : left Eye Open Probability
RightEyeOpenProbability : right Eye Open Probability
SmilingProbability : Smiling probability
Tracking ID : If face tracking is enabled
*/
Widget _buildRow(
bool leftEyeProb,
double headEulerY,
double headEulerZ,
double leftEyeOpenProbability,
double rightEyeOpenProbability,
double smileProb,
int tID) {
return ListTile(
title: new Text(
"\nLeftEyeProb: $leftEyeProb \nHeadEulerY : $headEulerY \nHeadEulerZ : $headEulerZ \nLeftEyeOpenProbability : $leftEyeOpenProbability \nRightEyeOpenProbability : $rightEyeOpenProbability \nSmileProb : $smileProb \nFaceTrackingEnabled : $tID",
),
dense: true,
);
}
class _TextDetectPainter extends BoxPainter {
final List<VisionFace> _faceLabels;
final Size _originalImageSize;
_TextDetectPainter(faceLabels, originalImageSize)
: _faceLabels = faceLabels,
_originalImageSize = originalImageSize;
@override
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
final paint = new Paint()
..strokeWidth = 2.0
..color = Colors.red
..style = PaintingStyle.stroke;
final _heightRatio = _originalImageSize.height / configuration.size.height;
final _widthRatio = _originalImageSize.width / configuration.size.width;
for (var faceLabel in _faceLabels) {
final _rect = Rect.fromLTRB(
offset.dx + faceLabel.rect.left / _widthRatio,
offset.dy + faceLabel.rect.top / _heightRatio,
offset.dx + faceLabel.rect.right / _widthRatio,
offset.dy + faceLabel.rect.bottom / _heightRatio);
canvas.drawRect(_rect, paint);
}
}
}