-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathVisionCameraFaceDetectorPlugin.kt
317 lines (285 loc) · 9.5 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
package com.visioncamerafacedetector
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.core.types.Orientation
import com.mrousavy.camera.frameprocessors.Frame
import com.mrousavy.camera.frameprocessors.FrameProcessorPlugin
import com.mrousavy.camera.frameprocessors.VisionCameraProxy
private const val TAG = "FaceDetector"
class VisionCameraFaceDetectorPlugin(
proxy: VisionCameraProxy,
options: Map<String, Any>?
) : FrameProcessorPlugin() {
// device display data
private val displayMetrics = proxy.context.resources.displayMetrics
private val density = displayMetrics.density
private val windowWidth = (displayMetrics.widthPixels).toDouble() / density
private val windowHeight = (displayMetrics.heightPixels).toDouble() / density
// detection props
private var autoScale = false
private var faceDetector: FaceDetector? = null
private var runLandmarks = false
private var runClassifications = false
private var runContours = false
private var trackingEnabled = false
init {
// handle auto scaling
autoScale = options?.get("autoScale").toString() == "true"
// initializes faceDetector on creation
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
if (options?.get("performanceMode").toString() == "accurate") {
performanceModeValue = FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE
}
if (options?.get("landmarkMode").toString() == "all") {
runLandmarks = true
landmarkModeValue = FaceDetectorOptions.LANDMARK_MODE_ALL
}
if (options?.get("classificationMode").toString() == "all") {
runClassifications = true
classificationModeValue = FaceDetectorOptions.CLASSIFICATION_MODE_ALL
}
if (options?.get("contourMode").toString() == "all") {
runContours = true
contourModeValue = FaceDetectorOptions.CONTOUR_MODE_ALL
}
val minFaceSizeParam = options?.get("minFaceSize").toString()
if (
minFaceSizeParam != "null" &&
minFaceSizeParam != minFaceSize.toString()
) {
minFaceSize = minFaceSizeParam.toFloat()
}
val optionsBuilder = FaceDetectorOptions.Builder()
.setPerformanceMode(performanceModeValue)
.setLandmarkMode(landmarkModeValue)
.setContourMode(contourModeValue)
.setClassificationMode(classificationModeValue)
.setMinFaceSize(minFaceSize)
if (options?.get("trackingEnabled").toString() == "true") {
trackingEnabled = true
optionsBuilder.enableTracking()
}
faceDetector = FaceDetection.getClient(
optionsBuilder.build()
)
}
private fun processBoundingBox(
boundingBox: Rect,
sourceWidth: Double,
scaleX: Double,
scaleY: Double
): Map<String, Any> {
val bounds: MutableMap<String, Any> = HashMap()
val width = boundingBox.width().toDouble() * scaleX
val x = boundingBox.left.toDouble() * scaleX
bounds["width"] = width
bounds["height"] = boundingBox.height().toDouble() * scaleY
bounds["x"] = (-x + sourceWidth * scaleX) - width
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
}
private fun getFrameRotation(
orientation: Orientation
): Int {
return when (orientation) {
Orientation.PORTRAIT -> 0
Orientation.LANDSCAPE_LEFT -> 90
Orientation.PORTRAIT_UPSIDE_DOWN -> 180
Orientation.LANDSCAPE_RIGHT -> 270
}
}
override fun callback(
frame: Frame,
params: Map<String, Any>?
): Any {
val result = ArrayList<Map<String, Any>>()
try {
val rotation = getFrameRotation(frame.orientation)
val image = InputImage.fromMediaImage(frame.image, rotation)
val sourceWidth: Double
val sourceHeight: Double
if (rotation == 270 || rotation == 90) {
sourceWidth = image.height.toDouble()
sourceHeight = image.width.toDouble()
} else {
sourceWidth = image.width.toDouble()
sourceHeight = image.height.toDouble()
}
val scaleX = if(autoScale) windowWidth / sourceWidth else 1.0
val scaleY = if(autoScale) windowHeight / sourceHeight else 1.0
val task = faceDetector!!.process(image)
val faces = Tasks.await(task)
faces.forEach{face ->
val map: MutableMap<String, Any> = HashMap()
if (runLandmarks) {
map["landmarks"] = processLandmarks(
face,
scaleX,
scaleY
)
}
if (runClassifications) {
map["leftEyeOpenProbability"] = face.leftEyeOpenProbability?.toDouble() ?: -1
map["rightEyeOpenProbability"] = face.rightEyeOpenProbability?.toDouble() ?: -1
map["smilingProbability"] = face.smilingProbability?.toDouble() ?: -1
}
if (runContours) {
map["contours"] = processFaceContours(
face,
scaleX,
scaleY
)
}
if (trackingEnabled) {
map["trackingId"] = face.trackingId ?: -1
}
map["rollAngle"] = face.headEulerAngleZ.toDouble()
map["pitchAngle"] = face.headEulerAngleX.toDouble()
map["yawAngle"] = face.headEulerAngleY.toDouble()
map["bounds"] = processBoundingBox(
face.boundingBox,
sourceWidth,
scaleX,
scaleY
)
result.add(map)
}
} catch (e: Exception) {
Log.e(TAG, "Error processing face detection: ", e)
} catch (e: FrameInvalidError) {
Log.e(TAG, "Frame invalid error: ", e)
}
return result
}
}