-
Notifications
You must be signed in to change notification settings - Fork 22
/
VisionCameraFaceDetectorPlugin.kt
324 lines (287 loc) · 9.73 KB
/
VisionCameraFaceDetectorPlugin.kt
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package com.visioncamerafacedetector
import android.content.res.Resources
import android.graphics.Rect
import android.util.Log
import com.google.android.gms.tasks.Tasks
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.face.Face
import com.google.mlkit.vision.face.FaceContour
import com.google.mlkit.vision.face.FaceDetection
import com.google.mlkit.vision.face.FaceDetector
import com.google.mlkit.vision.face.FaceDetectorOptions
import com.google.mlkit.vision.face.FaceLandmark
import com.mrousavy.camera.core.FrameInvalidError
import com.mrousavy.camera.frameprocessor.Frame
import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin
import com.mrousavy.camera.frameprocessor.VisionCameraProxy
private const val TAG = "FaceDetector"
class VisionCameraFaceDetectorPlugin(
proxy: VisionCameraProxy,
options: Map<String, Any>?
) : FrameProcessorPlugin() {
private var faceDetector: FaceDetector? = null
// device display data
private val density = Resources.getSystem().displayMetrics.density.toInt()
private val windowWidth = Resources.getSystem().displayMetrics.widthPixels / density
private val windowHeight = Resources.getSystem().displayMetrics.heightPixels / density
private fun initFD(params: Map<String, Any>?) {
var performanceModeValue = FaceDetectorOptions.PERFORMANCE_MODE_FAST
var landmarkModeValue = FaceDetectorOptions.LANDMARK_MODE_NONE
var classificationModeValue = FaceDetectorOptions.CLASSIFICATION_MODE_NONE
var contourModeValue = FaceDetectorOptions.CONTOUR_MODE_NONE
var minFaceSize = 0.15f
var enableTracking = false
if (params?.get("performanceMode").toString() == "accurate") {
performanceModeValue = FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE
}
if (params?.get("landmarkMode").toString() == "all") {
landmarkModeValue = FaceDetectorOptions.LANDMARK_MODE_ALL
}
if (params?.get("classificationMode").toString() == "all") {
classificationModeValue = FaceDetectorOptions.CLASSIFICATION_MODE_ALL
}
if (params?.get("contourMode").toString() == "all") {
contourModeValue = FaceDetectorOptions.CONTOUR_MODE_ALL
}
val minFaceSizeParam = params?.get("minFaceSize").toString()
if (
minFaceSizeParam != "null" &&
minFaceSizeParam != minFaceSize.toString()
) {
minFaceSize = minFaceSizeParam.toFloat()
}
if (params?.get("trackingEnabled").toString() == "true") {
enableTracking = true
}
val optionsBuilder = FaceDetectorOptions.Builder()
.setPerformanceMode(performanceModeValue)
.setLandmarkMode(landmarkModeValue)
.setContourMode(contourModeValue)
.setClassificationMode(classificationModeValue)
.setMinFaceSize(minFaceSize)
if (enableTracking) {
optionsBuilder.enableTracking()
}
val options = optionsBuilder.build()
faceDetector = FaceDetection.getClient(options)
}
private fun processBoundingBox(
boundingBox: Rect,
scaleX: Double,
scaleY: Double
): Map<String, Any> {
val bounds: MutableMap<String, Any> = HashMap()
val width = boundingBox.width().toDouble() * scaleX
bounds["width"] = width
bounds["height"] = boundingBox.height().toDouble() * scaleY
bounds["x"] = windowWidth - (width + (
boundingBox.left.toDouble() * scaleX
))
bounds["y"] = boundingBox.top.toDouble() * scaleY
return bounds
}
private fun processLandmarks(
face: Face,
scaleX: Double,
scaleY: Double
): Map<String, Any> {
val faceLandmarksTypes = intArrayOf(
FaceLandmark.LEFT_CHEEK,
FaceLandmark.LEFT_EAR,
FaceLandmark.LEFT_EYE,
FaceLandmark.MOUTH_BOTTOM,
FaceLandmark.MOUTH_LEFT,
FaceLandmark.MOUTH_RIGHT,
FaceLandmark.NOSE_BASE,
FaceLandmark.RIGHT_CHEEK,
FaceLandmark.RIGHT_EAR,
FaceLandmark.RIGHT_EYE
)
val faceLandmarksTypesStrings = arrayOf(
"LEFT_CHEEK",
"LEFT_EAR",
"LEFT_EYE",
"MOUTH_BOTTOM",
"MOUTH_LEFT",
"MOUTH_RIGHT",
"NOSE_BASE",
"RIGHT_CHEEK",
"RIGHT_EAR",
"RIGHT_EYE"
)
val faceLandmarksTypesMap: MutableMap<String, Any> = HashMap()
for (i in faceLandmarksTypesStrings.indices) {
val landmark = face.getLandmark(faceLandmarksTypes[i])
val landmarkName = faceLandmarksTypesStrings[i]
Log.d(
TAG,
"Getting '$landmarkName' landmark"
)
if (landmark == null) {
Log.d(
TAG,
"Landmark '$landmarkName' is null - going next"
)
continue
}
val point = landmark.position
val currentPointsMap: MutableMap<String, Double> = HashMap()
currentPointsMap["x"] = point.x.toDouble() * scaleX
currentPointsMap["y"] = point.y.toDouble() * scaleY
faceLandmarksTypesMap[landmarkName] = currentPointsMap
}
return faceLandmarksTypesMap
}
private fun processFaceContours(
face: Face,
scaleX: Double,
scaleY: Double
): Map<String, Any> {
val faceContoursTypes = intArrayOf(
FaceContour.FACE,
FaceContour.LEFT_CHEEK,
FaceContour.LEFT_EYE,
FaceContour.LEFT_EYEBROW_BOTTOM,
FaceContour.LEFT_EYEBROW_TOP,
FaceContour.LOWER_LIP_BOTTOM,
FaceContour.LOWER_LIP_TOP,
FaceContour.NOSE_BOTTOM,
FaceContour.NOSE_BRIDGE,
FaceContour.RIGHT_CHEEK,
FaceContour.RIGHT_EYE,
FaceContour.RIGHT_EYEBROW_BOTTOM,
FaceContour.RIGHT_EYEBROW_TOP,
FaceContour.UPPER_LIP_BOTTOM,
FaceContour.UPPER_LIP_TOP
)
val faceContoursTypesStrings = arrayOf(
"FACE",
"LEFT_CHEEK",
"LEFT_EYE",
"LEFT_EYEBROW_BOTTOM",
"LEFT_EYEBROW_TOP",
"LOWER_LIP_BOTTOM",
"LOWER_LIP_TOP",
"NOSE_BOTTOM",
"NOSE_BRIDGE",
"RIGHT_CHEEK",
"RIGHT_EYE",
"RIGHT_EYEBROW_BOTTOM",
"RIGHT_EYEBROW_TOP",
"UPPER_LIP_BOTTOM",
"UPPER_LIP_TOP"
)
val faceContoursTypesMap: MutableMap<String, Any> = HashMap()
for (i in faceContoursTypesStrings.indices) {
val contour = face.getContour(faceContoursTypes[i])
val contourName = faceContoursTypesStrings[i]
Log.d(
TAG,
"Getting '$contourName' contour"
)
if (contour == null) {
Log.d(
TAG,
"Face contour '$contourName' is null - going next"
)
continue
}
val points = contour.points
val pointsMap: MutableMap<String, Map<String, Double>> = HashMap()
for (j in points.indices) {
val currentPointsMap: MutableMap<String, Double> = HashMap()
currentPointsMap["x"] = points[j].x.toDouble() * scaleX
currentPointsMap["y"] = points[j].y.toDouble() * scaleY
pointsMap[j.toString()] = currentPointsMap
}
faceContoursTypesMap[contourName] = pointsMap
}
return faceContoursTypesMap
}
override fun callback(
frame: Frame,
params: Map<String, Any>?
): Map<String, Any> {
val resultMap: MutableMap<String, Any> = HashMap()
try {
val frameImage = frame.image
val orientation = frame.orientation
if (
frameImage == null &&
orientation == null
) {
Log.i(TAG, "Image or orientation is null")
return resultMap
}
if (faceDetector == null) {
initFD(params)
}
val rotation = orientation!!.toDegrees()
val image = InputImage.fromMediaImage(frameImage!!, rotation)
val scaleX: Double
val scaleY: Double
if (rotation == 270 || rotation == 90) {
scaleX = windowWidth.toDouble() / image.height
scaleY = windowHeight.toDouble() / image.width
} else {
scaleX = windowWidth.toDouble() / image.width
scaleY = windowHeight.toDouble() / image.height
}
val task = faceDetector!!.process(image)
val faces = Tasks.await(task)
val facesList = ArrayList<Map<String, Any?>>()
faces.forEach{face ->
val map: MutableMap<String, Any?> = HashMap()
if (params?.get("landmarkMode").toString() == "all") {
map["landmarks"] = processLandmarks(
face,
scaleX,
scaleY
)
}
if (params?.get("classificationMode").toString() == "all") {
map["leftEyeOpenProbability"] = face.leftEyeOpenProbability?.toDouble() ?: -1
map["rightEyeOpenProbability"] = face.rightEyeOpenProbability?.toDouble() ?: -1
map["smilingProbability"] = face.smilingProbability?.toDouble() ?: -1
}
if (params?.get("contourMode").toString() == "all") {
map["contours"] = processFaceContours(
face,
scaleX,
scaleY
)
}
if (params?.get("trackingEnabled").toString() == "true") {
map["trackingId"] = face.trackingId
}
map["rollAngle"] = face.headEulerAngleZ.toDouble()
map["pitchAngle"] = face.headEulerAngleX.toDouble()
map["yawAngle"] = face.headEulerAngleY.toDouble()
map["bounds"] = processBoundingBox(
face.boundingBox,
scaleX,
scaleY
)
facesList.add(map)
}
val frameMap: MutableMap<String, Any> = HashMap()
val returnOriginal = params?.get("returnOriginal").toString() == "true"
val convertFrame = params?.get("convertFrame").toString() == "true"
if (returnOriginal) {
frameMap["original"] = frame
}
if (convertFrame) {
frameMap["converted"] = BitmapUtils.convertYuvToRgba(frameImage)
}
resultMap["faces"] = facesList
if(returnOriginal || convertFrame) {
resultMap["frame"] = frameMap
}
} catch (e: Exception) {
Log.e(TAG, "Error processing face detection: ", e)
} catch (e: FrameInvalidError) {
Log.e(TAG, "Frame invalid error: ", e)
}
return resultMap
}
}